chore: move files to frontend

This commit is contained in:
Henrique Dias
2019-05-21 11:13:59 +01:00
parent d45d7f92fb
commit 7414ca10b3
137 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
import { removePrefix } from './utils'
import { baseURL } from '@/utils/constants'
import store from '@/store'
const ssl = (window.location.protocol === 'https:')
const protocol = (ssl ? 'wss:' : 'ws:')
export default function command(url, command, onmessage, onclose) {
url = removePrefix(url)
url = `${protocol}//${window.location.host}${baseURL}/api/command${url}?auth=${store.state.jwt}`
let conn = new window.WebSocket(url)
conn.onopen = () => conn.send(command)
conn.onmessage = onmessage
conn.onclose = onclose
}

143
frontend/src/api/files.js Normal file
View File

@@ -0,0 +1,143 @@
import { fetchURL, removePrefix } from './utils'
import { baseURL } from '@/utils/constants'
import store from '@/store'
export async function fetch (url) {
url = removePrefix(url)
const res = await fetchURL(`/api/resources${url}`, {})
if (res.status === 200) {
let data = await res.json()
data.url = `/files${url}`
if (data.isDir) {
if (!data.url.endsWith('/')) data.url += '/'
data.items = data.items.map((item, index) => {
item.index = index
item.url = `${data.url}${encodeURIComponent(item.name)}`
if (item.isDir) {
item.url += '/'
}
return item
})
}
return data
} else {
throw new Error(res.status)
}
}
async function resourceAction (url, method, content) {
url = removePrefix(url)
let opts = { method }
if (content) {
opts.body = content
}
const res = await fetchURL(`/api/resources${url}`, opts)
if (res.status !== 200) {
throw new Error(res.responseText)
} else {
return res
}
}
export async function remove (url) {
return resourceAction(url, 'DELETE')
}
export async function put (url, content = '') {
return resourceAction(url, 'PUT', content)
}
export function download (format, ...files) {
let url = `${baseURL}/api/raw`
if (files.length === 1) {
url += removePrefix(files[0]) + '?'
} else {
let arg = ''
for (let file of files) {
arg += removePrefix(file) + ','
}
arg = arg.substring(0, arg.length - 1)
arg = encodeURIComponent(arg)
url += `/?files=${arg}&`
}
if (format !== null) {
url += `algo=${format}&`
}
url += `auth=${store.state.jwt}`
window.open(url)
}
export async function post (url, content = '', overwrite = false, onupload) {
url = removePrefix(url)
return new Promise((resolve, reject) => {
let request = new XMLHttpRequest()
request.open('POST', `${baseURL}/api/resources${url}?override=${overwrite}`, true)
request.setRequestHeader('X-Auth', store.state.jwt)
if (typeof onupload === 'function') {
request.upload.onprogress = onupload
}
// Send a message to user before closing the tab during file upload
window.onbeforeunload = () => "Files are being uploaded."
request.onload = () => {
if (request.status === 200) {
resolve(request.responseText)
} else if (request.status === 409) {
reject(request.status)
} else {
reject(request.responseText)
}
}
request.onerror = (error) => {
reject(error)
}
request.send(content)
// Upload is done no more message before closing the tab
}).finally(() => { window.onbeforeunload = null })
}
function moveCopy (items, copy = false) {
let promises = []
for (let item of items) {
const from = removePrefix(item.from)
const to = encodeURIComponent(removePrefix(item.to))
const url = `${from}?action=${copy ? 'copy' : 'rename'}&destination=${to}`
promises.push(resourceAction(url, 'PATCH'))
}
return Promise.all(promises)
}
export function move (items) {
return moveCopy(items)
}
export function copy (items) {
return moveCopy(items, true)
}
export async function checksum (url, algo) {
const data = await resourceAction(`${url}?checksum=${algo}`, 'GET')
return (await data.json()).checksums[algo]
}

15
frontend/src/api/index.js Normal file
View File

@@ -0,0 +1,15 @@
import * as files from './files'
import * as share from './share'
import * as users from './users'
import * as settings from './settings'
import search from './search'
import commands from './commands'
export {
files,
share,
users,
settings,
commands,
search
}

View File

@@ -0,0 +1,8 @@
import { fetchJSON, removePrefix } from './utils'
export default async function search (url, query) {
url = removePrefix(url)
query = encodeURIComponent(query)
return fetchJSON(`/api/search${url}?query=${query}`, {})
}

View File

@@ -0,0 +1,16 @@
import { fetchURL, fetchJSON } from './utils'
export function get () {
return fetchJSON(`/api/settings`, {})
}
export async function update (settings) {
const res = await fetchURL(`/api/settings`, {
method: 'PUT',
body: JSON.stringify(settings)
})
if (res.status !== 200) {
throw new Error(res.status)
}
}

32
frontend/src/api/share.js Normal file
View File

@@ -0,0 +1,32 @@
import { fetchURL, fetchJSON, removePrefix } from './utils'
export async function getHash(hash) {
return fetchJSON(`/api/public/share/${hash}`)
}
export async function get(url) {
url = removePrefix(url)
return fetchJSON(`/api/share${url}`)
}
export async function remove(hash) {
const res = await fetchURL(`/api/share/${hash}`, {
method: 'DELETE'
})
if (res.status !== 200) {
throw new Error(res.status)
}
}
export async function create(url, expires = '', unit = 'hours') {
url = removePrefix(url)
url = `/api/share${url}`
if (expires !== '') {
url += `?expires=${expires}&unit=${unit}`
}
return fetchJSON(url, {
method: 'POST'
})
}

52
frontend/src/api/users.js Normal file
View File

@@ -0,0 +1,52 @@
import { fetchURL, fetchJSON } from './utils'
export async function getAll () {
return fetchJSON(`/api/users`, {})
}
export async function get (id) {
return fetchJSON(`/api/users/${id}`, {})
}
export async function create (user) {
const res = await fetchURL(`/api/users`, {
method: 'POST',
body: JSON.stringify({
what: 'user',
which: [],
data: user
})
})
if (res.status === 201) {
return res.headers.get('Location')
} else {
throw new Error(res.status)
}
}
export async function update (user, which = ['all']) {
const res = await fetchURL(`/api/users/${user.id}`, {
method: 'PUT',
body: JSON.stringify({
what: 'user',
which: which,
data: user
})
})
if (res.status !== 200) {
throw new Error(res.status)
}
}
export async function remove (id) {
const res = await fetchURL(`/api/users/${id}`, {
method: 'DELETE'
})
if (res.status !== 200) {
throw new Error(res.status)
}
}

45
frontend/src/api/utils.js Normal file
View File

@@ -0,0 +1,45 @@
import store from '@/store'
import { renew } from '@/utils/auth'
import { baseURL } from '@/utils/constants'
export async function fetchURL (url, opts) {
opts = opts || {}
opts.headers = opts.headers || {}
let { headers, ...rest } = opts
const res = await fetch(`${baseURL}${url}`, {
headers: {
'X-Auth': store.state.jwt,
...headers
},
...rest
})
if (res.headers.get('X-Renew-Token') === 'true') {
await renew(store.state.jwt)
}
return res
}
export async function fetchJSON (url, opts) {
const res = await fetchURL(url, opts)
if (res.status === 200) {
return res.json()
} else {
throw new Error(res.status)
}
}
export function removePrefix (url) {
if (url.startsWith('/files')) {
url = url.slice(6)
}
if (url === '') url = '/'
if (url[0] !== '/') url = '/' + url
return url
}