mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
getFilesFromDataTansfer - with directory upload.
This commit is contained in:
parent
6bd3352a5f
commit
980b3217f8
@ -33,6 +33,7 @@
|
||||
import { Vue, Component, Ref, Prop, Watch } from 'vue-property-decorator'
|
||||
|
||||
import GuacamoleKeyboard from './utils/guacamole-keyboard'
|
||||
import { getFilesFromDataTansfer } from './utils/file-upload'
|
||||
import { NekoWebRTC } from './internal/webrtc'
|
||||
|
||||
@Component({
|
||||
@ -206,13 +207,13 @@
|
||||
this.onMouseMove(e as MouseEvent)
|
||||
}
|
||||
|
||||
onDrop(e: DragEvent) {
|
||||
async onDrop(e: DragEvent) {
|
||||
if (this.isControling || this.implicitControl) {
|
||||
let dt = e.dataTransfer
|
||||
if (!dt) return
|
||||
|
||||
let files = [...dt.files]
|
||||
if (!files) return
|
||||
const files = await getFilesFromDataTansfer(dt)
|
||||
if (files.length === 0) return
|
||||
|
||||
this.$emit('drop-files', { ...this.getMousePos(e.clientX, e.clientY), files })
|
||||
}
|
||||
|
37
src/component/utils/file-upload.ts
Normal file
37
src/component/utils/file-upload.ts
Normal file
@ -0,0 +1,37 @@
|
||||
export async function getFilesFromDataTansfer(dataTransfer: DataTransfer): Promise<Array<File>> {
|
||||
const files: Array<File> = []
|
||||
|
||||
const traverse = (entry: any): Promise<any> => {
|
||||
return new Promise((resolve) => {
|
||||
if (entry.isFile) {
|
||||
entry.file((file: File) => {
|
||||
files.push(file)
|
||||
resolve(file)
|
||||
})
|
||||
} else if (entry.isDirectory) {
|
||||
const reader = entry.createReader()
|
||||
reader.readEntries((entries: any) => {
|
||||
const promises = entries.map(traverse)
|
||||
Promise.all(promises).then(resolve)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const promises: Array<Promise<any>> = []
|
||||
for (const item of dataTransfer.items) {
|
||||
if ('webkitGetAsEntry' in item) {
|
||||
promises.push(traverse(item.webkitGetAsEntry()))
|
||||
} else if ('getAsEntry' in item) {
|
||||
// @ts-ignore
|
||||
promises.push(traverse(item.getAsEntry()))
|
||||
} else break
|
||||
}
|
||||
|
||||
if (promises.length === 0) {
|
||||
return [...dataTransfer.files]
|
||||
}
|
||||
|
||||
await Promise.all(promises)
|
||||
return files
|
||||
}
|
Loading…
Reference in New Issue
Block a user