From f5450b06e836f761fd7abd8346571ad052b462d1 Mon Sep 17 00:00:00 2001 From: Graham Steffaniak <42989099+gtsteffaniak@users.noreply.github.com> Date: Wed, 27 Nov 2024 08:30:03 -0500 Subject: [PATCH] Hotfix: Fix url link issues (#246) --- backend/files/file.go | 1 - backend/files/indexingFiles.go | 2 +- backend/files/indexing_test.go | 28 ++++++++++++++++++++++++++++ frontend/src/api/files.js | 13 +++++++------ frontend/src/api/public.js | 8 +++----- frontend/src/api/share.js | 4 ++-- frontend/src/api/utils.js | 19 ------------------- frontend/src/api/utils.test.js | 21 +-------------------- frontend/src/components/Search.vue | 9 +++++++-- frontend/src/utils/url.js | 3 +++ 10 files changed, 52 insertions(+), 56 deletions(-) diff --git a/backend/files/file.go b/backend/files/file.go index 8bd4bfda..edfbf946 100644 --- a/backend/files/file.go +++ b/backend/files/file.go @@ -90,7 +90,6 @@ func FileInfoFaster(opts FileOptions) (ExtendedFileInfo, error) { return response, err } opts.IsDir = isDir - // TODO : whats the best way to save trips to disk here? // disabled using cache because its not clear if this is helping or hurting // check if the file exists in the index diff --git a/backend/files/indexingFiles.go b/backend/files/indexingFiles.go index 1ce331e5..497e3eef 100644 --- a/backend/files/indexingFiles.go +++ b/backend/files/indexingFiles.go @@ -164,7 +164,7 @@ func (si *Index) makeIndexPath(subPath string) string { if strings.HasPrefix(subPath, "./") { subPath = strings.TrimPrefix(subPath, ".") } - if strings.HasPrefix(subPath, ".") || si.Root == subPath { + if si.Root == subPath || subPath == "." { return "/" } // clean path diff --git a/backend/files/indexing_test.go b/backend/files/indexing_test.go index 58051f15..834e19b7 100644 --- a/backend/files/indexing_test.go +++ b/backend/files/indexing_test.go @@ -110,3 +110,31 @@ func TestGetIndex(t *testing.T) { }) } } + +func TestMakeIndexPath(t *testing.T) { + tests := []struct { + name string + subPath string + expected string + }{ + {"Root path returns slash", "/", "/"}, + {"Dot-prefixed returns slash", ".", "/"}, + {"Double-dot prefix ignored", "./", "/"}, + {"Dot prefix followed by text", "./test", "/test"}, + {"Dot prefix followed by text", ".test", "/.test"}, + {"Hidden file at root", "/.test", "/.test"}, + {"Trailing slash removed", "/test/", "/test"}, + {"Subpath without root prefix", "/other/test", "/other/test"}, + {"Complex nested paths", "/nested/path", "/nested/path"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + si := &Index{Root: "/"} + result := si.makeIndexPath(tt.subPath) + if result != tt.expected { + t.Errorf("makeIndexPath(%q) = %q; want %q", tt.name, result, tt.expected) + } + }) + } +} diff --git a/frontend/src/api/files.js b/frontend/src/api/files.js index fcdea8d5..ca3e01e3 100644 --- a/frontend/src/api/files.js +++ b/frontend/src/api/files.js @@ -1,4 +1,4 @@ -import { createURL, fetchURL, adjustedData } from "./utils"; +import { fetchURL, adjustedData } from "./utils"; import { removePrefix, getApiPath } from "@/utils/url.js"; import { state } from "@/store"; import { notify } from "@/notify"; @@ -63,8 +63,8 @@ export function download(format, ...files) { } fileargs = fileargs.substring(0, fileargs.length - 1); } - const apiPath = getApiPath("api/raw",{path: path, files: fileargs, algo: format}); - const url = createURL(`${apiPath}`); + const apiPath = getApiPath("api/raw", { path: path, files: fileargs, algo: format }); + const url = window.origin+apiPath window.open(url); } catch (err) { notify.showError(err.message || "Error downloading files"); @@ -153,13 +153,14 @@ export async function checksum(url, algo) { } export function getDownloadURL(path, inline) { + try { const params = { path: removePrefix(path,"files"), ...(inline && { inline: "true" }), }; const apiPath = getApiPath("api/raw", params); - return createURL(apiPath); + return window.origin+apiPath } catch (err) { notify.showError(err.message || "Error getting download URL"); throw err; @@ -175,7 +176,7 @@ export function getPreviewURL(path, size, modified) { inline: "true", }; const apiPath = getApiPath("api/preview", params); - return createURL(apiPath); + return window.origin+apiPath } catch (err) { notify.showError(err.message || "Error getting preview URL"); throw err; @@ -191,7 +192,7 @@ export function getSubtitlesURL(file) { path: sub }; const apiPath = getApiPath("api/raw", params); - return createURL(apiPath); + return window.origin+apiPath } return subtitles; diff --git a/frontend/src/api/public.js b/frontend/src/api/public.js index 2b4250bb..d7e4aacd 100644 --- a/frontend/src/api/public.js +++ b/frontend/src/api/public.js @@ -1,4 +1,4 @@ -import { createURL, adjustedData } from "./utils"; +import { adjustedData } from "./utils"; import { getApiPath, removePrefix } from "@/utils/url.js"; import { notify } from "@/notify"; @@ -37,8 +37,7 @@ export function download(share, ...files) { "files": fileInfo, }; const apiPath = getApiPath("api/public/dl", params); - const url = createURL(apiPath); - window.open(url); + window.open(window.origin+apiPath) } catch (err) { notify.showError(err.message || "Error downloading files"); throw err; @@ -60,6 +59,5 @@ export async function getPublicUser() { // Generate a download URL export function getDownloadURL(share) { const apiPath = getApiPath("api/public/dl", share); - const url = createURL(apiPath) - return url + return window.origin+apiPath } diff --git a/frontend/src/api/share.js b/frontend/src/api/share.js index 147c4d0f..8108efee 100644 --- a/frontend/src/api/share.js +++ b/frontend/src/api/share.js @@ -1,4 +1,4 @@ -import { fetchURL, fetchJSON, createURL, adjustedData } from "./utils"; +import { fetchURL, fetchJSON, adjustedData } from "./utils"; import { notify } from "@/notify"; import { getApiPath } from "@/utils/url.js"; @@ -41,5 +41,5 @@ export async function create(path, password = "", expires = "", unit = "hours") } export function getShareURL(share) { - return createURL("share/"+share.hash, {}, false); + return window.origin+getApiPath(`share/${share.hash}`); } diff --git a/frontend/src/api/utils.js b/frontend/src/api/utils.js index c397622d..97813696 100644 --- a/frontend/src/api/utils.js +++ b/frontend/src/api/utils.js @@ -1,6 +1,5 @@ import { state } from "@/store"; import { renew, logout } from "@/utils/auth"; -import { baseURL } from "@/utils/constants"; import { notify } from "@/notify"; export async function fetchURL(url, opts, auth = true) { @@ -60,24 +59,6 @@ export async function fetchJSON(url, opts) { } } -export function createURL(endpoint) { - let prefix = baseURL; - - // Ensure prefix ends with a single slash - if (!prefix.endsWith("/")) { - prefix += "/"; - } - - // Remove leading slash from endpoint to avoid duplicate slashes - if (endpoint.startsWith("/")) { - endpoint = endpoint.substring(1); - } - - const url = new URL(prefix + endpoint, window.location.origin); - - return url.toString(); -} - export function adjustedData(data, url) { data.url = url; diff --git a/frontend/src/api/utils.test.js b/frontend/src/api/utils.test.js index 8464c90e..b54b81d2 100644 --- a/frontend/src/api/utils.test.js +++ b/frontend/src/api/utils.test.js @@ -1,5 +1,5 @@ import { describe, it, expect, vi } from 'vitest'; -import { adjustedData, createURL } from './utils.js'; +import { adjustedData } from './utils.js'; describe('adjustedData', () => { it('should append the URL and process directory data correctly', () => { @@ -87,25 +87,6 @@ describe('adjustedData', () => { }); - -describe('createURL', () => { - it('createURL', () => { - const url = "base"; - const expected = "http://localhost:3000/unit-testing/base" - expect(createURL(url)).toEqual(expected); - }); - it('createURL with slash', () => { - const url = "/base"; - const expected = "http://localhost:3000/unit-testing/base" - expect(createURL(url)).toEqual(expected); - }); - it('createURL with slash', () => { - const url = "/base"; - const expected = "http://localhost:3000/unit-testing/base" - expect(createURL(url)).toEqual(expected); - }); -}) - vi.mock('@/utils/constants', () => { return { baseURL: "unit-testing", diff --git a/frontend/src/components/Search.vue b/frontend/src/components/Search.vue index 288ec1af..11a89cd2 100644 --- a/frontend/src/components/Search.vue +++ b/frontend/src/components/Search.vue @@ -36,7 +36,7 @@