Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow don`t save urls to localStorage #313

Merged
merged 5 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 for allow resuming after browser restart. Default value: `true`
kukhariev marked this conversation as resolved.
Show resolved Hide resolved
kukhariev marked this conversation as resolved.
Show resolved Hide resolved
kukhariev marked this conversation as resolved.
Show resolved Hide resolved

- `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: false,
kukhariev marked this conversation as resolved.
Show resolved Hide resolved
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 for allow resuming after browser restart
* @defaultValue true
*/
storeIncompleteUploadUrl?: boolean;
}

export type UploaderClass = new (
Expand Down
12 changes: 10 additions & 2 deletions src/uploadx/lib/store.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
class Store {
prefix = 'UPLOADX-V3.0-';

set(key: string, value: string): void {
set(key: string, value: string): this {
localStorage.setItem(this.prefix + key, value);
return this;
}

get(key: string): string | null {
return localStorage.getItem(this.prefix + key);
}

delete(key: string): void {
delete(key: string): boolean {
kukhariev marked this conversation as resolved.
Show resolved Hide resolved
localStorage.removeItem(this.prefix + key);
return true;
}

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

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 @@ -254,7 +255,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