Skip to content

Commit

Permalink
Revert "v4 URL Mode (#2919)"
Browse files Browse the repository at this point in the history
Reverts 3b442bc

The TUS address returned is incorrect.
  • Loading branch information
infinite-persistence committed Aug 14, 2023
1 parent 3b442bc commit b1b0a0a
Show file tree
Hide file tree
Showing 8 changed files with 503 additions and 143 deletions.
1 change: 1 addition & 0 deletions flow-typed/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ declare type FileUploadSdkParams = {
uploadUrl?: string,
publishId?: number, // ID to query `stream_create | stream_update` progress
sdkRan?: boolean, // Legacy v3 flag (deprecated)
isMarkdown: boolean,
channel_id: ?string,
};

Expand Down
2 changes: 1 addition & 1 deletion ui/component/publish/shared/publishName/name-help-text.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function NameHelpText(props: Props) {

const currentUploadNames: Array<string> = React.useMemo(() => {
// $FlowFixMe - unable to resolve mixed
return Object.values(currentUploads).map((x) => (x.params && !x.params.preview ? x.params.name : ''));
return Object.values(currentUploads).map((x) => (x.params ? x.params.name : ''));
}, [currentUploads]);

let nameHelpText;
Expand Down
48 changes: 48 additions & 0 deletions web/setup/publish-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// @flow
import * as tus from 'tus-js-client';
import { COMMIT_ID } from 'config';
import { platform } from 'util/platform';

function generateCause(
v1Type: ?string,
xhr: ?any,
uploader: ?TusUploader = null,
originalMsg: ?(string | Error) = null,
misc: ?{} = {}
) {
return {
cause: {
app: `${COMMIT_ID ? COMMIT_ID.slice(0, 8) : '?'} | ${platform.os()} | ${platform.browser()}`,
...(xhr ? { xhrResponseURL: xhr.responseURL } : {}),
...(xhr ? { xhrStatus: `${xhr.status} (${xhr.statusText})` } : {}),
...(v1Type ? { type: v1Type } : {}),
...(uploader?._fingerprint ? { fingerprint: uploader?._fingerprint } : {}),
...(uploader?._retryAttempt ? { retryAttempt: uploader?._retryAttempt } : {}),
...(!tus.isSupported ? { isTusSupported: tus.isSupported } : {}),
...(originalMsg ? { original: typeof originalMsg === 'string' ? originalMsg : originalMsg.message } : {}),
...(misc || {}),
},
};
}

export function generateError(
errMsg: string,
params: FileUploadSdkParams,
xhr: ?any,
tusUploader: ?TusUploader = null,
originalErrMsg: ?(string | Error) = null
) {
const { uploadUrl, guid, isMarkdown, ...sdkParams } = params;
const { preview: isPreview, remote_url: remoteUrl } = sdkParams;

const useV1 = remoteUrl || isMarkdown || isPreview || !tus.isSupported; // TODO: DRY
const v1Type = !useV1 ? null : isPreview ? 'preview' : isMarkdown ? 'markdown' : remoteUrl ? 'replay' : '?';
let misc;

if (errMsg && typeof errMsg === 'string' && errMsg.startsWith('Stream')) {
misc = { ...sdkParams };
}

// $FlowIgnore - flow's constructor for Error is incorrect.
return new Error(errMsg, generateCause(v1Type, xhr, tusUploader, originalErrMsg, misc));
}
92 changes: 92 additions & 0 deletions web/setup/publish-v1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// @flow

// https://api.na-backend.odysee.com/api/v1/proxy currently expects publish to
// consist of a multipart/form-data POST request with:
// - 'file' binary
// - 'json_payload' publish params to be passed to the server's sdk.

import { generateError } from './publish-error';
import analytics from '../../ui/analytics';
import { PUBLISH_TIMEOUT_BUT_LIKELY_SUCCESSFUL } from '../../ui/constants/errors';
import { X_LBRY_AUTH_TOKEN } from '../../ui/constants/token';
import { doUpdateUploadAdd, doUpdateUploadProgress, doUpdateUploadRemove } from '../../ui/redux/actions/publish';
import { LBRY_WEB_PUBLISH_API } from 'config';

const ENDPOINT = LBRY_WEB_PUBLISH_API;
const ENDPOINT_METHOD = 'publish';

const PUBLISH_FETCH_TIMEOUT_MS = 60000;
const PREVIEW_FETCH_TIMEOUT_MS = 10000;

export function makeUploadRequest(
token: string,
params: FileUploadSdkParams,
file: File | string,
isPreview?: boolean
) {
const originalParams = { ...params };
const { remote_url: remoteUrl } = params;

const body = new FormData();

if (file) {
body.append('file', file);
delete params['remote_url'];
} else if (remoteUrl) {
body.append('remote_url', remoteUrl);
delete params['remote_url'];
}

const { uploadUrl, guid, isMarkdown, ...sdkParams } = params;

const jsonPayload = JSON.stringify({
jsonrpc: '2.0',
method: ENDPOINT_METHOD,
params: sdkParams,
id: new Date().getTime(),
});

// no fileData? do the livestream remote publish
body.append('json_payload', jsonPayload);

return new Promise<any>((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open('POST', ENDPOINT);
xhr.setRequestHeader(X_LBRY_AUTH_TOKEN, token);
if (!remoteUrl) {
xhr.timeout = isPreview ? PREVIEW_FETCH_TIMEOUT_MS : PUBLISH_FETCH_TIMEOUT_MS;
}
xhr.responseType = 'json';
xhr.upload.onprogress = (e) => {
const percentage = ((e.loaded / e.total) * 100).toFixed(2);
window.store.dispatch(doUpdateUploadProgress({ guid, progress: percentage }));
};
xhr.onload = () => {
window.store.dispatch(doUpdateUploadRemove(guid));
resolve(xhr);
};
xhr.onerror = () => {
window.store.dispatch(doUpdateUploadProgress({ guid, status: 'error' }));
reject(generateError(__('There was a problem with your upload. Please try again.'), originalParams, xhr));
};
xhr.ontimeout = () => {
if (isPreview) {
analytics.error(`publish-v1: preview timed out after ${PREVIEW_FETCH_TIMEOUT_MS / 1000}s`);
resolve(null);
} else {
analytics.error(`publish-v1: timed out after ${PUBLISH_FETCH_TIMEOUT_MS / 1000}s`);
window.store.dispatch(doUpdateUploadProgress({ guid, status: 'error' }));
reject(generateError(PUBLISH_TIMEOUT_BUT_LIKELY_SUCCESSFUL, params, xhr));
}
};
xhr.onabort = () => {
window.store.dispatch(doUpdateUploadRemove(guid));
};

if (!isPreview) {
window.store.dispatch(doUpdateUploadAdd(file, params, xhr, 'v1'));
}

xhr.send(body);
});
}
Loading

0 comments on commit b1b0a0a

Please sign in to comment.