-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
61 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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 fullPath = `${path}/${file.name}`; | ||
|
||
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}`, | ||
// optionally set upsert to true to overwrite existing files | ||
"x-upsert": "true", | ||
}, | ||
uploadDataDuringCreation: true, | ||
// Important if you want to allow re-uploading the same file https://github.com/tus/tus-js-client/blob/main/docs/api.md#removefingerprintonsuccess | ||
removeFingerprintOnSuccess: true, | ||
metadata: { | ||
bucketName: bucket, | ||
objectName: fullPath, | ||
contentType: file.type, | ||
cacheControl: "3600", | ||
}, | ||
// NOTE: it must be set to 6MB (for now) do not change it | ||
chunkSize: 6 * 1024 * 1024, | ||
onError: (error) => { | ||
reject(error); | ||
}, | ||
onProgress, | ||
onSuccess: () => { | ||
resolve(upload); | ||
}, | ||
}); | ||
|
||
// Check if there are any previous uploads to continue. | ||
return upload.findPreviousUploads().then((previousUploads) => { | ||
// Found previous uploads so we select the first one. | ||
if (previousUploads.length) { | ||
upload.resumeFromPreviousUpload(previousUploads[0]); | ||
} | ||
|
||
upload.start(); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters