Archived
2
0

lint fix.

This commit is contained in:
Miroslav Šedivý 2022-11-19 16:53:13 +01:00
parent fac8700192
commit 3703d2b73d

View File

@ -231,7 +231,7 @@
import Markdown from './markdown' import Markdown from './markdown'
import Content from './context.vue' import Content from './context.vue'
import { FileTransfer } from '~/neko/types' import { FileTransfer, FileListItem } from '~/neko/types'
@Component({ @Component({
name: 'neko-files', name: 'neko-files',
@ -267,12 +267,15 @@
this.$accessor.files.refresh() this.$accessor.files.refresh()
} }
download(item: any) { download(item: FileListItem) {
if (this.downloads.map((t) => t.name).includes(item.name)) { if (this.downloads.map((t) => t.name).includes(item.name)) {
return return
} }
const url = `/file?pwd=${this.$accessor.password}&filename=${item.name}` const url =
'/file?pwd=' + encodeURIComponent(this.$accessor.password) + '&filename=' + encodeURIComponent(item.name)
const abortController = new AbortController()
let transfer: FileTransfer = { let transfer: FileTransfer = {
id: Math.round(Math.random() * 10000), id: Math.round(Math.random() * 10000),
name: item.name, name: item.name,
@ -283,13 +286,13 @@
progress: 0, progress: 0,
status: 'pending', status: 'pending',
axios: null, axios: null,
abortController: null, abortController: abortController,
} }
transfer.abortController = new AbortController()
transfer.axios = this.$http transfer.axios = this.$http
.get(url, { .get(url, {
responseType: 'blob', responseType: 'blob',
signal: transfer.abortController.signal, signal: abortController.signal,
onDownloadProgress: (x) => { onDownloadProgress: (x) => {
transfer.progress = x.loaded transfer.progress = x.loaded
@ -318,17 +321,20 @@
.catch((err) => { .catch((err) => {
this.$log.error(err) this.$log.error(err)
}) })
this.$accessor.files.addTransfer(transfer) this.$accessor.files.addTransfer(transfer)
} }
upload(dt: DataTransfer) { upload(dt: DataTransfer) {
const url = '/file?pwd=' + encodeURIComponent(this.$accessor.password)
this.uploadAreaDrag = false this.uploadAreaDrag = false
for (const file of dt.files) { for (const file of dt.files) {
const abortController = new AbortController()
const formdata = new FormData() const formdata = new FormData()
formdata.append('files', file, file.name) formdata.append('files', file, file.name)
const url = `/file?pwd=${this.$accessor.password}`
let transfer: FileTransfer = { let transfer: FileTransfer = {
id: Math.round(Math.random() * 10000), id: Math.round(Math.random() * 10000),
name: file.name, name: file.name,
@ -337,11 +343,12 @@
progress: 0, progress: 0,
status: 'pending', status: 'pending',
axios: null, axios: null,
abortController: null, abortController: abortController,
} }
transfer.abortController = new AbortController()
this.$http this.$http
.post(url, formdata, { .post(url, formdata, {
signal: abortController.signal,
onUploadProgress: (x: any) => { onUploadProgress: (x: any) => {
transfer.progress = x.loaded transfer.progress = x.loaded
@ -358,6 +365,7 @@
.catch((err) => { .catch((err) => {
this.$log.error(err) this.$log.error(err)
}) })
this.$accessor.files.addTransfer(transfer) this.$accessor.files.addTransfer(transfer)
} }
} }
@ -366,19 +374,20 @@
const input = document.createElement('input') const input = document.createElement('input')
input.type = 'file' input.type = 'file'
input.setAttribute('multiple', 'true') input.setAttribute('multiple', 'true')
input.click() input.onchange = (e: Event) => {
if (e === null) return
input.onchange = (e) => {
if (e === null) {
return
}
const dt = new DataTransfer() const dt = new DataTransfer()
const target = e.target as any const target = e.target as HTMLInputElement
if (target.files === null) return
for (const f of target.files) { for (const f of target.files) {
dt.items.add(f) dt.items.add(f)
} }
this.upload(dt) this.upload(dt)
} }
input.click()
} }
removeTransfer(transfer: FileTransfer) { removeTransfer(transfer: FileTransfer) {
@ -388,19 +397,34 @@
this.$accessor.files.removeTransfer(transfer) this.$accessor.files.removeTransfer(transfer)
} }
fileIcon(file: any) { fileIcon(file: FileListItem) {
let className = 'file-icon fas ' let className = 'file-icon fas '
// if is directory
if (file.type === 'dir') { if (file.type === 'dir') {
className += 'fa-folder' className += 'fa-folder'
return className return className
} }
const parts = file.name.split('.') // try to get file extension
if (!parts) { const ext = file.name.split('.').pop()
if (ext === undefined) {
className += 'fa-file' className += 'fa-file'
return className return className
} }
const ext = parts[parts.length - 1] // try to find icon
switch (ext) { switch (ext.toLowerCase()) {
case 'txt':
case 'md':
className += 'fa-file-text'
break
case 'pdf':
className += 'fa-file-pdf'
break
case 'zip':
case 'rar':
case '7z':
case 'gz':
className += 'fa-archive'
break
case 'aac': case 'aac':
case 'flac': case 'flac':
case 'midi': case 'midi':
@ -409,6 +433,7 @@
case 'wav': case 'wav':
className += 'fa-music' className += 'fa-music'
break break
case 'avi':
case 'mkv': case 'mkv':
case 'mov': case 'mov':
case 'mpeg': case 'mpeg':
@ -433,19 +458,19 @@
} }
fileSize(size: number) { fileSize(size: number) {
if (size < 1000) { if (size < 1024) {
return `${size} b` return size + ' B'
} }
if (size < 1000 ** 2) { if (size < 1024 * 1024) {
return `${(size / 1000).toFixed(2)} kb` return Math.round(size / 1024) + ' KB'
} }
if (size < 1000 ** 3) { if (size < 1024 * 1024 * 1024) {
return `${(size / 1000 ** 2).toFixed(2)} mb` return Math.round(size / (1024 * 1024)) + ' MB'
} }
if (size < 1000 ** 4) { if (size < 1024 * 1024 * 1024 * 1024) {
return `${(size / 1000 ** 3).toFixed(2)} gb` return Math.round(size / (1024 * 1024 * 1024)) + ' GB'
} }
return `${(size / 1000 ** 4).toFixed(3)} tb` return Math.round(size / (1024 * 1024 * 1024 * 1024)) + ' TB'
} }
} }
</script> </script>