Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement uploadstreamdescriptor #12

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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