Skip to content

Commit

Permalink
refactor(api/internal): initial 'showOpenFilePicker' and structure
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Nov 8, 2023
1 parent ea36ab3 commit d602409
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7249,7 +7249,7 @@ declare module "socket:internal/pickers" {
/**
* TODO
*/
export function showOpenFilePicker(options?: any): Promise<void>;
export function showOpenFilePicker(options?: any): Promise<any[]>;
namespace _default {
export { showOpenFilePicker };
}
Expand Down
36 changes: 36 additions & 0 deletions api/internal/pickers.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit d602409

Please sign in to comment.