fix: file picker in app not working with mrpack (#6027)
fix: file picker in app
This commit is contained in:
@@ -1,6 +1,17 @@
|
|||||||
import { provideFilePicker } from '@modrinth/ui'
|
import { provideFilePicker } from '@modrinth/ui'
|
||||||
import { convertFileSrc } from '@tauri-apps/api/core'
|
import { convertFileSrc } from '@tauri-apps/api/core'
|
||||||
import { open } from '@tauri-apps/plugin-dialog'
|
import { open } from '@tauri-apps/plugin-dialog'
|
||||||
|
import { readFile } from '@tauri-apps/plugin-fs'
|
||||||
|
|
||||||
|
function getFileName(path: string, fallback: string) {
|
||||||
|
return path.split(/[\\/]/).pop() || fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createFileFromPath(path: string, fallbackName: string, type?: string) {
|
||||||
|
const bytes = await readFile(path)
|
||||||
|
const name = getFileName(path, fallbackName)
|
||||||
|
return new File([bytes], name, type ? { type } : undefined)
|
||||||
|
}
|
||||||
|
|
||||||
export function setupFilePickerProvider() {
|
export function setupFilePickerProvider() {
|
||||||
provideFilePicker({
|
provideFilePicker({
|
||||||
@@ -12,8 +23,7 @@ export function setupFilePickerProvider() {
|
|||||||
if (!result) return null
|
if (!result) return null
|
||||||
const path = result.path ?? result
|
const path = result.path ?? result
|
||||||
if (!path) return null
|
if (!path) return null
|
||||||
const name = path.split(/[\\/]/).pop() || 'icon'
|
const file = await createFileFromPath(path, 'icon')
|
||||||
const file = new File([], name)
|
|
||||||
return { file, path, previewUrl: convertFileSrc(path) }
|
return { file, path, previewUrl: convertFileSrc(path) }
|
||||||
},
|
},
|
||||||
async pickModpackFile() {
|
async pickModpackFile() {
|
||||||
@@ -24,8 +34,11 @@ export function setupFilePickerProvider() {
|
|||||||
if (!result) return null
|
if (!result) return null
|
||||||
const path = result.path ?? result
|
const path = result.path ?? result
|
||||||
if (!path) return null
|
if (!path) return null
|
||||||
const name = path.split(/[\\/]/).pop() || 'modpack.mrpack'
|
const file = await createFileFromPath(
|
||||||
const file = new File([], name)
|
path,
|
||||||
|
'modpack.mrpack',
|
||||||
|
'application/x-modrinth-modpack+zip',
|
||||||
|
)
|
||||||
return { file, path, previewUrl: '' }
|
return { file, path, previewUrl: '' }
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user