157b50c58e
* Upload: fix redux key clash ## Issue `params` is the "final" value that will be passed to the SDK and `channel` is not a valid argument (it should be `channel_name`). Also, it seems like we only pass the channel ID now and skip the channel name entirely. For the anonymous case, a clash will still happen when since the channel part is hardcoded to `anonymous`. ## Approach Generate a guid in `params` and use that as the key to handle all the cases above. We couldn't use the `uploadUrl` because v1 doesn't have it. The old formula is retained to allow users to retry or cancel their existing uploads one last time (otherwise it will persist forever). The next upload will be using the new key. * Upload: add tab-locking ## Issue - The previous code does detect uploads from multiple tabs, but it was done by handling the CONFLICT error message from the backend. At certain corner-cases, this does not work well. A better way is to not allow resumption while the same file is being uploading from another tab. - When an upload from 1 tab finishes, the GUI on the other tab does not remove the completed item. User either have to refresh or click Cancel. Clicking Cancel results in the 404 backend error. This should be avoided. ## Approach - Added tab synchronization and locking by passing the "locked" and "removed" information through `localStorage`. ## Other considered approaches - Wallet sync -- but decided not to pollute the wallet. - 3rd-party redux tab syncing -- but decided it's not worth adding another module for 1 usage. * Upload: check if locked before confirming delete ## Reproduce Have 2 tabs + paused upload Open "cancel" dialog in one of the tabs. Continue upload in other tab Confirm cancellation in first tab Upload disappears from both tabs, but based on network traffic the upload keeps happening. (If upload finishes the claim seems to get created)
67 lines
2 KiB
JavaScript
67 lines
2 KiB
JavaScript
// @flow
|
|
import * as tus from 'tus-js-client';
|
|
import { v4 as uuid } from 'uuid';
|
|
import { makeUploadRequest } from './publish-v1';
|
|
import { makeResumableUploadRequest } from './publish-v2';
|
|
|
|
// A modified version of Lbry.apiCall that allows
|
|
// to perform calling methods at arbitrary urls
|
|
// and pass form file fields
|
|
export default function apiPublishCallViaWeb(
|
|
apiCall: (any, any, any, any) => any,
|
|
token: string,
|
|
method: string,
|
|
params: FileUploadSdkParams,
|
|
resolve: Function,
|
|
reject: Function
|
|
) {
|
|
const { file_path: filePath, preview, remote_url: remoteUrl } = params;
|
|
|
|
if (!filePath && !remoteUrl) {
|
|
return apiCall(method, params, resolve, reject);
|
|
}
|
|
|
|
let fileField = filePath;
|
|
|
|
if (preview) {
|
|
// Send dummy file for the preview. The tx-fee calculation does not depend on it.
|
|
const dummyContent = 'x';
|
|
fileField = new File([dummyContent], 'dummy.md', { type: 'text/markdown' });
|
|
}
|
|
|
|
// Putting a dummy value here, the server is going to process the POSTed file
|
|
// and set the file_path itself
|
|
if (fileField) {
|
|
params.file_path = '__POST_FILE__';
|
|
}
|
|
|
|
// Add a random ID to serve as the redux key.
|
|
// If it already exists, then it is a resumed session.
|
|
if (!params.guid) {
|
|
params.guid = uuid();
|
|
}
|
|
|
|
const useV1 = remoteUrl || preview || !tus.isSupported;
|
|
|
|
// Note: both function signature (params) should match.
|
|
const makeRequest = useV1 ? makeUploadRequest : makeResumableUploadRequest;
|
|
|
|
return makeRequest(token, params, fileField, preview)
|
|
.then((xhr) => {
|
|
let error;
|
|
if (xhr && xhr.response) {
|
|
if (xhr.status >= 200 && xhr.status < 300 && !xhr.response.error) {
|
|
return resolve(xhr.response.result);
|
|
} else if (xhr.response.error) {
|
|
error = new Error(xhr.response.error.message);
|
|
} else {
|
|
error = new Error(__('Upload likely timed out. Try a smaller file while we work on this.'));
|
|
}
|
|
}
|
|
|
|
if (error) {
|
|
return Promise.reject(error);
|
|
}
|
|
})
|
|
.catch(reject);
|
|
}
|