2020-07-07 19:58:07 +00:00
|
|
|
import store from '@/store'
|
|
|
|
import url from '@/utils/url'
|
|
|
|
|
|
|
|
export function checkConflict(files, items) {
|
|
|
|
if (typeof items === 'undefined' || items === null) {
|
|
|
|
items = []
|
|
|
|
}
|
|
|
|
|
2020-07-13 14:20:56 +00:00
|
|
|
let folder_upload = files[0].fullPath !== undefined
|
2020-07-07 19:58:07 +00:00
|
|
|
|
|
|
|
let conflict = false
|
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
let file = files[i]
|
|
|
|
let name = file.name
|
|
|
|
|
|
|
|
if (folder_upload) {
|
|
|
|
let dirs = file.fullPath.split("/")
|
|
|
|
if (dirs.length > 1) {
|
|
|
|
name = dirs[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = items.findIndex(function hasConflict(element) {
|
|
|
|
return (element.name === this)
|
|
|
|
}, name)
|
|
|
|
|
|
|
|
if (res >= 0) {
|
|
|
|
conflict = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return conflict
|
|
|
|
}
|
|
|
|
|
|
|
|
export function scanFiles(dt) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
let reading = 0
|
|
|
|
const contents = []
|
|
|
|
|
|
|
|
if (dt.items !== undefined) {
|
|
|
|
for (let item of dt.items) {
|
|
|
|
if (item.kind === "file" && typeof item.webkitGetAsEntry === "function") {
|
|
|
|
const entry = item.webkitGetAsEntry()
|
|
|
|
readEntry(entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resolve(dt.files)
|
|
|
|
}
|
|
|
|
|
|
|
|
function readEntry(entry, directory = "") {
|
|
|
|
if (entry.isFile) {
|
|
|
|
reading++
|
|
|
|
entry.file(file => {
|
|
|
|
reading--
|
|
|
|
|
|
|
|
file.fullPath = `${directory}${file.name}`
|
|
|
|
contents.push(file)
|
|
|
|
|
|
|
|
if (reading === 0) {
|
|
|
|
resolve(contents)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else if (entry.isDirectory) {
|
|
|
|
const dir = {
|
|
|
|
isDir: true,
|
2020-07-09 15:16:04 +00:00
|
|
|
size: 0,
|
2020-07-13 14:20:56 +00:00
|
|
|
fullPath: `${directory}${entry.name}`
|
2020-07-07 19:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contents.push(dir)
|
|
|
|
|
|
|
|
readReaderContent(entry.createReader(), `${directory}${entry.name}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function readReaderContent(reader, directory) {
|
|
|
|
reading++
|
|
|
|
|
|
|
|
reader.readEntries(function (entries) {
|
|
|
|
reading--
|
|
|
|
if (entries.length > 0) {
|
|
|
|
for (const entry of entries) {
|
|
|
|
readEntry(entry, `${directory}/`)
|
|
|
|
}
|
|
|
|
|
|
|
|
readReaderContent(reader, `${directory}/`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reading === 0) {
|
|
|
|
resolve(contents)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-29 14:04:03 +00:00
|
|
|
export function handleFiles(files, base, overwrite = false) {
|
2020-07-07 19:58:07 +00:00
|
|
|
for (let i = 0; i < files.length; i++) {
|
2020-07-09 15:16:04 +00:00
|
|
|
let id = store.state.upload.id
|
2020-09-29 14:04:03 +00:00
|
|
|
let path = base
|
|
|
|
let file = files[i]
|
2020-07-07 19:58:07 +00:00
|
|
|
|
2020-09-29 14:04:03 +00:00
|
|
|
if (file.fullPath !== undefined) {
|
|
|
|
path += url.encodePath(file.fullPath)
|
|
|
|
} else {
|
|
|
|
path += url.encodeRFC5987ValueChars(file.name)
|
|
|
|
}
|
2020-07-07 19:58:07 +00:00
|
|
|
|
2020-07-09 15:16:04 +00:00
|
|
|
if (file.isDir) {
|
2020-09-29 14:04:03 +00:00
|
|
|
path += '/'
|
2020-07-07 19:58:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 15:16:04 +00:00
|
|
|
const item = {
|
|
|
|
id,
|
2020-09-29 14:04:03 +00:00
|
|
|
path,
|
2020-07-09 15:16:04 +00:00
|
|
|
file,
|
|
|
|
overwrite
|
2020-07-07 19:58:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 15:16:04 +00:00
|
|
|
store.dispatch('upload/upload', item);
|
2020-07-07 19:58:07 +00:00
|
|
|
}
|
|
|
|
}
|