Skip to content

Commit

Permalink
feat: store uploads meta in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev committed Sep 11, 2019
1 parent 9a6c5e7 commit 6aa3ed6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
21 changes: 21 additions & 0 deletions src/uploadx/src/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const UPLOADX_KEY = 'UPLOADX_V3.0';
class FileStore {
store: Record<string, string> = {};
constructor() {
const _all = localStorage.getItem(UPLOADX_KEY) || '{}';
this.store = JSON.parse(_all);
}

set(id: string, data: any) {
this.store[id] = data;
localStorage.setItem(UPLOADX_KEY, JSON.stringify(this.store));
}
get(id: string) {
return this.store[id];
}
remove(id: string) {
delete this.store[id];
localStorage.setItem(UPLOADX_KEY, JSON.stringify(this.store));
}
}
export const store = new FileStore();
33 changes: 27 additions & 6 deletions src/uploadx/src/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
UploadStatus,
UploadxControlEvent
} from './interfaces';
import { store } from './store';
import { actionToStatusMap, createHash, isNumber, noop, unfunc } from './utils';

/**
Expand Down Expand Up @@ -48,12 +49,13 @@ export abstract class Uploader implements UploadState {
}
if (s !== this._status) {
s === 'paused' && this.abort();
s === 'cancelled' && this.abort();
s === 'cancelled' && this.onCancel();
['cancel', 'complete', 'error'].includes(s) && this.cleanup();
this._status = s;
this.stateChange(this);
}
}

get status() {
return this._status;
}
Expand Down Expand Up @@ -91,7 +93,15 @@ export abstract class Uploader implements UploadState {
/**
* File URI
*/
url: string;
protected _url = '';
public get url(): string {
return this._url || store.get(this.uploadId);
}
public set url(value: string) {
this._url !== value && store.set(this.uploadId, value);
this._url = value;
}

/**
* Custom headers
*/
Expand Down Expand Up @@ -160,7 +170,7 @@ export abstract class Uploader implements UploadState {
* UploadState emitter
*/
private stateChange: (evt: UploadState) => void;

private cleanup = () => store.remove(this.uploadId);
constructor(readonly file: File, public options: UploaderOptions) {
this.name = file.name;
this.size = file.size;
Expand All @@ -171,7 +181,12 @@ export abstract class Uploader implements UploadState {
size: this.size,
lastModified: this.file.lastModified
};
this.uploadId = createHash(JSON.stringify(this.metadata)).toString(16);
const print = JSON.stringify({
...this.metadata,
type: this.constructor.name,
endpoint: options.endpoint
});
this.uploadId = createHash(print).toString(16);
this.stateChange = options.stateChange || noop;
this.chunkSize = options.chunkSize || Uploader.startingChunkSize;
this.configure(options);
Expand All @@ -195,7 +210,8 @@ export abstract class Uploader implements UploadState {
this.status = 'uploading';
try {
await this.getToken();
this.url = await this.getFileUrl();
this.offset = undefined;
this.url = this.url || (await this.getFileUrl());
this.retry.reset();
this.startTime = new Date().getTime();
this.start();
Expand Down Expand Up @@ -245,7 +261,10 @@ export abstract class Uploader implements UploadState {
this._xhr && this._xhr.abort();
}

protected onCancel(): void {}
protected onCancel(): void {
this.abort();
this.url && this.request({ method: 'DELETE' });
}

/**
* Gets the value from the response
Expand Down Expand Up @@ -287,6 +306,7 @@ export abstract class Uploader implements UploadState {
break;
}
if (this.isNotFoundError) {
this.url = '';
this.status = 'queue';
break;
}
Expand Down Expand Up @@ -338,6 +358,7 @@ export abstract class Uploader implements UploadState {
}
return body;
}

protected getChunk() {
const start = this.offset as number;
const end = this.chunkSize ? Math.min(start + this.chunkSize, this.size) : this.size;
Expand Down
4 changes: 0 additions & 4 deletions src/uploadx/src/uploaderx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ export class UploaderX extends Uploader {
}
}

protected onCancel(): void {
this.url && this.request({ method: 'DELETE' });
}

protected setAuth(token: string) {
this.headers.Authorization = `Bearer ${token}`;
}
Expand Down

0 comments on commit 6aa3ed6

Please sign in to comment.