Skip to content

Commit

Permalink
Remove redundant calls to baseURL/url #2581 (#2579)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: 이광오 <maeryo@hanwha.com>
Co-authored-by: Oleg Lobanov <oleg.lobanov@bitvavo.com>
  • Loading branch information
3 people committed Jul 31, 2023
1 parent 2a4a46c commit 4b72bbf
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
1 change: 0 additions & 1 deletion frontend/src/api/files.js
Expand Up @@ -88,7 +88,6 @@ export async function post(url, content = "", overwrite = false, onupload) {
!["http:", "https:"].includes(window.location.protocol)) ||
// Tus is disabled / not applicable
!(await useTus(content));

return useResourcesApi
? postResources(url, content, overwrite, onupload)
: postTus(url, content, overwrite, onupload);
Expand Down
21 changes: 13 additions & 8 deletions frontend/src/api/tus.js
@@ -1,26 +1,31 @@
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 { removePrefix } from "@/api/utils";
import { fetchURL } from "./utils";

const RETRY_BASE_DELAY = 1000;
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) {
// Shouldn't happen as we check for tus support before calling this function
throw new Error("Tus.io settings are not defined");
}

url = removePrefix(url);
let resourceUrl = `${tusEndpoint}${url}?override=${overwrite}`;
filePath = removePrefix(filePath);
let resourcePath = `${tusEndpoint}${filePath}?override=${overwrite}`;

await createUpload(resourceUrl);
await createUpload(resourcePath);

return new Promise((resolve, reject) => {
let upload = new tus.Upload(content, {
uploadUrl: resourceUrl,
uploadUrl: `${baseURL}${resourcePath}`,
chunkSize: tusSettings.chunkSize,
retryDelays: computeRetryDelays(tusSettings),
parallelUploads: 1,
Expand All @@ -46,8 +51,8 @@ export async function upload(url, content = "", overwrite = false, onupload) {
});
}

async function createUpload(resourceUrl) {
let headResp = await fetchURL(resourceUrl, {
async function createUpload(resourcePath) {
let headResp = await fetchURL(resourcePath, {
method: "POST",
});
if (headResp.status !== 201) {
Expand Down
1 change: 0 additions & 1 deletion frontend/src/api/utils.js
Expand Up @@ -8,7 +8,6 @@ export async function fetchURL(url, opts, auth = true) {
opts.headers = opts.headers || {};

let { headers, ...rest } = opts;

let res;
try {
res = await fetch(`${baseURL}${url}`, {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/utils/constants.js
Expand Up @@ -17,7 +17,7 @@ const resizePreview = window.FileBrowser.ResizePreview;
const enableExec = window.FileBrowser.EnableExec;
const tusSettings = window.FileBrowser.TusSettings;
const origin = window.location.origin;
const tusEndpoint = `${baseURL}/api/tus`;
const tusEndpoint = `/api/tus`;

export {
name,
Expand Down

0 comments on commit 4b72bbf

Please sign in to comment.