Skip to content

Commit

Permalink
Move upload client
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Dec 5, 2023
1 parent a9ee44b commit db9a1c0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 61 deletions.
2 changes: 1 addition & 1 deletion apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@trigger.dev/nextjs": "^2.2.11",
"@vercel/edge-config": "^0.4.1",
"@vercel/toolbar": "^0.1.6",
"@zip.js/zip.js": "2.7.31",
"@zip.js/zip.js": "2.7.32",
"change-case": "^5.2.0",
"framer-motion": "^10.16.14",
"ms": "^2.1.3",
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/components/tables/vault/upload-zone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import { createFolderAction } from "@/actions/create-folder-action";
import { invalidateCacheAction } from "@/actions/invalidate-cache-action";
import { resumableUpload } from "@/utils/upload";
import { createClient } from "@midday/supabase/client";
import { getCurrentUserTeamQuery } from "@midday/supabase/queries";
import { resumableUpload } from "@midday/supabase/storage";
import {
ContextMenu,
ContextMenuContent,
Expand Down
59 changes: 59 additions & 0 deletions apps/dashboard/src/utils/upload.ts
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();
});
});
}
Binary file modified bun.lockb
Binary file not shown.
59 changes: 0 additions & 59 deletions packages/supabase/src/utils/storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { SupabaseClient } from "@supabase/supabase-js";
import * as tus from "tus-js-client";

export const EMPTY_FOLDER_PLACEHOLDER_FILE_NAME = ".emptyFolderPlaceholder";

Expand Down Expand Up @@ -28,64 +27,6 @@ export async function upload(
throw result.error;
}

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();
});
});
}

type RemoveParams = {
path: string;
bucket: string;
Expand Down

0 comments on commit db9a1c0

Please sign in to comment.