fix(medusa-interfaces, medusa-file-*): flag for streaming to a private bucket (#4771)
* wip: unify file service api * fix: flag type * fix: update Spaces service * fix: S3/Spaces - default to private * fix: MinIO `isPrivate` for `getPresignedDownloadUrl`, add `isPrivate` to `GetUploadedFileType` * chore: changeset
This commit is contained in:
8
.changeset/late-mugs-vanish.md
Normal file
8
.changeset/late-mugs-vanish.md
Normal file
@@ -0,0 +1,8 @@
|
||||
---
|
||||
"medusa-file-spaces": patch
|
||||
"medusa-file-minio": patch
|
||||
"medusa-file-s3": patch
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
fix(medusa-interfaces, medusa-file-\*): add `ìsPrivate` flag to the streaming methods, fix minio default bucket
|
||||
@@ -120,11 +120,10 @@ class MinioService extends AbstractFileService implements IFileService {
|
||||
|
||||
async getUploadStreamDescriptor(
|
||||
fileData: UploadStreamDescriptorType & {
|
||||
usePrivateBucket?: boolean
|
||||
contentType?: string
|
||||
}
|
||||
) {
|
||||
const usePrivateBucket = fileData.usePrivateBucket ?? true
|
||||
const usePrivateBucket = fileData.isPrivate ?? true
|
||||
|
||||
this.validatePrivateBucketConfiguration_(usePrivateBucket)
|
||||
|
||||
@@ -149,10 +148,8 @@ class MinioService extends AbstractFileService implements IFileService {
|
||||
}
|
||||
}
|
||||
|
||||
async getDownloadStream(
|
||||
fileData: GetUploadedFileType & { usePrivateBucket?: boolean }
|
||||
) {
|
||||
const usePrivateBucket = !!fileData.usePrivateBucket
|
||||
async getDownloadStream(fileData: GetUploadedFileType) {
|
||||
const usePrivateBucket = fileData.isPrivate ?? true
|
||||
this.validatePrivateBucketConfiguration_(usePrivateBucket)
|
||||
const client = this.getClient(usePrivateBucket)
|
||||
|
||||
@@ -164,14 +161,17 @@ class MinioService extends AbstractFileService implements IFileService {
|
||||
return client.getObject(params).createReadStream()
|
||||
}
|
||||
|
||||
async getPresignedDownloadUrl({ usePrivateBucket = true, ...fileData }) {
|
||||
this.validatePrivateBucketConfiguration_(usePrivateBucket)
|
||||
const client = this.getClient(usePrivateBucket, {
|
||||
async getPresignedDownloadUrl({
|
||||
isPrivate = true,
|
||||
...fileData
|
||||
}: GetUploadedFileType) {
|
||||
this.validatePrivateBucketConfiguration_(isPrivate)
|
||||
const client = this.getClient(isPrivate, {
|
||||
signatureVersion: "v4",
|
||||
})
|
||||
|
||||
const params = {
|
||||
Bucket: usePrivateBucket ? this.private_bucket_ : this.bucket_,
|
||||
Bucket: isPrivate ? this.private_bucket_ : this.bucket_,
|
||||
Key: `${fileData.fileKey}`,
|
||||
Expires: this.downloadUrlDuration,
|
||||
}
|
||||
@@ -179,7 +179,7 @@ class MinioService extends AbstractFileService implements IFileService {
|
||||
return await client.getSignedUrlPromise("getObject", params)
|
||||
}
|
||||
|
||||
validatePrivateBucketConfiguration_(usePrivateBucket) {
|
||||
validatePrivateBucketConfiguration_(usePrivateBucket: boolean) {
|
||||
if (
|
||||
usePrivateBucket &&
|
||||
(!this.private_access_key_id_ || !this.private_bucket_)
|
||||
|
||||
@@ -109,9 +109,11 @@ class S3Service extends AbstractFileService implements IFileService {
|
||||
const client = this.getClient()
|
||||
const pass = new stream.PassThrough()
|
||||
|
||||
const isPrivate = fileData.isPrivate ?? true // default to private
|
||||
|
||||
const fileKey = `${fileData.name}.${fileData.ext}`
|
||||
const params: PutObjectRequest = {
|
||||
ACL: fileData.acl ?? "private",
|
||||
ACL: isPrivate ? "private" : "public-read",
|
||||
Bucket: this.bucket_,
|
||||
Body: pass,
|
||||
Key: fileKey,
|
||||
|
||||
@@ -82,9 +82,13 @@ class DigitalOceanService extends AbstractFileService {
|
||||
|
||||
const pass = new stream.PassThrough()
|
||||
|
||||
// default to private
|
||||
const isPrivate =
|
||||
typeof fileData.isPrivate === "undefined" ? true : fileData.isPrivate
|
||||
|
||||
const fileKey = `${fileData.name}.${fileData.ext}`
|
||||
const params = {
|
||||
ACL: fileData.acl ?? "private",
|
||||
ACL: isPrivate ? "private" : "public-read",
|
||||
Bucket: this.bucket_,
|
||||
Body: pass,
|
||||
Key: fileKey,
|
||||
|
||||
@@ -15,6 +15,7 @@ export type FileServiceGetUploadStreamResult = {
|
||||
|
||||
export type GetUploadedFileType = {
|
||||
fileKey: string
|
||||
isPrivate?: boolean
|
||||
[x: string]: unknown
|
||||
}
|
||||
|
||||
@@ -26,7 +27,7 @@ export type DeleteFileType = {
|
||||
export type UploadStreamDescriptorType = {
|
||||
name: string
|
||||
ext?: string
|
||||
acl?: string
|
||||
isPrivate?: boolean
|
||||
[x: string]: unknown
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user