jan/web/utils/file.ts
2024-12-16 20:48:10 +07:00

50 lines
1.0 KiB
TypeScript

import { baseName } from '@janhq/core'
import Uppy from '@uppy/core'
import XHR from '@uppy/xhr-upload'
export type FilePathWithSize = {
path: string
name: string
size: number
}
export interface FileWithPath extends File {
path?: string
}
export const getFileInfoFromFile = async (
files: FileWithPath[]
): Promise<FilePathWithSize[]> => {
const result: FilePathWithSize[] = []
for (const file of files) {
if (file.path && file.path.length > 0) {
const fileName = await baseName(file.path)
result.push({
path: file.path,
name: fileName,
size: file.size,
})
}
}
return result
}
/**
* This function creates an Uppy instance with XHR plugin for file upload to the server.
* @returns Uppy instance
*/
export const uploader = () => {
const uppy = new Uppy().use(XHR, {
endpoint: `${API_BASE_URL}/v1/files`,
method: 'POST',
fieldName: 'file',
formData: true,
limit: 1,
})
uppy.setMeta({
purpose: 'assistants',
})
return uppy
}