Skip to content

Commit

Permalink
fix: allow don`t save urls to localStorage (#313)
Browse files Browse the repository at this point in the history
* fix: allow don`t save urls to localStorage

* refactor: increase uploadId length

* docs: update

* fix: compatibility with older browsers

* refactor: undo `store`
  • Loading branch information
kukhariev authored Jul 14, 2021
1 parent e9ac7d0 commit 5ad5d7e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export class AppHomeComponent {
- `autoUpload` Auto start upload when files added. Default value: `true`
- `storeIncompleteUploadUrl` Keep an incomplete upload URL to allow resuming after browser restart. Default value: `true`
- `chunkSize` Fixed chunk size. If not specified, the optimal size will be automatically adjusted based on the network speed.
If set to `0`, normal unloading will be used instead of chunked.
Expand Down
1 change: 1 addition & 0 deletions src/app/directive-way/directive-way.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class DirectiveWayComponent {
endpoint: `${environment.api}/files?uploadType=uploadx`,
token: this.authService.getAccessToken(),
maxChunkSize: 1024 * 1024 * 8,
storeIncompleteUploadUrl: true,
retryConfig: {
maxAttempts: 30,
maxDelay: 60_000,
Expand Down
2 changes: 1 addition & 1 deletion src/uploadx/lib/id.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export class IdService implements UidService {
type: uploader.constructor.name,
endpoint: uploader.endpoint
});
return createHash(print).toString(16);
return createHash(uploader.name + uploader.size).toString(16) + createHash(print).toString(16);
}
}
5 changes: 5 additions & 0 deletions src/uploadx/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ export interface UploaderOptions extends UploadItem {
* Function used to apply authorization token
*/
authorize?: AuthorizeRequest;
/**
* Keep an incomplete upload url to allow resuming after browser restart
* @defaultValue true
*/
storeIncompleteUploadUrl?: boolean;
}

export type UploaderClass = new (
Expand Down
6 changes: 6 additions & 0 deletions src/uploadx/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class Store {
delete(key: string): void {
localStorage.removeItem(this.prefix + key);
}

clear(): void {
Object.keys(localStorage).forEach(
key => key.indexOf(this.prefix) === 0 && localStorage.removeItem(key)
);
}
}

export const store = isLocalStorageAvailable() ? new Store() : new Map<string, string>();
Expand Down
7 changes: 4 additions & 3 deletions src/uploadx/lib/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ export abstract class Uploader implements UploadState {
private startTime!: number;
private _token!: string;
private _url = '';
private storeEnabled = this.options.storeIncompleteUploadUrl !== false;

get url(): string {
return this._url || store.get(this.uploadId) || '';
return this._url || (this.storeEnabled && store.get(this.uploadId)) || '';
}

set url(value: string) {
this._url !== value && store.set(this.uploadId, value);
this._url !== value && this.storeEnabled && store.set(this.uploadId, value);
this._url = value;
}

Expand Down Expand Up @@ -261,7 +262,7 @@ export abstract class Uploader implements UploadState {
return { start, end, body };
}

private cleanup = () => store.delete(this.uploadId);
private cleanup = () => this.storeEnabled && store.delete(this.uploadId);

private onProgress(): (evt: ProgressEvent) => void {
let throttle: ReturnType<typeof setTimeout> | undefined;
Expand Down

0 comments on commit 5ad5d7e

Please sign in to comment.