Skip to content

Commit

Permalink
chore: refactor url creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiresviana committed May 2, 2022
1 parent f663237 commit 9734f70
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 68 deletions.
32 changes: 31 additions & 1 deletion frontend/src/api/files.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetchURL, removePrefix } from "./utils";
import { fetchURL, removePrefix, createURL } from "./utils";
import { baseURL } from "@/utils/constants";
import store from "@/store";

Expand Down Expand Up @@ -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;
}
11 changes: 10 additions & 1 deletion frontend/src/api/pub.js
Original file line number Diff line number Diff line change
@@ -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 = "") {
Expand Down Expand Up @@ -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);
}
6 changes: 5 additions & 1 deletion frontend/src/api/share.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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);
}
16 changes: 16 additions & 0 deletions frontend/src/api/utils.js
Original file line number Diff line number Diff line change
@@ -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 || {};
Expand Down Expand Up @@ -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();
}
13 changes: 7 additions & 6 deletions frontend/src/components/files/ListingItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</template>

<script>
import { baseURL, enableThumbs } from "@/utils/constants";
import { enableThumbs } from "@/utils/constants";
import { mapMutations, mapGetters, mapState } from "vuex";
import filesize from "filesize";
import moment from "moment";
Expand All @@ -58,6 +58,7 @@ export default {
"modified",
"index",
"readOnly",
"path",
],
computed: {
...mapState(["user", "selected", "req", "jwt"]),
Expand All @@ -83,12 +84,12 @@ export default {
return true;
},
thumbnailUrl() {
const path = this.url.replace(/^\/files\//, "");
// reload the image when the file is replaced
const key = Date.parse(this.modified);
const file = {
path: this.path,
modified: this.modified,
};
return `${baseURL}/api/preview/thumb/${path}?k=${key}&inline=true`;
return api.getPreviewURL(file, "thumb");
},
isThumbsEnabled() {
return enableThumbs;
Expand Down
7 changes: 3 additions & 4 deletions frontend/src/components/prompts/Share.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<td class="small">
<button
class="action copy-clipboard"
:data-clipboard-text="buildLink(link.hash)"
:data-clipboard-text="buildLink(link)"
:aria-label="$t('buttons.copyToClipboard')"
:title="$t('buttons.copyToClipboard')"
>
Expand Down Expand Up @@ -118,7 +118,6 @@
<script>
import { mapState, mapGetters } from "vuex";
import { share as api } from "@/api";
import { baseURL } from "@/utils/constants";
import moment from "moment";
import Clipboard from "clipboard";
Expand Down Expand Up @@ -213,8 +212,8 @@ export default {
humanTime(time) {
return moment(time * 1000).fromNow();
},
buildLink(hash) {
return `${window.location.origin}${baseURL}/share/${hash}`;
buildLink(share) {
return api.getShareURL(share);
},
sort() {
this.links = this.links.sort((a, b) => {
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const theme = window.FileBrowser.Theme;
const enableThumbs = window.FileBrowser.EnableThumbs;
const resizePreview = window.FileBrowser.ResizePreview;
const enableExec = window.FileBrowser.EnableExec;
const origin = window.location.origin;

export {
name,
Expand All @@ -31,4 +32,5 @@ export {
enableThumbs,
resizePreview,
enableExec,
origin,
};
12 changes: 6 additions & 6 deletions frontend/src/utils/url.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function removeLastDir(url) {
export function removeLastDir(url) {
var arr = url.split("/");
if (arr.pop() === "") {
arr.pop();
Expand All @@ -9,7 +9,7 @@ function removeLastDir(url) {

// this code borrow from mozilla
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent#Examples
function encodeRFC5987ValueChars(str) {
export function encodeRFC5987ValueChars(str) {
return (
encodeURIComponent(str)
// Note that although RFC3986 reserves "!", RFC5987 does not,
Expand All @@ -22,15 +22,15 @@ function encodeRFC5987ValueChars(str) {
);
}

function encodePath(str) {
export function encodePath(str) {
return str
.split("/")
.map((v) => encodeURIComponent(v))
.join("/");
}

export default {
encodeRFC5987ValueChars: encodeRFC5987ValueChars,
removeLastDir: removeLastDir,
encodePath: encodePath,
encodeRFC5987ValueChars,
removeLastDir,
encodePath,
};
19 changes: 4 additions & 15 deletions frontend/src/views/Share.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
</a>
</div>
<div class="share__box__element share__box__center">
<qrcode-vue :value="fullLink" size="200" level="M"></qrcode-vue>
<qrcode-vue :value="link" size="200" level="M"></qrcode-vue>
</div>
</div>
<div
Expand Down Expand Up @@ -173,7 +173,6 @@
<script>
import { mapState, mapMutations, mapGetters } from "vuex";
import { pub as api } from "@/api";
import { baseURL } from "@/utils/constants";
import filesize from "filesize";
import moment from "moment";
Expand Down Expand Up @@ -231,21 +230,10 @@ export default {
return "insert_drive_file";
},
link: function () {
let queryArg = "";
if (this.token !== "") {
queryArg = `?token=${this.token}`;
}
const path = this.$route.path.split("/").splice(2).join("/");
return `${baseURL}/api/public/dl/${path}${queryArg}`;
return api.getDownloadURL(this.req);
},
inlineLink: function () {
let url = new URL(this.fullLink);
url.searchParams.set("inline", "true");
return url.href;
},
fullLink: function () {
return window.location.origin + this.link;
return api.getDownloadURL(this.req, true);
},
humanSize: function () {
if (this.req.isDir) {
Expand Down Expand Up @@ -287,6 +275,7 @@ export default {
try {
let file = await api.fetch(url, this.password);
file.hash = this.hash;
this.token = file.token || "";
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/views/files/Listing.vue
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
v-bind:modified="item.modified"
v-bind:type="item.type"
v-bind:size="item.size"
v-bind:path="item.path"
>
</item>
</div>
Expand All @@ -225,6 +226,7 @@
v-bind:modified="item.modified"
v-bind:type="item.type"
v-bind:size="item.size"
v-bind:path="item.path"
>
</item>
</div>
Expand Down
42 changes: 15 additions & 27 deletions frontend/src/views/files/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
</a>
<a
target="_blank"
:href="downloadUrl + '&inline=true'"
:href="raw"
class="button button--flat"
v-if="!req.isDir"
>
Expand Down Expand Up @@ -145,7 +145,7 @@
<script>
import { mapState } from "vuex";
import { files as api } from "@/api";
import { baseURL, resizePreview } from "@/utils/constants";
import { resizePreview } from "@/utils/constants";
import url from "@/utils/url";
import throttle from "lodash.throttle";
import HeaderBar from "@/components/header/HeaderBar";
Expand Down Expand Up @@ -186,23 +186,14 @@ export default {
return this.nextLink !== "";
},
downloadUrl() {
return `${baseURL}/api/raw${url.encodePath(this.req.path)}?auth=${
this.jwt
}`;
return api.getDownloadURL(this.req);
},
previewUrl() {
// reload the image when the file is replaced
const key = Date.parse(this.req.modified);
raw() {
if (this.req.type === "image" && !this.fullSize) {
return `${baseURL}/api/preview/big${url.encodePath(
this.req.path
)}?k=${key}`;
return api.getPreviewURL(this.req, "big");
}
return `${baseURL}/api/raw${url.encodePath(this.req.path)}?k=${key}`;
},
raw() {
return `${this.previewUrl}&inline=true`;
return api.getDownloadURL(this.req, true);
},
showMore() {
return this.$store.state.show === "more";
Expand Down Expand Up @@ -276,9 +267,7 @@ export default {
}
if (this.req.subtitles) {
this.subtitles = this.req.subtitles.map(
(sub) => `${baseURL}/api/raw${sub}?inline=true`
);
this.subtitles = api.getSubtitlesURL(this.req);
}
let dirs = this.$route.fullPath.split("/");
Expand Down Expand Up @@ -320,15 +309,14 @@ export default {
return;
}
},
prefetchUrl: function (item) {
const key = Date.parse(item.modified);
if (item.type === "image" && !this.fullSize) {
return `${baseURL}/api/preview/big${item.path}?k=${key}&inline=true`;
} else if (item.type === "image") {
return `${baseURL}/api/raw${item.path}?k=${key}&inline=true`;
} else {
prefetchUrl(item) {
if (item.type !== "image") {
return "";
}
return this.fullSize
? api.getDownloadURL(item, true)
: api.getPreviewURL(item, "big");
},
openMore() {
this.$store.commit("showHover", "more");
Expand Down Expand Up @@ -358,7 +346,7 @@ export default {
this.$router.push({ path: uri });
},
download() {
api.download(null, this.$route.path);
window.open(this.downloadUrl);
},
},
};
Expand Down
Loading

0 comments on commit 9734f70

Please sign in to comment.