| import { stripSpecialCharacters } from "@midday/utils"; |
| import type { SupabaseClient } from "@supabase/supabase-js"; |
| import * as tus from "tus-js-client"; |
|
|
| type ResumableUploadParmas = { |
| file: File; |
| path: string[]; |
| bucket: string; |
| onProgress?: (bytesUploaded: number, bytesTotal: number) => void; |
| }; |
|
|
| export async function resumableUpload( |
| client: SupabaseClient, |
| { file, path, bucket, onProgress }: ResumableUploadParmas, |
| ) { |
| const { |
| data: { session }, |
| } = await client.auth.getSession(); |
|
|
| const filename = stripSpecialCharacters(file.name); |
|
|
| const fullPath = decodeURIComponent([...path, filename].join("/")); |
|
|
| return new Promise((resolve, reject) => { |
| const upload = new tus.Upload(file, { |
| endpoint: `https://${process.env.NEXT_PUBLIC_SUPABASE_ID}.supabase.co/storage/v1/upload/resumable`, |
| retryDelays: [0, 3000, 5000, 10000], |
| headers: { |
| authorization: `Bearer ${session?.access_token}`, |
| |
| "x-upsert": "true", |
| }, |
| uploadDataDuringCreation: true, |
| |
| removeFingerprintOnSuccess: true, |
| metadata: { |
| bucketName: bucket, |
| objectName: fullPath, |
| contentType: file.type, |
| cacheControl: "3600", |
| }, |
| |
| chunkSize: 6 * 1024 * 1024, |
| onError: (error) => { |
| reject(error); |
| }, |
| onProgress, |
| onSuccess: () => { |
| resolve({ |
| ...upload, |
| filename, |
| }); |
| }, |
| }); |
|
|
| |
| return upload.findPreviousUploads().then((previousUploads) => { |
| |
| if (previousUploads.length) { |
| |
| upload.resumeFromPreviousUpload(previousUploads[0]); |
| } |
|
|
| upload.start(); |
| }); |
| }); |
| } |
|
|