fix: tus url generation

This commit is contained in:
Oleg Lobanov 2023-07-31 13:18:18 +02:00
parent 605c5cb6eb
commit 42467bc3d8
No known key found for this signature in database
4 changed files with 15 additions and 21 deletions

View File

@ -1,8 +1,7 @@
import { createURL, fetchURL, removePrefix, removeTusEndpoint } from "./utils"; import { createURL, fetchURL, removePrefix } from "./utils";
import { baseURL } from "@/utils/constants"; import { baseURL } from "@/utils/constants";
import store from "@/store"; import store from "@/store";
import { upload as postTus, useTus } from "./tus"; import { upload as postTus, useTus } from "./tus";
import { tusEndpoint } from "../utils/constants";
export async function fetch(url) { export async function fetch(url) {
url = removePrefix(url); url = removePrefix(url);
@ -89,9 +88,6 @@ export async function post(url, content = "", overwrite = false, onupload) {
!["http:", "https:"].includes(window.location.protocol)) || !["http:", "https:"].includes(window.location.protocol)) ||
// Tus is disabled / not applicable // Tus is disabled / not applicable
!(await useTus(content)); !(await useTus(content));
if (!useResourcesApi) {
url = removeTusEndpoint(tusEndpoint, url);
}
return useResourcesApi return useResourcesApi
? postResources(url, content, overwrite, onupload) ? postResources(url, content, overwrite, onupload)
: postTus(url, content, overwrite, onupload); : postTus(url, content, overwrite, onupload);

View File

@ -1,5 +1,5 @@
import * as tus from "tus-js-client"; import * as tus from "tus-js-client";
import { tusEndpoint, tusSettings } from "@/utils/constants"; import { baseURL, tusEndpoint, tusSettings } from "@/utils/constants";
import store from "@/store"; import store from "@/store";
import { removePrefix } from "@/api/utils"; import { removePrefix } from "@/api/utils";
import { fetchURL } from "./utils"; import { fetchURL } from "./utils";
@ -7,20 +7,25 @@ import { fetchURL } from "./utils";
const RETRY_BASE_DELAY = 1000; const RETRY_BASE_DELAY = 1000;
const RETRY_MAX_DELAY = 20000; const RETRY_MAX_DELAY = 20000;
export async function upload(url, content = "", overwrite = false, onupload) { export async function upload(
filePath,
content = "",
overwrite = false,
onupload
) {
if (!tusSettings) { if (!tusSettings) {
// Shouldn't happen as we check for tus support before calling this function // Shouldn't happen as we check for tus support before calling this function
throw new Error("Tus.io settings are not defined"); throw new Error("Tus.io settings are not defined");
} }
url = removePrefix(url); filePath = removePrefix(filePath);
let resourceUrl = `${tusEndpoint}${url}?override=${overwrite}`; let resourcePath = `${tusEndpoint}${filePath}?override=${overwrite}`;
await createUpload(resourceUrl); await createUpload(resourcePath);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let upload = new tus.Upload(content, { let upload = new tus.Upload(content, {
uploadUrl: resourceUrl, uploadUrl: `${baseURL}${resourcePath}`,
chunkSize: tusSettings.chunkSize, chunkSize: tusSettings.chunkSize,
retryDelays: computeRetryDelays(tusSettings), retryDelays: computeRetryDelays(tusSettings),
parallelUploads: 1, parallelUploads: 1,
@ -46,8 +51,8 @@ export async function upload(url, content = "", overwrite = false, onupload) {
}); });
} }
async function createUpload(resourceUrl) { async function createUpload(resourcePath) {
let headResp = await fetchURL(resourceUrl, { let headResp = await fetchURL(resourcePath, {
method: "POST", method: "POST",
}); });
if (headResp.status !== 201) { if (headResp.status !== 201) {

View File

@ -78,10 +78,3 @@ export function createURL(endpoint, params = {}, auth = true) {
return url.toString(); return url.toString();
} }
export function removeTusEndpoint(tusEndpoint, url) {
if (url.startsWith(tusEndpoint)) {
return url.substring(tusEndpoint.length);
}
return url;
}

View File

@ -17,7 +17,7 @@ const resizePreview = window.FileBrowser.ResizePreview;
const enableExec = window.FileBrowser.EnableExec; const enableExec = window.FileBrowser.EnableExec;
const tusSettings = window.FileBrowser.TusSettings; const tusSettings = window.FileBrowser.TusSettings;
const origin = window.location.origin; const origin = window.location.origin;
const tusEndpoint = `${baseURL}/api/tus`; const tusEndpoint = `/api/tus`;
export { export {
name, name,