2023-10-07 19:12:42 +08:00
|
|
|
/**
|
|
|
|
* 本地文件选择
|
|
|
|
*/
|
|
|
|
export class FilePicker {
|
|
|
|
/**
|
|
|
|
* 接口是否可用
|
|
|
|
* @returns { Boolean }
|
|
|
|
*/
|
|
|
|
static canUse() {
|
|
|
|
return !!(window.showDirectoryPicker && window.showOpenFilePicker)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 选择一个目录
|
|
|
|
* @returns { Promise<FileSystemDirectoryHandle> }
|
|
|
|
*/
|
|
|
|
static chooseDirectory() {
|
|
|
|
if (!this.canUse()) {
|
|
|
|
throw new Error('Not Support showDirectoryPicker')
|
|
|
|
}
|
|
|
|
return window.showDirectoryPicker()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 选择一个文件
|
|
|
|
* @param { Boolean } multiple
|
|
|
|
* @param { { description?: string; accept?: { [key: string]: string[]; } } } types
|
|
|
|
* @returns { Promise<FileSystemFileHandle[]> }
|
|
|
|
*/
|
|
|
|
static chooseFile(multiple, types) {
|
|
|
|
if (!this.canUse()) {
|
|
|
|
throw new Error('Not Support showOpenFilePicker')
|
|
|
|
}
|
|
|
|
const pickerOpts = {
|
|
|
|
multiple,
|
|
|
|
types,
|
|
|
|
excludeAcceptAllOption: true
|
|
|
|
}
|
|
|
|
return window.showOpenFilePicker(pickerOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 判断一个文件是否在某个目录下
|
|
|
|
* @param {FileSystemDirectoryHandle} directoryHandle
|
|
|
|
* @param {FileSystemFileHandle} fileHandle
|
|
|
|
*/
|
|
|
|
static async isFileInDirectory(directoryHandle, fileHandle) {
|
|
|
|
const relativePaths = await directoryHandle.resolve(fileHandle)
|
2023-10-08 17:41:37 +08:00
|
|
|
if (relativePaths === null || relativePaths.length > 1) {
|
2023-10-07 19:12:42 +08:00
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|