Skip to content

Commit

Permalink
implement uploadstreamdescriptor (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernix01 committed Feb 6, 2024
1 parent c8819e8 commit 03cd917
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "medusa-storage-supabase",
"version": "0.1.1",
"version": "0.1.2",
"description": "A plugin to store files in supabase.",
"author": "bernix01 <gbernal096@gmail.com>",
"license": "GPL-3.0-only",
Expand Down
18 changes: 17 additions & 1 deletion src/services/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { StorageClient } from "@supabase/storage-js";
import { randomUUID } from "crypto";
import { createReadStream } from "fs";
import { PassThrough } from "stream";

class SupabaseFileService extends AbstractFileService {
logger: Logger;
Expand Down Expand Up @@ -104,7 +105,22 @@ class SupabaseFileService extends AbstractFileService {
async getUploadStreamDescriptor(
fileData: UploadStreamDescriptorType
): Promise<FileServiceGetUploadStreamResult> {
throw new Error("Method not implemented.");
const pass = new PassThrough();
const key = fileData.isPrivate
? `private/${randomUUID()}.${fileData.ext}`
: `public/${randomUUID()}.${fileData.ext}`;

const promise = this.storageClient.from(this.bucket).upload(key, pass, {
contentType: fileData.contentType as string,
duplex: "half",
});

return {
writeStream: pass,
promise,
url: `${this.storageUrl}/${this.bucket}/${key}`,
fileKey: key,
};
}

async getDownloadStream(
Expand Down

0 comments on commit 03cd917

Please sign in to comment.