Skip to content

Commit

Permalink
Update: support uploading new version (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenboyu2 authored Jan 10, 2018
1 parent 1de962e commit fe9a0c9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 63 deletions.
35 changes: 21 additions & 14 deletions src/api/PlainUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import type { BoxItem } from '../flowTypes';

class PlainUpload extends BaseUpload {
file: File;
id: string;
folderId: string;
fileId: ?string;
overwrite: boolean;
retryTimeout: number;
successCallback: Function;
Expand Down Expand Up @@ -94,6 +95,10 @@ class PlainUpload extends BaseUpload {
return;
}

if (!this.fileId && !!fileId) {
this.fileId = fileId;
}

let url = `${this.getBaseUrl()}/files/content`;
if (fileId) {
url = url.replace('content', `${fileId}/content`);
Expand All @@ -102,7 +107,7 @@ class PlainUpload extends BaseUpload {
const { size, name } = this.file;
const attributes = {
name: fileName || name,
parent: { id: this.id },
parent: { id: this.folderId },
size
};

Expand All @@ -120,11 +125,9 @@ class PlainUpload extends BaseUpload {
*
* @param {Object} - Request options
* @param {boolean} [options.url] - Upload URL to use
* @param {string} [options.fileId] - ID of file to replace
* @param {string} [options.fileName] - New name for file
* @return {Promise<*>}
*/
async makeRequest({ url, fileId, fileName }: { url?: string, fileId?: string, fileName?: string }): Promise<*> {
async makeRequest({ url }: { url?: string }): Promise<*> {
if (this.isDestroyed()) {
return;
}
Expand All @@ -136,14 +139,14 @@ class PlainUpload extends BaseUpload {
if (!uploadUrl) {
uploadUrl = `${this.uploadHost}/api/2.0/files/content`;

if (fileId) {
uploadUrl = uploadUrl.replace('content', `${fileId}/content`);
if (this.fileId) {
uploadUrl = uploadUrl.replace('content', `${this.fileId}/content`);
}
}

const attributes = JSON.stringify({
name: fileName || this.file.name,
parent: { id: this.id }
name: this.file.name,
parent: { id: this.folderId }
});

this.xhr.uploadFile({
Expand All @@ -163,7 +166,8 @@ class PlainUpload extends BaseUpload {
* Otherwise, re-upload with a different name.
*
* @param {Object} options - Upload options
* @param {string} options.id - Folder id
* @param {string} options.folderId - untyped folder id
* @param {string} [options.fileId] - Untyped file id (e.g. no "file_" prefix)
* @param {File} options.file - File blob object
* @param {Function} [options.successCallback] - Function to call with response
* @param {Function} [options.errorCallback] - Function to call with errors
Expand All @@ -172,14 +176,16 @@ class PlainUpload extends BaseUpload {
* @return {void}
*/
upload({
id,
folderId,
fileId,
file,
successCallback = noop,
errorCallback = noop,
progressCallback = noop,
overwrite = true
}: {
id: string,
folderId: string,
fileId: ?string,
file: File,
successCallback: Function,
errorCallback: Function,
Expand All @@ -191,14 +197,15 @@ class PlainUpload extends BaseUpload {
}

// Save references
this.id = id;
this.folderId = folderId;
this.fileId = fileId;
this.file = file;
this.successCallback = successCallback;
this.errorCallback = errorCallback;
this.progressCallback = progressCallback;
this.overwrite = overwrite;

this.makePreflightRequest({});
this.makePreflightRequest(fileId ? { fileId } : {});
}

/**
Expand Down
43 changes: 10 additions & 33 deletions src/api/__tests__/PlainUpload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('api/PlainUpload', () => {
size: 1,
name: 'zavala'
};
upload.id = '123';
upload.folderId = '123';

upload.xhr = {
options: jest.fn()
Expand All @@ -138,7 +138,7 @@ describe('api/PlainUpload', () => {
data: {
name: upload.file.name,
parent: {
id: upload.id
id: upload.folderId
},
size: upload.file.size
},
Expand All @@ -157,7 +157,7 @@ describe('api/PlainUpload', () => {
size: 1,
name: 'zavala'
};
upload.id = '123';
upload.folderId = '123';

upload.xhr = {
options: jest.fn()
Expand Down Expand Up @@ -194,7 +194,7 @@ describe('api/PlainUpload', () => {
upload.file = {
name: 'warlock'
};
upload.id = '123';
upload.folderId = '123';
upload.xhr = {
uploadFile: jest.fn()
};
Expand All @@ -219,14 +219,13 @@ describe('api/PlainUpload', () => {
upload.file = {
name: 'warlock'
};
upload.id = '123';
upload.fileId = fileId;
upload.folderId = '123';
upload.xhr = {
uploadFile: jest.fn()
};

await upload.makeRequest({
fileId
});
await upload.makeRequest({});
expect(upload.xhr.uploadFile).toHaveBeenCalledWith({
url: `${upload.uploadHost}/api/2.0/files/${fileId}/content`,
data: expect.any(Object),
Expand All @@ -235,28 +234,6 @@ describe('api/PlainUpload', () => {
progressHandler: expect.any(Function)
});
});

test('should stringify name and parent for upload data', async () => {
const name = 'titan';
const parentId = '123';
JSON.stringify = jest.fn();

upload.isDestroyed = jest.fn().mockReturnValueOnce(false);
upload.id = parentId;
upload.xhr = {
uploadFile: jest.fn()
};

await upload.makeRequest({
fileName: name
});
expect(JSON.stringify).toHaveBeenCalledWith({
name,
parent: {
id: parentId
}
});
});
});

describe('upload()', () => {
Expand All @@ -268,7 +245,7 @@ describe('api/PlainUpload', () => {
});

test('should set properties and make preflight request', () => {
const id = '123';
const folderId = '123';
const file = {};
const successCallback = () => {};
const errorCallback = () => {};
Expand All @@ -278,15 +255,15 @@ describe('api/PlainUpload', () => {
upload.isDestroyed = jest.fn().mockReturnValueOnce(false);
upload.makePreflightRequest = jest.fn();
upload.upload({
id,
folderId,
file,
successCallback,
errorCallback,
progressCallback,
overwrite
});

expect(upload.id).toBe(id);
expect(upload.folderId).toBe(folderId);
expect(upload.file).toBe(file);
expect(upload.successCallback).toBe(successCallback);
expect(upload.errorCallback).toBe(errorCallback);
Expand Down
19 changes: 12 additions & 7 deletions src/api/uploads/MultiputUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MultiputUpload extends BaseMultiput {
commitRetryCount: number;
createSessionNumRetriesPerformed: number;
destinationFileId: ?string;
folderId: ?string;
folderId: string;
file: File;
fileSha1: ?string;
firstUnuploadedPartIndex: number;
Expand Down Expand Up @@ -101,36 +101,37 @@ class MultiputUpload extends BaseMultiput {
*
* @param {Object} options
* @param {File} options.file
* @param {string} [options.id] - Untyped folder id (e.g. no "d_" prefix)
* @param {string} options.folderId - Untyped folder id (e.g. no "folder_" prefix)
* @param {string} [options.fileId] - Untyped file id (e.g. no "file_" prefix)
* @param {Function} [options.errorCallback]
* @param {Function} [options.progressCallback]
* @param {Function} [options.successCallback]
* @return {void}
*/
upload = ({
file,
id,
folderId,
errorCallback,
progressCallback,
successCallback,
overwrite = true,
fileId
}: {
file: File,
id?: ?string,
folderId: string,
errorCallback?: Function,
progressCallback?: Function,
successCallback?: Function,
overwrite?: boolean,
fileId?: string
fileId: ?string
}): void => {
this.file = file;
this.fileName = this.file.name;
// These values are used as part of our (best effort) attempt to abort uploads if we detect
// a file change during the upload.
this.initialFileSize = this.file.size;
this.initialFileLastModified = getFileLastModifiedAsISONoMSIfPossible(this.file);
this.folderId = id;
this.folderId = folderId;
this.errorCallback = errorCallback || noop;
this.progressCallback = progressCallback || noop;
this.successCallback = successCallback || noop;
Expand Down Expand Up @@ -472,7 +473,11 @@ class MultiputUpload extends BaseMultiput {
}: { buffer: ArrayBuffer, readCompleteTimestamp: number } = await this.readFile(reader, blob);
const sha256ArrayBuffer = await digest('SHA-256', buffer);
const sha256 = btoa(
new Uint8Array(sha256ArrayBuffer).reduce((data, byte) => data + String.fromCharCode(byte), '')
Array.prototype.reduce.call(
new Uint8Array(sha256ArrayBuffer),
(data, byte) => data + String.fromCharCode(byte),
''
)
);
this.sendPartToWorker(part, buffer);

Expand Down
24 changes: 15 additions & 9 deletions src/components/ContentUploader/ContentUploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import {
STATUS_IN_PROGRESS,
STATUS_COMPLETE,
STATUS_ERROR,
ERROR_CODE_UPLOAD_FILE_LIMIT
ERROR_CODE_UPLOAD_FILE_LIMIT,
TYPED_ID_FOLDER_PREFIX,
TYPED_ID_FILE_PREFIX
} from '../../constants';
import type {
BoxItem,
Expand Down Expand Up @@ -181,8 +183,11 @@ class ContentUploader extends Component<Props, State> {

const itemFolderId =
uploadAPIOptions && uploadAPIOptions.folderId
? `folder_${uploadAPIOptions.folderId}`
: `folder_${rootFolderId}`;
? `${TYPED_ID_FOLDER_PREFIX}${uploadAPIOptions.folderId}`
: `${TYPED_ID_FOLDER_PREFIX}${rootFolderId}`;
const itemFileId =
uploadAPIOptions && uploadAPIOptions.fileId ? `${TYPED_ID_FILE_PREFIX}${uploadAPIOptions.fileId}` : null;

const options = {
token,
sharedLink,
Expand All @@ -191,7 +196,7 @@ class ContentUploader extends Component<Props, State> {
uploadHost,
clientName,
responseFilter,
id: itemFolderId,
id: itemFileId || itemFolderId,
...uploadAPIOptions
};
return new API(options);
Expand Down Expand Up @@ -405,16 +410,17 @@ class ContentUploader extends Component<Props, State> {

this.numItemsUploading += 1;

api.upload({
// TODO: rename id to folderId
const uploadOptions: Object = {
file,
id: options && options.folderId ? options.folderId : rootFolderId,
folderId: options && options.folderId ? options.folderId : rootFolderId,
errorCallback: (error) => this.handleUploadError(item, error),
progressCallback: (event) => this.handleUploadProgress(item, event),
successCallback: (entries) => this.handleUploadSuccess(item, entries),
overwrite: true,
fileId: options && options.fileId ? options.fileId : undefined
});
fileId: options && options.fileId ? options.fileId : null
};

api.upload(uploadOptions);

item.status = STATUS_IN_PROGRESS;
const { items } = this.state;
Expand Down

0 comments on commit fe9a0c9

Please sign in to comment.