From 9734f707f07036a07130acfde39b0d700cc857f8 Mon Sep 17 00:00:00 2001 From: Ramires Viana <59319979+ramiresviana@users.noreply.github.com> Date: Mon, 2 May 2022 13:47:22 +0000 Subject: [PATCH] chore: refactor url creation --- frontend/src/api/files.js | 32 +++++++++++++- frontend/src/api/pub.js | 11 ++++- frontend/src/api/share.js | 6 ++- frontend/src/api/utils.js | 16 +++++++ frontend/src/components/files/ListingItem.vue | 13 +++--- frontend/src/components/prompts/Share.vue | 7 ++- frontend/src/utils/constants.js | 2 + frontend/src/utils/url.js | 12 ++--- frontend/src/views/Share.vue | 19 ++------ frontend/src/views/files/Listing.vue | 2 + frontend/src/views/files/Preview.vue | 44 +++++++------------ frontend/src/views/settings/Shares.vue | 11 ++--- 12 files changed, 106 insertions(+), 69 deletions(-) diff --git a/frontend/src/api/files.js b/frontend/src/api/files.js index 7494e55b..6cae2359 100644 --- a/frontend/src/api/files.js +++ b/frontend/src/api/files.js @@ -1,4 +1,4 @@ -import { fetchURL, removePrefix } from "./utils"; +import { fetchURL, removePrefix, createURL } from "./utils"; import { baseURL } from "@/utils/constants"; import store from "@/store"; @@ -154,3 +154,33 @@ export async function checksum(url, algo) { const data = await resourceAction(`${url}?checksum=${algo}`, "GET"); return (await data.json()).checksums[algo]; } + +export function getDownloadURL(file, inline) { + const params = { + ...(inline && { inline: "true" }), + }; + + return createURL("api/raw" + file.path, params); +} + +export function getPreviewURL(file, size) { + const params = { + inline: "true", + key: Date.parse(file.modified), + }; + + return createURL("api/preview/" + size + file.path, params); +} + +export function getSubtitlesURL(file) { + const params = { + inline: "true", + }; + + const subtitles = []; + for (const sub of file.subtitles) { + subtitles.push(createURL("api/raw" + sub, params)); + } + + return subtitles; +} diff --git a/frontend/src/api/pub.js b/frontend/src/api/pub.js index 58eb1eb6..626571b3 100644 --- a/frontend/src/api/pub.js +++ b/frontend/src/api/pub.js @@ -1,4 +1,4 @@ -import { fetchURL, removePrefix } from "./utils"; +import { fetchURL, removePrefix, createURL } from "./utils"; import { baseURL } from "@/utils/constants"; export async function fetch(url, password = "") { @@ -59,3 +59,12 @@ export function download(format, hash, token, ...files) { window.open(url); } + +export function getDownloadURL(share, inline = false) { + const params = { + ...(inline && { inline: "true" }), + ...(share.token && { token: share.token }), + }; + + return createURL("api/public/dl/" + share.hash + share.path, params, false); +} diff --git a/frontend/src/api/share.js b/frontend/src/api/share.js index 54bbc460..29dfe877 100644 --- a/frontend/src/api/share.js +++ b/frontend/src/api/share.js @@ -1,4 +1,4 @@ -import { fetchURL, fetchJSON, removePrefix } from "./utils"; +import { fetchURL, fetchJSON, removePrefix, createURL } from "./utils"; export async function list() { return fetchJSON("/api/shares"); @@ -34,3 +34,7 @@ export async function create(url, password = "", expires = "", unit = "hours") { body: body, }); } + +export function getShareURL(share) { + return createURL("share/" + share.hash, {}, false); +} diff --git a/frontend/src/api/utils.js b/frontend/src/api/utils.js index 65c6740a..f9fc9023 100644 --- a/frontend/src/api/utils.js +++ b/frontend/src/api/utils.js @@ -1,6 +1,7 @@ import store from "@/store"; import { renew } from "@/utils/auth"; import { baseURL } from "@/utils/constants"; +import { encodePath } from "@/utils/url"; export async function fetchURL(url, opts) { opts = opts || {}; @@ -45,3 +46,18 @@ export function removePrefix(url) { if (url[0] !== "/") url = "/" + url; return url; } + +export function createURL(endpoint, params = {}, auth = true) { + const url = new URL(encodePath(endpoint), origin + baseURL); + + const searchParams = { + ...(auth && { auth: store.state.jwt }), + ...params, + }; + + for (const key in searchParams) { + url.searchParams.set(key, searchParams[key]); + } + + return url.toString(); +} diff --git a/frontend/src/components/files/ListingItem.vue b/frontend/src/components/files/ListingItem.vue index 351fba1f..4495be82 100644 --- a/frontend/src/components/files/ListingItem.vue +++ b/frontend/src/components/files/ListingItem.vue @@ -35,7 +35,7 @@