2019-02-22 06:01:59 +01:00
|
|
|
// @flow
|
Support resume-able upload via tus (#186)
* Publish button: use spinner instead of "Publishing..."
Looks better, plus the preview could take a while sometimes.
* Refactor `doPublish`. No functional change
This is to allow `doPublish` to accept a custom payload as an input (for resuming uploads), instead of always resolving it from the redux data.
* Add doPublishResume
* Support resume-able upload via tus
## Issue
38 Handle resumable file upload
## Notes
Since we can't serialize a File object, we'll need to the user to re-select the file to resume.
* Exclude "modified date" for Firefox/Android
## Issue
It appears that the modification date of the Android file changes when selected, so that file was deemed "different" when trying to resume upload.
## Change
Exclude modification date for now. Let's assume a smart user.
* Move 'currentUploads' to 'publish' reducer
`publish` is currently rehydrated, so we can ride on that and don't need to store the `currentUploads` in `localStorage` for persistence. This would allow us to store Markdown Post data too, as `localStorage` has a 5MB limit per app.
We could have also made `webReducer` rehydrate, but in this repo, there is no need to split it to another reducer. It also makes more sense to be part of publish anyway (at least to me).
This change is mostly moving items between files, with the exception of
1. An additional REHYDRATE in the publish reducer to clean up the tusUploader.
2. Not clearing `currentUploads` in CLEAR_PUBLISH.
* Restore v1 code for livestream replay, etc.
v2 (tus) does not handle `remote_url`, so the app still needs v1 for that. Since we'll still have v1 code, use v1 for previews as well.
2021-11-10 19:16:16 +01:00
|
|
|
import * as tus from 'tus-js-client';
|
2021-12-07 15:48:09 +01:00
|
|
|
import { v4 as uuid } from 'uuid';
|
Support resume-able upload via tus (#186)
* Publish button: use spinner instead of "Publishing..."
Looks better, plus the preview could take a while sometimes.
* Refactor `doPublish`. No functional change
This is to allow `doPublish` to accept a custom payload as an input (for resuming uploads), instead of always resolving it from the redux data.
* Add doPublishResume
* Support resume-able upload via tus
## Issue
38 Handle resumable file upload
## Notes
Since we can't serialize a File object, we'll need to the user to re-select the file to resume.
* Exclude "modified date" for Firefox/Android
## Issue
It appears that the modification date of the Android file changes when selected, so that file was deemed "different" when trying to resume upload.
## Change
Exclude modification date for now. Let's assume a smart user.
* Move 'currentUploads' to 'publish' reducer
`publish` is currently rehydrated, so we can ride on that and don't need to store the `currentUploads` in `localStorage` for persistence. This would allow us to store Markdown Post data too, as `localStorage` has a 5MB limit per app.
We could have also made `webReducer` rehydrate, but in this repo, there is no need to split it to another reducer. It also makes more sense to be part of publish anyway (at least to me).
This change is mostly moving items between files, with the exception of
1. An additional REHYDRATE in the publish reducer to clean up the tusUploader.
2. Not clearing `currentUploads` in CLEAR_PUBLISH.
* Restore v1 code for livestream replay, etc.
v2 (tus) does not handle `remote_url`, so the app still needs v1 for that. Since we'll still have v1 code, use v1 for previews as well.
2021-11-10 19:16:16 +01:00
|
|
|
import { makeUploadRequest } from './publish-v1';
|
|
|
|
import { makeResumableUploadRequest } from './publish-v2';
|
2019-10-11 02:37:18 +02:00
|
|
|
|
2019-02-22 06:01:59 +01:00
|
|
|
// A modified version of Lbry.apiCall that allows
|
|
|
|
// to perform calling methods at arbitrary urls
|
|
|
|
// and pass form file fields
|
2019-09-30 22:11:45 +02:00
|
|
|
export default function apiPublishCallViaWeb(
|
2020-04-21 07:41:01 +02:00
|
|
|
apiCall: (any, any, any, any) => any,
|
2019-09-30 22:11:45 +02:00
|
|
|
token: string,
|
2019-02-22 06:01:59 +01:00
|
|
|
method: string,
|
Support resume-able upload via tus (#186)
* Publish button: use spinner instead of "Publishing..."
Looks better, plus the preview could take a while sometimes.
* Refactor `doPublish`. No functional change
This is to allow `doPublish` to accept a custom payload as an input (for resuming uploads), instead of always resolving it from the redux data.
* Add doPublishResume
* Support resume-able upload via tus
## Issue
38 Handle resumable file upload
## Notes
Since we can't serialize a File object, we'll need to the user to re-select the file to resume.
* Exclude "modified date" for Firefox/Android
## Issue
It appears that the modification date of the Android file changes when selected, so that file was deemed "different" when trying to resume upload.
## Change
Exclude modification date for now. Let's assume a smart user.
* Move 'currentUploads' to 'publish' reducer
`publish` is currently rehydrated, so we can ride on that and don't need to store the `currentUploads` in `localStorage` for persistence. This would allow us to store Markdown Post data too, as `localStorage` has a 5MB limit per app.
We could have also made `webReducer` rehydrate, but in this repo, there is no need to split it to another reducer. It also makes more sense to be part of publish anyway (at least to me).
This change is mostly moving items between files, with the exception of
1. An additional REHYDRATE in the publish reducer to clean up the tusUploader.
2. Not clearing `currentUploads` in CLEAR_PUBLISH.
* Restore v1 code for livestream replay, etc.
v2 (tus) does not handle `remote_url`, so the app still needs v1 for that. Since we'll still have v1 code, use v1 for previews as well.
2021-11-10 19:16:16 +01:00
|
|
|
params: FileUploadSdkParams,
|
2019-02-22 06:01:59 +01:00
|
|
|
resolve: Function,
|
|
|
|
reject: Function
|
|
|
|
) {
|
2021-03-26 22:03:52 +01:00
|
|
|
const { file_path: filePath, preview, remote_url: remoteUrl } = params;
|
2019-11-04 16:16:56 +01:00
|
|
|
|
2021-03-26 22:03:52 +01:00
|
|
|
if (!filePath && !remoteUrl) {
|
2019-11-04 16:16:56 +01:00
|
|
|
return apiCall(method, params, resolve, reject);
|
|
|
|
}
|
|
|
|
|
2020-10-01 08:24:52 +02:00
|
|
|
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' });
|
|
|
|
}
|
2019-11-04 16:16:56 +01:00
|
|
|
|
2019-02-22 06:01:59 +01:00
|
|
|
// Putting a dummy value here, the server is going to process the POSTed file
|
|
|
|
// and set the file_path itself
|
2021-03-26 22:03:52 +01:00
|
|
|
if (fileField) {
|
|
|
|
params.file_path = '__POST_FILE__';
|
|
|
|
}
|
2019-11-04 16:16:56 +01:00
|
|
|
|
2021-12-07 15:48:09 +01:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
Support resume-able upload via tus (#186)
* Publish button: use spinner instead of "Publishing..."
Looks better, plus the preview could take a while sometimes.
* Refactor `doPublish`. No functional change
This is to allow `doPublish` to accept a custom payload as an input (for resuming uploads), instead of always resolving it from the redux data.
* Add doPublishResume
* Support resume-able upload via tus
## Issue
38 Handle resumable file upload
## Notes
Since we can't serialize a File object, we'll need to the user to re-select the file to resume.
* Exclude "modified date" for Firefox/Android
## Issue
It appears that the modification date of the Android file changes when selected, so that file was deemed "different" when trying to resume upload.
## Change
Exclude modification date for now. Let's assume a smart user.
* Move 'currentUploads' to 'publish' reducer
`publish` is currently rehydrated, so we can ride on that and don't need to store the `currentUploads` in `localStorage` for persistence. This would allow us to store Markdown Post data too, as `localStorage` has a 5MB limit per app.
We could have also made `webReducer` rehydrate, but in this repo, there is no need to split it to another reducer. It also makes more sense to be part of publish anyway (at least to me).
This change is mostly moving items between files, with the exception of
1. An additional REHYDRATE in the publish reducer to clean up the tusUploader.
2. Not clearing `currentUploads` in CLEAR_PUBLISH.
* Restore v1 code for livestream replay, etc.
v2 (tus) does not handle `remote_url`, so the app still needs v1 for that. Since we'll still have v1 code, use v1 for previews as well.
2021-11-10 19:16:16 +01:00
|
|
|
const useV1 = remoteUrl || preview || !tus.isSupported;
|
2019-10-11 02:37:18 +02:00
|
|
|
|
Support resume-able upload via tus (#186)
* Publish button: use spinner instead of "Publishing..."
Looks better, plus the preview could take a while sometimes.
* Refactor `doPublish`. No functional change
This is to allow `doPublish` to accept a custom payload as an input (for resuming uploads), instead of always resolving it from the redux data.
* Add doPublishResume
* Support resume-able upload via tus
## Issue
38 Handle resumable file upload
## Notes
Since we can't serialize a File object, we'll need to the user to re-select the file to resume.
* Exclude "modified date" for Firefox/Android
## Issue
It appears that the modification date of the Android file changes when selected, so that file was deemed "different" when trying to resume upload.
## Change
Exclude modification date for now. Let's assume a smart user.
* Move 'currentUploads' to 'publish' reducer
`publish` is currently rehydrated, so we can ride on that and don't need to store the `currentUploads` in `localStorage` for persistence. This would allow us to store Markdown Post data too, as `localStorage` has a 5MB limit per app.
We could have also made `webReducer` rehydrate, but in this repo, there is no need to split it to another reducer. It also makes more sense to be part of publish anyway (at least to me).
This change is mostly moving items between files, with the exception of
1. An additional REHYDRATE in the publish reducer to clean up the tusUploader.
2. Not clearing `currentUploads` in CLEAR_PUBLISH.
* Restore v1 code for livestream replay, etc.
v2 (tus) does not handle `remote_url`, so the app still needs v1 for that. Since we'll still have v1 code, use v1 for previews as well.
2021-11-10 19:16:16 +01:00
|
|
|
// Note: both function signature (params) should match.
|
|
|
|
const makeRequest = useV1 ? makeUploadRequest : makeResumableUploadRequest;
|
2019-10-08 02:44:03 +02:00
|
|
|
|
Support resume-able upload via tus (#186)
* Publish button: use spinner instead of "Publishing..."
Looks better, plus the preview could take a while sometimes.
* Refactor `doPublish`. No functional change
This is to allow `doPublish` to accept a custom payload as an input (for resuming uploads), instead of always resolving it from the redux data.
* Add doPublishResume
* Support resume-able upload via tus
## Issue
38 Handle resumable file upload
## Notes
Since we can't serialize a File object, we'll need to the user to re-select the file to resume.
* Exclude "modified date" for Firefox/Android
## Issue
It appears that the modification date of the Android file changes when selected, so that file was deemed "different" when trying to resume upload.
## Change
Exclude modification date for now. Let's assume a smart user.
* Move 'currentUploads' to 'publish' reducer
`publish` is currently rehydrated, so we can ride on that and don't need to store the `currentUploads` in `localStorage` for persistence. This would allow us to store Markdown Post data too, as `localStorage` has a 5MB limit per app.
We could have also made `webReducer` rehydrate, but in this repo, there is no need to split it to another reducer. It also makes more sense to be part of publish anyway (at least to me).
This change is mostly moving items between files, with the exception of
1. An additional REHYDRATE in the publish reducer to clean up the tusUploader.
2. Not clearing `currentUploads` in CLEAR_PUBLISH.
* Restore v1 code for livestream replay, etc.
v2 (tus) does not handle `remote_url`, so the app still needs v1 for that. Since we'll still have v1 code, use v1 for previews as well.
2021-11-10 19:16:16 +01:00
|
|
|
return makeRequest(token, params, fileField, preview)
|
2021-03-26 22:03:52 +01:00
|
|
|
.then((xhr) => {
|
2019-10-08 02:44:03 +02:00
|
|
|
let error;
|
2020-03-13 20:54:44 +01:00
|
|
|
if (xhr && xhr.response) {
|
|
|
|
if (xhr.status >= 200 && xhr.status < 300 && !xhr.response.error) {
|
2019-10-08 02:44:03 +02:00
|
|
|
return resolve(xhr.response.result);
|
2020-03-13 20:54:44 +01:00
|
|
|
} else if (xhr.response.error) {
|
|
|
|
error = new Error(xhr.response.error.message);
|
2019-10-08 02:44:03 +02:00
|
|
|
} else {
|
2019-10-11 02:37:18 +02:00
|
|
|
error = new Error(__('Upload likely timed out. Try a smaller file while we work on this.'));
|
2019-10-08 02:44:03 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-22 06:01:59 +01:00
|
|
|
|
|
|
|
if (error) {
|
2019-10-08 02:44:03 +02:00
|
|
|
return Promise.reject(error);
|
2019-02-22 06:01:59 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
}
|