Upload: tab sync and various fixes (#428)

* 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)
This commit is contained in:
infinite-persistence 2021-12-07 06:48:09 -08:00 committed by GitHub
parent e982d9c13c
commit 157b50c58e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 286 additions and 58 deletions

View file

@ -62,7 +62,8 @@ declare type FileUploadSdkParams = {
remote_url?: string, remote_url?: string,
thumbnail_url?: string, thumbnail_url?: string,
title?: string, title?: string,
// Temporary values // Temporary values; remove when passing to SDK
guid: string,
uploadUrl?: string, uploadUrl?: string,
}; };

View file

@ -1307,6 +1307,8 @@
"Uploading...": "Uploading...", "Uploading...": "Uploading...",
"Creating...": "Creating...", "Creating...": "Creating...",
"Stopped. Duplicate session detected.": "Stopped. Duplicate session detected.", "Stopped. Duplicate session detected.": "Stopped. Duplicate session detected.",
"File being uploaded in another tab or window.": "File being uploaded in another tab or window.",
"There are pending uploads.": "There are pending uploads.",
"Use a URL": "Use a URL", "Use a URL": "Use a URL",
"Edit Cover Image": "Edit Cover Image", "Edit Cover Image": "Edit Cover Image",
"Cover Image": "Cover Image", "Cover Image": "Cover Image",

View file

@ -2,6 +2,7 @@
import * as PAGES from 'constants/pages'; import * as PAGES from 'constants/pages';
import React, { useEffect, useRef, useState, useLayoutEffect } from 'react'; import React, { useEffect, useRef, useState, useLayoutEffect } from 'react';
import { lazyImport } from 'util/lazyImport'; import { lazyImport } from 'util/lazyImport';
import { tusUnlockAndNotify, tusHandleTabUpdates } from 'util/tus';
import classnames from 'classnames'; import classnames from 'classnames';
import analytics from 'analytics'; import analytics from 'analytics';
import { setSearchUserId } from 'redux/actions/search'; import { setSearchUserId } from 'redux/actions/search';
@ -231,12 +232,29 @@ function App(props: Props) {
useEffect(() => { useEffect(() => {
if (!uploadCount) return; if (!uploadCount) return;
const handleUnload = (event) => tusUnlockAndNotify();
const handleBeforeUnload = (event) => { const handleBeforeUnload = (event) => {
event.preventDefault(); event.preventDefault();
event.returnValue = 'magic'; // without setting this to something it doesn't work event.returnValue = __('There are pending uploads.'); // without setting this to something it doesn't work
}; };
window.addEventListener('unload', handleUnload);
window.addEventListener('beforeunload', handleBeforeUnload); window.addEventListener('beforeunload', handleBeforeUnload);
return () => window.removeEventListener('beforeunload', handleBeforeUnload);
return () => {
window.removeEventListener('unload', handleUnload);
window.removeEventListener('beforeunload', handleBeforeUnload);
};
}, [uploadCount]);
useEffect(() => {
if (!uploadCount) return;
const onStorageUpdate = (e) => tusHandleTabUpdates(e.key);
window.addEventListener('storage', onStorageUpdate);
return () => window.removeEventListener('storage', onStorageUpdate);
}, [uploadCount]); }, [uploadCount]);
// allows user to pause miniplayer using the spacebar without the page scrolling down // allows user to pause miniplayer using the spacebar without the page scrolling down

View file

@ -5,11 +5,12 @@ import Button from 'component/button';
import FileThumbnail from 'component/fileThumbnail'; import FileThumbnail from 'component/fileThumbnail';
import * as MODALS from 'constants/modal_types'; import * as MODALS from 'constants/modal_types';
import { serializeFileObj } from 'util/file'; import { serializeFileObj } from 'util/file';
import { tusIsSessionLocked } from 'util/tus';
type Props = { type Props = {
uploadItem: FileUploadItem, uploadItem: FileUploadItem,
doPublishResume: (any) => void, doPublishResume: (any) => void,
doUpdateUploadRemove: (any) => void, doUpdateUploadRemove: (string, any) => void,
doOpenModal: (string, {}) => void, doOpenModal: (string, {}) => void,
}; };
@ -18,11 +19,16 @@ export default function WebUploadItem(props: Props) {
const { params, file, fileFingerprint, progress, status, resumable, uploader } = uploadItem; const { params, file, fileFingerprint, progress, status, resumable, uploader } = uploadItem;
const [showFileSelector, setShowFileSelector] = useState(false); const [showFileSelector, setShowFileSelector] = useState(false);
const locked = tusIsSessionLocked(params.guid);
function handleFileChange(newFile: WebFile, clearName = true) { function handleFileChange(newFile: WebFile, clearName = true) {
if (serializeFileObj(newFile) === fileFingerprint) { if (serializeFileObj(newFile) === fileFingerprint) {
setShowFileSelector(false); setShowFileSelector(false);
doPublishResume({ ...params, file_path: newFile }); doPublishResume({ ...params, file_path: newFile });
if (!params.guid) {
// Can remove this if-block after January 2022.
doUpdateUploadRemove('', params);
}
} else { } else {
doOpenModal(MODALS.CONFIRM, { doOpenModal(MODALS.CONFIRM, {
title: __('Invalid file'), title: __('Invalid file'),
@ -40,6 +46,12 @@ export default function WebUploadItem(props: Props) {
subtitle: __('Cancel and remove the selected upload?'), subtitle: __('Cancel and remove the selected upload?'),
body: params.name ? <p className="empty">{`lbry://${params.name}`}</p> : undefined, body: params.name ? <p className="empty">{`lbry://${params.name}`}</p> : undefined,
onConfirm: (closeModal) => { onConfirm: (closeModal) => {
if (tusIsSessionLocked(params.guid)) {
// Corner-case: it's possible for the upload to resume in another tab
// after the modal has appeared. Make a final lock-check here.
// We can invoke a toast here, but just do nothing for now.
// The upload status should make things obvious.
} else {
if (uploader) { if (uploader) {
if (resumable) { if (resumable) {
// $FlowFixMe - couldn't resolve to TusUploader manually. // $FlowFixMe - couldn't resolve to TusUploader manually.
@ -48,13 +60,20 @@ export default function WebUploadItem(props: Props) {
uploader.abort(); // XHR uploader.abort(); // XHR
} }
} }
doUpdateUploadRemove(params);
// The second parameter (params) can be removed after January 2022.
doUpdateUploadRemove(params.guid, params);
}
closeModal(); closeModal();
}, },
}); });
} }
function resolveProgressStr() { function resolveProgressStr() {
if (locked) {
return __('File being uploaded in another tab or window.');
}
if (!uploader) { if (!uploader) {
return __('Stopped.'); return __('Stopped.');
} }
@ -81,7 +100,7 @@ export default function WebUploadItem(props: Props) {
} }
function getRetryButton() { function getRetryButton() {
if (!resumable) { if (!resumable || locked) {
return null; return null;
} }
@ -109,8 +128,10 @@ export default function WebUploadItem(props: Props) {
} }
function getCancelButton() { function getCancelButton() {
if (!locked) {
return <Button label={__('Cancel')} button="link" onClick={handleCancel} />; return <Button label={__('Cancel')} button="link" onClick={handleCancel} />;
} }
}
function getFileSelector() { function getFileSelector() {
return ( return (

View file

@ -7,7 +7,7 @@ type Props = {
currentUploads: { [key: string]: FileUploadItem }, currentUploads: { [key: string]: FileUploadItem },
uploadCount: number, uploadCount: number,
doPublishResume: (any) => void, doPublishResume: (any) => void,
doUpdateUploadRemove: (any) => void, doUpdateUploadRemove: (string, any) => void,
doOpenModal: (string, {}) => void, doOpenModal: (string, {}) => void,
}; };

3
ui/constants/storage.js Normal file
View file

@ -0,0 +1,3 @@
// Local Storage keys
export const TUS_LOCKED_UPLOADS = 'tusLockedUploads';
export const TUS_REMOVED_UPLOADS = 'tusRemovedUploads';

View file

@ -702,21 +702,28 @@ export function doUpdateUploadAdd(
}; };
} }
export const doUpdateUploadProgress = (props: { export const doUpdateUploadProgress = (props: { guid: string, progress?: string, status?: string }) => (
params: { [key: string]: any }, dispatch: Dispatch
progress?: string, ) =>
status?: string,
}) => (dispatch: Dispatch) =>
dispatch({ dispatch({
type: ACTIONS.UPDATE_UPLOAD_PROGRESS, type: ACTIONS.UPDATE_UPLOAD_PROGRESS,
data: props, data: props,
}); });
export function doUpdateUploadRemove(params: { [key: string]: any }) { /**
* doUpdateUploadRemove
*
* @param guid
* @param params Optional. Retain to allow removal of old keys, which are
* derived from `name#channel` instead of using a guid.
* Can be removed after January 2022.
* @returns {(function(Dispatch, GetState): void)|*}
*/
export function doUpdateUploadRemove(guid: string, params?: { [key: string]: any }) {
return (dispatch: Dispatch, getState: GetState) => { return (dispatch: Dispatch, getState: GetState) => {
dispatch({ dispatch({
type: ACTIONS.UPDATE_UPLOAD_REMOVE, type: ACTIONS.UPDATE_UPLOAD_REMOVE,
data: { params }, data: { guid, params },
}); });
}; };
} }

View file

@ -2,11 +2,14 @@
import { handleActions } from 'util/redux-utils'; import { handleActions } from 'util/redux-utils';
import { buildURI } from 'util/lbryURI'; import { buildURI } from 'util/lbryURI';
import { serializeFileObj } from 'util/file'; import { serializeFileObj } from 'util/file';
import { tusLockAndNotify, tusUnlockAndNotify, tusRemoveAndNotify, tusClearRemovedUploads } from 'util/tus';
import * as ACTIONS from 'constants/action_types'; import * as ACTIONS from 'constants/action_types';
import * as THUMBNAIL_STATUSES from 'constants/thumbnail_upload_statuses'; import * as THUMBNAIL_STATUSES from 'constants/thumbnail_upload_statuses';
import { CHANNEL_ANONYMOUS } from 'constants/claim'; import { CHANNEL_ANONYMOUS } from 'constants/claim';
const getKeyFromParam = (params) => `${params.name}#${params.channel || 'anonymous'}`; // This is the old key formula. Retain it for now to allow users to delete
// any pending uploads. Can be removed from January 2022 onwards.
const getOldKeyFromParam = (params) => `${params.name}#${params.channel || 'anonymous'}`;
type PublishState = { type PublishState = {
editingURI: ?string, editingURI: ?string,
@ -138,10 +141,9 @@ export const publishReducer = handleActions(
}, },
[ACTIONS.UPDATE_UPLOAD_ADD]: (state: PublishState, action) => { [ACTIONS.UPDATE_UPLOAD_ADD]: (state: PublishState, action) => {
const { file, params, uploader } = action.data; const { file, params, uploader } = action.data;
const key = getKeyFromParam(params);
const currentUploads = Object.assign({}, state.currentUploads); const currentUploads = Object.assign({}, state.currentUploads);
currentUploads[key] = { currentUploads[params.guid] = {
file, file,
fileFingerprint: file ? serializeFileObj(file) : undefined, // TODO: get hash instead? fileFingerprint: file ? serializeFileObj(file) : undefined, // TODO: get hash instead?
progress: '0', progress: '0',
@ -153,10 +155,14 @@ export const publishReducer = handleActions(
return { ...state, currentUploads }; return { ...state, currentUploads };
}, },
[ACTIONS.UPDATE_UPLOAD_PROGRESS]: (state: PublishState, action) => { [ACTIONS.UPDATE_UPLOAD_PROGRESS]: (state: PublishState, action) => {
const { params, progress, status } = action.data; const { guid, progress, status } = action.data;
const key = getKeyFromParam(params); const key = guid;
const currentUploads = Object.assign({}, state.currentUploads); const currentUploads = Object.assign({}, state.currentUploads);
if (guid === 'force--update') {
return { ...state, currentUploads };
}
if (!currentUploads[key]) { if (!currentUploads[key]) {
return state; return state;
} }
@ -165,11 +171,17 @@ export const publishReducer = handleActions(
currentUploads[key].progress = progress; currentUploads[key].progress = progress;
delete currentUploads[key].status; delete currentUploads[key].status;
if (currentUploads[key].uploader.url && !currentUploads[key].params.uploadUrl) { if (currentUploads[key].uploader.url) {
// TUS has finally obtained an upload url from the server. Stash that to check later when resuming. // TUS has finally obtained an upload url from the server...
if (!currentUploads[key].params.uploadUrl) {
// ... Stash that to check later when resuming.
// Ignoring immutable-update requirement (probably doesn't matter to the GUI). // Ignoring immutable-update requirement (probably doesn't matter to the GUI).
currentUploads[key].params.uploadUrl = currentUploads[key].uploader.url; currentUploads[key].params.uploadUrl = currentUploads[key].uploader.url;
} }
// ... lock this tab as the active uploader.
tusLockAndNotify(key);
}
} else if (status) { } else if (status) {
currentUploads[key].status = status; currentUploads[key].status = status;
if (status === 'error') { if (status === 'error') {
@ -180,13 +192,19 @@ export const publishReducer = handleActions(
return { ...state, currentUploads }; return { ...state, currentUploads };
}, },
[ACTIONS.UPDATE_UPLOAD_REMOVE]: (state: PublishState, action) => { [ACTIONS.UPDATE_UPLOAD_REMOVE]: (state: PublishState, action) => {
const { params } = action.data; const { guid, params } = action.data;
const key = getKeyFromParam(params); const key = guid || getOldKeyFromParam(params);
const currentUploads = Object.assign({}, state.currentUploads);
if (state.currentUploads[key]) {
const currentUploads = Object.assign({}, state.currentUploads);
delete currentUploads[key]; delete currentUploads[key];
tusUnlockAndNotify(key);
tusRemoveAndNotify(key);
return { ...state, currentUploads }; return { ...state, currentUploads };
}
return state;
}, },
[ACTIONS.REHYDRATE]: (state: PublishState, action) => { [ACTIONS.REHYDRATE]: (state: PublishState, action) => {
if (action && action.payload && action.payload.publish) { if (action && action.payload && action.payload.publish) {
@ -194,7 +212,10 @@ export const publishReducer = handleActions(
// Cleanup for 'publish::currentUploads' // Cleanup for 'publish::currentUploads'
if (newPublish.currentUploads) { if (newPublish.currentUploads) {
Object.keys(newPublish.currentUploads).forEach((key) => { const uploadKeys = Object.keys(newPublish.currentUploads);
if (uploadKeys.length > 0) {
// Clear uploader and corrupted params
uploadKeys.forEach((key) => {
const params = newPublish.currentUploads[key].params; const params = newPublish.currentUploads[key].params;
if (!params || Object.keys(params).length === 0) { if (!params || Object.keys(params).length === 0) {
delete newPublish.currentUploads[key]; delete newPublish.currentUploads[key];
@ -202,6 +223,9 @@ export const publishReducer = handleActions(
delete newPublish.currentUploads[key].uploader; delete newPublish.currentUploads[key].uploader;
} }
}); });
} else {
tusClearRemovedUploads();
}
} }
return newPublish; return newPublish;

15
ui/util/storage.js Normal file
View file

@ -0,0 +1,15 @@
export function isLocalStorageAvailable() {
try {
return Boolean(window.localStorage);
} catch (e) {
return false;
}
}
export function isSessionStorageAvailable() {
try {
return Boolean(window.sessionStorage);
} catch (e) {
return false;
}
}

129
ui/util/tus.js Normal file
View file

@ -0,0 +1,129 @@
// @flow
/**
* This serves a bridge between tabs using localStorage to indicate whether an
* upload is currently in progress (locked) or removed.
*
* An alternative is to sync the redux's 'publish::currentUploads' through the
* wallet's sync process, but let's not pollute the wallet for now.
*/
import { v4 as uuid } from 'uuid';
import { TUS_LOCKED_UPLOADS, TUS_REMOVED_UPLOADS } from 'constants/storage';
import { isLocalStorageAvailable } from 'util/storage';
import { doUpdateUploadRemove, doUpdateUploadProgress } from 'redux/actions/publish';
const localStorageAvailable = isLocalStorageAvailable();
let gTabId: string = '';
function getTabId() {
if (!gTabId) {
// We want to maximize bootup speed, so only initialize
// the tab ID on first use instead when declared.
gTabId = uuid();
}
return gTabId;
}
// ****************************************************************************
// Locked
// ****************************************************************************
function getLockedUploads() {
if (localStorageAvailable) {
const storedValue = window.localStorage.getItem(TUS_LOCKED_UPLOADS);
return storedValue ? JSON.parse(storedValue) : {};
}
return {};
}
export function tusIsSessionLocked(guid: string) {
const lockedUploads = getLockedUploads();
return lockedUploads[guid] && lockedUploads[guid] !== getTabId();
}
export function tusLockAndNotify(guid: string) {
const lockedUploads = getLockedUploads();
if (!lockedUploads[guid] && localStorageAvailable) {
lockedUploads[guid] = getTabId();
window.localStorage.setItem(TUS_LOCKED_UPLOADS, JSON.stringify(lockedUploads));
}
}
/**
* tusUnlockAndNotify
*
* @param guid The upload session to unlock and notify other tabs of.
* Passing 'undefined' will clear all sessions locked by this tab.
*/
export function tusUnlockAndNotify(guid?: string) {
if (!localStorageAvailable) return;
const lockedUploads = getLockedUploads();
if (guid) {
delete lockedUploads[guid];
} else {
const ourTabId = getTabId();
const lockedUploadsEntries = Object.entries(lockedUploads);
lockedUploadsEntries.forEach(([lockedGuid, tabId]) => {
if (tabId === ourTabId) {
delete lockedUploads[lockedGuid];
}
});
}
if (Object.keys(lockedUploads).length > 0) {
window.localStorage.setItem(TUS_LOCKED_UPLOADS, JSON.stringify(lockedUploads));
} else {
window.localStorage.removeItem(TUS_LOCKED_UPLOADS);
}
}
// ****************************************************************************
// Removed
// ****************************************************************************
function getRemovedUploads() {
if (localStorageAvailable) {
const storedValue = window.localStorage.getItem(TUS_REMOVED_UPLOADS);
return storedValue ? storedValue.split(',') : [];
}
return [];
}
export function tusRemoveAndNotify(guid: string) {
if (!localStorageAvailable) return;
const removedUploads = getRemovedUploads();
removedUploads.push(guid);
window.localStorage.setItem(TUS_REMOVED_UPLOADS, removedUploads.join(','));
}
export function tusClearRemovedUploads() {
if (!localStorageAvailable) return;
window.localStorage.removeItem(TUS_REMOVED_UPLOADS);
}
// ****************************************************************************
// Respond to changes from other tabs.
// ****************************************************************************
export function tusHandleTabUpdates(storageKey: string) {
switch (storageKey) {
case TUS_LOCKED_UPLOADS:
// The locked IDs are in localStorage, but related GUI is unaware.
// Send a redux update to force an update.
window.store.dispatch(doUpdateUploadProgress({ guid: 'force--update' }));
break;
case TUS_REMOVED_UPLOADS:
// The other tab's store has removed this upload, so it's safe to do the
// same without affecting rehydration.
if (localStorageAvailable) {
const removedUploads = getRemovedUploads();
removedUploads.forEach((guid) => window.store.dispatch(doUpdateUploadRemove(guid)));
}
break;
}
}

View file

@ -30,10 +30,12 @@ export function makeUploadRequest(
delete params['remote_url']; delete params['remote_url'];
} }
const { uploadUrl, guid, ...sdkParams } = params;
const jsonPayload = JSON.stringify({ const jsonPayload = JSON.stringify({
jsonrpc: '2.0', jsonrpc: '2.0',
method: ENDPOINT_METHOD, method: ENDPOINT_METHOD,
params, params: sdkParams,
id: new Date().getTime(), id: new Date().getTime(),
}); });
@ -47,18 +49,18 @@ export function makeUploadRequest(
xhr.responseType = 'json'; xhr.responseType = 'json';
xhr.upload.onprogress = (e) => { xhr.upload.onprogress = (e) => {
const percentage = ((e.loaded / e.total) * 100).toFixed(2); const percentage = ((e.loaded / e.total) * 100).toFixed(2);
window.store.dispatch(doUpdateUploadProgress({ params, progress: percentage })); window.store.dispatch(doUpdateUploadProgress({ guid, progress: percentage }));
}; };
xhr.onload = () => { xhr.onload = () => {
window.store.dispatch(doUpdateUploadRemove(params)); window.store.dispatch(doUpdateUploadRemove(guid));
resolve(xhr); resolve(xhr);
}; };
xhr.onerror = () => { xhr.onerror = () => {
window.store.dispatch(doUpdateUploadProgress({ params, status: 'error' })); window.store.dispatch(doUpdateUploadProgress({ guid, status: 'error' }));
reject(new Error(__('There was a problem with your upload. Please try again.'))); reject(new Error(__('There was a problem with your upload. Please try again.')));
}; };
xhr.onabort = () => { xhr.onabort = () => {
window.store.dispatch(doUpdateUploadRemove(params)); window.store.dispatch(doUpdateUploadRemove(guid));
}; };
if (!isPreview) { if (!isPreview) {

View file

@ -38,13 +38,12 @@ export function makeResumableUploadRequest(
reject(new Error('Publish: v2 does not support remote_url')); reject(new Error('Publish: v2 does not support remote_url'));
} }
const payloadParams = Object.assign({}, params); const { uploadUrl, guid, ...sdkParams } = params;
delete payloadParams.uploadUrl; // cleanup
const jsonPayload = JSON.stringify({ const jsonPayload = JSON.stringify({
jsonrpc: '2.0', jsonrpc: '2.0',
method: RESUMABLE_ENDPOINT_METHOD, method: RESUMABLE_ENDPOINT_METHOD,
params: payloadParams, params: sdkParams,
id: new Date().getTime(), id: new Date().getTime(),
}); });
@ -70,7 +69,7 @@ export function makeResumableUploadRequest(
filetype: file instanceof File ? file.type : undefined, filetype: file instanceof File ? file.type : undefined,
}, },
onShouldRetry: (err, retryAttempt, options) => { onShouldRetry: (err, retryAttempt, options) => {
window.store.dispatch(doUpdateUploadProgress({ params, status: 'retry' })); window.store.dispatch(doUpdateUploadProgress({ guid, status: 'retry' }));
const status = err.originalResponse ? err.originalResponse.getStatus() : 0; const status = err.originalResponse ? err.originalResponse.getStatus() : 0;
return !inStatusCategory(status, 400); return !inStatusCategory(status, 400);
}, },
@ -79,17 +78,17 @@ export function makeResumableUploadRequest(
const errMsg = typeof err === 'string' ? err : err.message; const errMsg = typeof err === 'string' ? err : err.message;
if (status === STATUS_CONFLICT || status === STATUS_LOCKED || errMsg === 'file currently locked') { if (status === STATUS_CONFLICT || status === STATUS_LOCKED || errMsg === 'file currently locked') {
window.store.dispatch(doUpdateUploadProgress({ params, status: 'conflict' })); window.store.dispatch(doUpdateUploadProgress({ guid, status: 'conflict' }));
// prettier-ignore // prettier-ignore
reject(new Error(`${status}: concurrent upload detected. Uploading the same file from multiple tabs or windows is not allowed.`)); reject(new Error(`${status}: concurrent upload detected. Uploading the same file from multiple tabs or windows is not allowed.`));
} else { } else {
window.store.dispatch(doUpdateUploadProgress({ params, status: 'error' })); window.store.dispatch(doUpdateUploadProgress({ guid, status: 'error' }));
reject(new Error(err)); reject(new Error(err));
} }
}, },
onProgress: (bytesUploaded, bytesTotal) => { onProgress: (bytesUploaded, bytesTotal) => {
const percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2); const percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2);
window.store.dispatch(doUpdateUploadProgress({ params, progress: percentage })); window.store.dispatch(doUpdateUploadProgress({ guid, progress: percentage }));
}, },
onSuccess: () => { onSuccess: () => {
let retries = 1; let retries = 1;
@ -102,7 +101,7 @@ export function makeResumableUploadRequest(
xhr.setRequestHeader(X_LBRY_AUTH_TOKEN, token); xhr.setRequestHeader(X_LBRY_AUTH_TOKEN, token);
xhr.responseType = 'json'; xhr.responseType = 'json';
xhr.onload = () => { xhr.onload = () => {
window.store.dispatch(doUpdateUploadRemove(params)); window.store.dispatch(doUpdateUploadRemove(guid));
resolve(xhr); resolve(xhr);
}; };
xhr.onerror = () => { xhr.onerror = () => {
@ -111,12 +110,12 @@ export function makeResumableUploadRequest(
analytics.error('notify: first attempt failed (status=0). Retrying after 10s...'); analytics.error('notify: first attempt failed (status=0). Retrying after 10s...');
setTimeout(() => makeNotifyRequest(), 10000); // Auto-retry after 10s delay. setTimeout(() => makeNotifyRequest(), 10000); // Auto-retry after 10s delay.
} else { } else {
window.store.dispatch(doUpdateUploadProgress({ params, status: 'error' })); window.store.dispatch(doUpdateUploadProgress({ guid, status: 'error' }));
reject(new Error(`There was a problem in the processing. Please retry. (${xhr.status})`)); reject(new Error(`There was a problem in the processing. Please retry. (${xhr.status})`));
} }
}; };
xhr.onabort = () => { xhr.onabort = () => {
window.store.dispatch(doUpdateUploadRemove(params)); window.store.dispatch(doUpdateUploadRemove(guid));
}; };
xhr.send(jsonPayload); xhr.send(jsonPayload);

View file

@ -1,5 +1,6 @@
// @flow // @flow
import * as tus from 'tus-js-client'; import * as tus from 'tus-js-client';
import { v4 as uuid } from 'uuid';
import { makeUploadRequest } from './publish-v1'; import { makeUploadRequest } from './publish-v1';
import { makeResumableUploadRequest } from './publish-v2'; import { makeResumableUploadRequest } from './publish-v2';
@ -34,6 +35,12 @@ export default function apiPublishCallViaWeb(
params.file_path = '__POST_FILE__'; 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; const useV1 = remoteUrl || preview || !tus.isSupported;
// Note: both function signature (params) should match. // Note: both function signature (params) should match.