lbry-desktop/src/platforms/web/publish.js

61 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-02-22 06:01:59 +01:00
// @flow
2019-09-30 22:11:45 +02:00
import { HEADERS } from 'lbry-redux';
function checkAndParse(response) {
2019-02-22 06:01:59 +01:00
if (response.status >= 200 && response.status < 300) {
return response.json();
}
return response.json().then(json => {
let error;
if (json.error) {
error = new Error(json.error);
} else {
error = new Error('Protocol error with unknown response signature');
}
return Promise.reject(error);
});
}
// 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(
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,
params: { file_path: string },
resolve: Function,
reject: Function
) {
const counter = new Date().getTime();
const fileField = params.file_path;
// Putting a dummy value here, the server is going to process the POSTed file
// and set the file_path itself
params.file_path = '__POST_FILE__';
const jsonPayload = JSON.stringify({
jsonrpc: '2.0',
method,
params,
id: counter,
});
const body = new FormData();
body.append('file', fileField);
body.append('json_payload', jsonPayload);
const options = {
method: 'POST',
2019-09-30 22:11:45 +02:00
headers: { [HEADERS.AUTH_TOKEN]: token },
2019-02-22 06:01:59 +01:00
body,
};
return fetch(connectionString, options)
2019-09-30 22:11:45 +02:00
.then(checkAndParse)
2019-02-22 06:01:59 +01:00
.then(response => {
const error = response.error || (response.result && response.result.error);
if (error) {
return reject(error);
}
return resolve(response.result);
})
.catch(reject);
}