Remaining typing fixes, compiles cleanly.

This commit is contained in:
Leo Vasanko 2023-11-12 23:15:11 +00:00
parent c2b9ef6c2f
commit 445ce075d4

View File

@ -11,29 +11,38 @@ const props = defineProps({
path: Array<string> path: Array<string>
}) })
type CloudFile = {
file: File
cloudName: string
cloudPos: number
}
function uploadHandler(event: Event) { function uploadHandler(event: Event) {
event.preventDefault() event.preventDefault()
event.stopPropagation() event.stopPropagation()
// @ts-ignore // @ts-ignore
let infiles = Array.from(event.dataTransfer?.files || event.target.files) as File[] const infiles = Array.from(event.dataTransfer?.files || event.target.files) as File[]
if (!infiles.length) return if (!infiles.length) return
const loc = props.path!.join('/') const loc = props.path!.join('/')
for (const f of infiles) { let files = []
f.cloudName = loc + '/' + (f.webkitRelativePath || f.name) for (const file of infiles) {
f.cloudPos = 0 files.push({
file,
cloudName: loc + '/' + (file.webkitRelativePath || file.name),
cloudPos: 0,
})
} }
const dotfiles = infiles.filter(f => f.cloudName.includes('/.')) const dotfiles = files.filter(f => f.cloudName.includes('/.'))
if (dotfiles.length) { if (dotfiles.length) {
documentStore.error = "Won't upload dotfiles" documentStore.error = "Won't upload dotfiles"
console.log("Dotfiles omitted", dotfiles) console.log("Dotfiles omitted", dotfiles)
infiles = infiles.filter(f => !f.cloudName.includes('/.')) files = files.filter(f => !f.cloudName.includes('/.'))
} }
if (!infiles.length) return if (!files.length) return
infiles.sort((a, b) => collator.compare(a.cloudName, b.cloudName)) files.sort((a, b) => collator.compare(a.cloudName, b.cloudName))
// @ts-ignore // @ts-ignore
upqueue = [...upqueue, ...infiles] upqueue = [...upqueue, ...files]
statsAdd(infiles) statsAdd(files)
startWorker() startWorker()
} }
@ -49,7 +58,7 @@ const uprogress_init = {
tlast: 0, tlast: 0,
statbytes: 0, statbytes: 0,
statdur: 0, statdur: 0,
files: [] as File[], files: [] as CloudFile[],
filestart: 0, filestart: 0,
fileidx: 0, fileidx: 0,
filecount: 0, filecount: 0,
@ -79,7 +88,7 @@ setInterval(() => {
uprogress.statdur *= .9 uprogress.statdur *= .9
} }
}, 100) }, 100)
const statUpdate = ({name, size, start, end}) => { const statUpdate = ({name, size, start, end}: {name: string, size: number, start: number, end: number}) => {
if (name !== uprogress.filename) return // If stats have been reset if (name !== uprogress.filename) return // If stats have been reset
const now = Date.now() const now = Date.now()
uprogress.uploaded = uprogress.filestart + end uprogress.uploaded = uprogress.filestart + end
@ -98,7 +107,7 @@ const statNextFile = () => {
const f = uprogress.files.shift() const f = uprogress.files.shift()
if (!f) return statReset() if (!f) return statReset()
uprogress.filepos = 0 uprogress.filepos = 0
uprogress.filesize = f.size uprogress.filesize = f.file.size
uprogress.filename = f.cloudName uprogress.filename = f.cloudName
} }
const statReset = () => { const statReset = () => {
@ -106,14 +115,14 @@ const statReset = () => {
uprogress.t0 = Date.now() uprogress.t0 = Date.now()
uprogress.tlast = uprogress.t0 + 1 uprogress.tlast = uprogress.t0 + 1
} }
const statsAdd = (f: Array<File>) => { const statsAdd = (f: CloudFile[]) => {
if (uprogress.files.length === 0) statReset() if (uprogress.files.length === 0) statReset()
uprogress.total += f.reduce((a, b) => a + b.size, 0) uprogress.total += f.reduce((a, b) => a + b.file.size, 0)
uprogress.filecount += f.length uprogress.filecount += f.length
uprogress.files = [...uprogress.files, ...f] uprogress.files = [...uprogress.files, ...f]
statNextFile() statNextFile()
} }
let upqueue = [] as File[] let upqueue = [] as CloudFile[]
// TODO: Rewrite as WebSocket class // TODO: Rewrite as WebSocket class
const WSCreate = async () => await new Promise<WebSocket>(resolve => { const WSCreate = async () => await new Promise<WebSocket>(resolve => {
@ -156,17 +165,19 @@ const worker = async () => {
const ws = await WSCreate() const ws = await WSCreate()
while (upqueue.length) { while (upqueue.length) {
const f = upqueue[0] const f = upqueue[0]
if (f.cloudPos === f.size) { if (f.cloudPos === f.file.size) {
upqueue.shift() upqueue.shift()
continue continue
} }
const start = f.cloudPos const start = f.cloudPos
const end = Math.min(f.size, start + (1<<20)) const end = Math.min(f.file.size, start + (1<<20))
const control = { name: f.cloudName, size: f.size, start, end } const control = { name: f.cloudName, size: f.file.size, start, end }
const data = f.slice(start, end) const data = f.file.slice(start, end)
f.cloudPos = end f.cloudPos = end
// Note: files may get modified during I/O // Note: files may get modified during I/O
// @ts-ignore FIXME proper WebSocket class, avoid attaching functions to WebSocket object
ws.sendMsg(control) ws.sendMsg(control)
// @ts-ignore
await ws.sendData(data) await ws.sendData(data)
} }
if (upqueue.length) startWorker() if (upqueue.length) startWorker()