-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reverts 3b442bc The TUS address returned is incorrect.
- Loading branch information
1 parent
3b442bc
commit b1b0a0a
Showing
8 changed files
with
503 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
Oops, something went wrong.