From d602409122159f8081aaf87c0e5d88f9db18d25f Mon Sep 17 00:00:00 2001 From: Joseph Werle Date: Wed, 8 Nov 2023 14:45:20 -0500 Subject: [PATCH] refactor(api/internal): initial 'showOpenFilePicker' and structure --- api/index.d.ts | 2 +- api/internal/pickers.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 api/internal/pickers.js diff --git a/api/index.d.ts b/api/index.d.ts index f31c474fd3..ed77a365ad 100644 --- a/api/index.d.ts +++ b/api/index.d.ts @@ -7249,7 +7249,7 @@ declare module "socket:internal/pickers" { /** * TODO */ - export function showOpenFilePicker(options?: any): Promise; + export function showOpenFilePicker(options?: any): Promise; namespace _default { export { showOpenFilePicker }; } diff --git a/api/internal/pickers.js b/api/internal/pickers.js new file mode 100644 index 0000000000..eb184cf0f0 --- /dev/null +++ b/api/internal/pickers.js @@ -0,0 +1,36 @@ +import application from '../application.js' +import mime from '../mime.js' +import path from '../path.js' +import fs from '../fs/promises.js' + +/** + * TODO + */ +export async function showOpenFilePicker (options = null) { + const currentWindow = await application.getCurrentWindow() + const handles = [] + + const results = await currentWindow.showOpenFilePicker({ + allowMultiple: options?.multiple === true, + allowFiles: true + }) + + for (const result of results) { + const stats = await fs.stat(result) + const types = await mime.lookup(path.extname(result).slice(1)) + const file = new File([], result, { + lastModified: stats.mtimeMs, + type: types[0]?.mime ?? '' + }) + + handles.push(Object.create(FileSystemFileHandle.prototype, { + getFile: { value () { return file } } + })) + } + + return handles +} + +export default { + showOpenFilePicker +}