From edb9e85efd7664f805fc4e6688419b74b9d725cf Mon Sep 17 00:00:00 2001 From: Ramires Viana <59319979+ramiresviana@users.noreply.github.com> Date: Wed, 3 Mar 2021 17:46:37 +0000 Subject: [PATCH] chore: share view logic responsability --- frontend/src/api/files.js | 10 ++--- frontend/src/api/index.js | 2 + frontend/src/api/pub.js | 37 ++++++++++++++++ frontend/src/api/share.js | 6 --- frontend/src/api/utils.js | 6 +-- frontend/src/components/files/ListingItem.vue | 15 ++++--- frontend/src/components/prompts/Download.vue | 42 +++++++------------ frontend/src/store/getters.js | 1 - frontend/src/store/index.js | 5 +-- frontend/src/store/mutations.js | 16 ++----- frontend/src/views/Share.vue | 35 +++++++++++----- frontend/src/views/files/Listing.vue | 18 +++++++- 12 files changed, 113 insertions(+), 80 deletions(-) create mode 100644 frontend/src/api/pub.js diff --git a/frontend/src/api/files.js b/frontend/src/api/files.js index bada7c7c..89d21a54 100644 --- a/frontend/src/api/files.js +++ b/frontend/src/api/files.js @@ -58,7 +58,7 @@ export async function put (url, content = '') { } export function download (format, ...files) { - let url = store.getters['isSharing'] ? `${baseURL}/api/public/dl/${store.state.hash}` : `${baseURL}/api/raw` + let url = `${baseURL}/api/raw` if (files.length === 1) { url += removePrefix(files[0]) + '?' @@ -74,15 +74,13 @@ export function download (format, ...files) { url += `/?files=${arg}&` } - if (format !== null) { + if (format) { url += `algo=${format}&` } - if (store.state.jwt !== ''){ + + if (store.state.jwt){ url += `auth=${store.state.jwt}&` } - if (store.state.token !== ''){ - url += `token=${store.state.token}` - } window.open(url) } diff --git a/frontend/src/api/index.js b/frontend/src/api/index.js index 11bb49d9..5dd59ae7 100644 --- a/frontend/src/api/index.js +++ b/frontend/src/api/index.js @@ -2,6 +2,7 @@ import * as files from './files' import * as share from './share' import * as users from './users' import * as settings from './settings' +import * as pub from './pub' import search from './search' import commands from './commands' @@ -10,6 +11,7 @@ export { share, users, settings, + pub, commands, search } diff --git a/frontend/src/api/pub.js b/frontend/src/api/pub.js new file mode 100644 index 00000000..5afd0913 --- /dev/null +++ b/frontend/src/api/pub.js @@ -0,0 +1,37 @@ +import { fetchJSON, removePrefix } from './utils' +import { baseURL } from '@/utils/constants' + +export async function fetch(hash, password = "") { + return fetchJSON(`/api/public/share/${hash}`, { + headers: {'X-SHARE-PASSWORD': password}, + }) +} + +export function download(format, hash, token, ...files) { + let url = `${baseURL}/api/public/dl/${hash}` + + const prefix = `/share/${hash}` + if (files.length === 1) { + url += removePrefix(files[0], prefix) + '?' + } else { + let arg = '' + + for (let file of files) { + arg += removePrefix(file, prefix) + ',' + } + + arg = arg.substring(0, arg.length - 1) + arg = encodeURIComponent(arg) + url += `/?files=${arg}&` + } + + if (format) { + url += `algo=${format}&` + } + + if (token) { + url += `token=${token}&` + } + + window.open(url) +} \ No newline at end of file diff --git a/frontend/src/api/share.js b/frontend/src/api/share.js index 4e6c40db..fbeecd70 100644 --- a/frontend/src/api/share.js +++ b/frontend/src/api/share.js @@ -4,12 +4,6 @@ export async function list() { return fetchJSON('/api/shares') } -export async function getHash(hash, password = "") { - return fetchJSON(`/api/public/share/${hash}`, { - headers: {'X-SHARE-PASSWORD': password}, - }) -} - export async function get(url) { url = removePrefix(url) return fetchJSON(`/api/share${url}`) diff --git a/frontend/src/api/utils.js b/frontend/src/api/utils.js index 68337d10..ffe7844c 100644 --- a/frontend/src/api/utils.js +++ b/frontend/src/api/utils.js @@ -33,11 +33,11 @@ export async function fetchJSON (url, opts) { } } -export function removePrefix (url) { +export function removePrefix (url, prefix) { if (url.startsWith('/files')) { url = url.slice(6) - } else if (store.getters['isSharing']) { - url = url.slice(7 + store.state.hash.length) + } else if (prefix) { + url = url.replace(prefix, '') } if (url === '') url = '/' diff --git a/frontend/src/components/files/ListingItem.vue b/frontend/src/components/files/ListingItem.vue index 596ce736..8f4bb8ba 100644 --- a/frontend/src/components/files/ListingItem.vue +++ b/frontend/src/components/files/ListingItem.vue @@ -13,7 +13,7 @@ :aria-label="name" :aria-selected="isSelected">
{{ $t('prompts.downloadMessage') }}
- - - - - - - +