chore: share view logic responsability

This commit is contained in:
Ramires Viana
2021-03-03 17:46:37 +00:00
parent d8306559fd
commit edb9e85efd
12 changed files with 113 additions and 80 deletions

View File

@@ -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)
}

View File

@@ -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
}

37
frontend/src/api/pub.js Normal file
View File

@@ -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)
}

View File

@@ -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}`)

View File

@@ -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 = '/'