2019-02-22 06:01:59 +01:00
|
|
|
// @flow
|
2019-10-11 02:37:18 +02:00
|
|
|
/*
|
|
|
|
https://api.lbry.tv/api/v1/proxy currently expects publish to consist
|
|
|
|
of a multipart/form-data POST request with:
|
|
|
|
- 'file' binary
|
|
|
|
- 'json_payload' collection of publish params to be passed to the server's sdk.
|
|
|
|
*/
|
2019-11-07 20:39:22 +01:00
|
|
|
import { X_LBRY_AUTH_TOKEN } from '../../ui/constants/token';
|
2019-10-11 02:37:18 +02:00
|
|
|
import { doUpdateUploadProgress } from 'lbryinc';
|
|
|
|
|
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-02-22 06:01:59 +01:00
|
|
|
connectionString: string,
|
2019-09-30 22:11:45 +02:00
|
|
|
token: string,
|
2019-02-22 06:01:59 +01:00
|
|
|
method: string,
|
2021-03-26 22:03:52 +01:00
|
|
|
params: { file_path: string, preview: boolean, remote_url?: string }, // new param for remoteUrl
|
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);
|
|
|
|
}
|
|
|
|
|
2019-02-22 06:01:59 +01:00
|
|
|
const counter = new Date().getTime();
|
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
|
|
|
|
|
|
|
const body = new FormData();
|
|
|
|
if (fileField) {
|
|
|
|
body.append('file', fileField);
|
|
|
|
params.file_path = '__POST_FILE__';
|
|
|
|
} else if (remoteUrl) {
|
|
|
|
body.append('remote_url', remoteUrl);
|
|
|
|
}
|
2019-11-04 16:16:56 +01:00
|
|
|
|
2019-02-22 06:01:59 +01:00
|
|
|
const jsonPayload = JSON.stringify({
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
method,
|
|
|
|
params,
|
|
|
|
id: counter,
|
|
|
|
});
|
2021-03-26 22:03:52 +01:00
|
|
|
// no fileData? do the livestream remote publish
|
2019-02-22 06:01:59 +01:00
|
|
|
body.append('json_payload', jsonPayload);
|
|
|
|
|
2019-10-11 02:37:18 +02:00
|
|
|
function makeRequest(connectionString, method, token, body, params) {
|
2019-10-08 02:44:03 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
xhr.open(method, connectionString);
|
2019-10-11 02:37:18 +02:00
|
|
|
xhr.setRequestHeader(X_LBRY_AUTH_TOKEN, token);
|
2019-10-08 02:44:03 +02:00
|
|
|
xhr.responseType = 'json';
|
2021-03-26 22:03:52 +01:00
|
|
|
xhr.upload.onprogress = (e) => {
|
2019-10-08 02:44:03 +02:00
|
|
|
let percentComplete = Math.ceil((e.loaded / e.total) * 100);
|
2019-10-11 02:37:18 +02:00
|
|
|
window.store.dispatch(doUpdateUploadProgress(percentComplete, params, xhr));
|
2019-10-08 02:44:03 +02:00
|
|
|
};
|
|
|
|
xhr.onload = () => {
|
2019-10-11 02:37:18 +02:00
|
|
|
window.store.dispatch(doUpdateUploadProgress(undefined, params));
|
2019-10-08 02:44:03 +02:00
|
|
|
resolve(xhr);
|
|
|
|
};
|
|
|
|
xhr.onerror = () => {
|
2019-10-11 02:37:18 +02:00
|
|
|
window.store.dispatch(doUpdateUploadProgress(undefined, params));
|
2021-01-08 22:38:01 +01:00
|
|
|
reject(new Error(__('There was a problem with your upload. Please try again.')));
|
2019-10-11 02:37:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onabort = () => {
|
|
|
|
window.store.dispatch(doUpdateUploadProgress(undefined, params));
|
2019-10-08 02:44:03 +02:00
|
|
|
};
|
|
|
|
xhr.send(body);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-11 02:37:18 +02:00
|
|
|
return makeRequest(connectionString, 'POST', token, body, params)
|
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);
|
|
|
|
}
|