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:
@@ -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(
|
async getUploadStreamDescriptor(
|
||||||
fileData: UploadStreamDescriptorType & {
|
fileData: UploadStreamDescriptorType & {
|
||||||
usePrivateBucket?: boolean
|
|
||||||
contentType?: string
|
contentType?: string
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
const usePrivateBucket = fileData.usePrivateBucket ?? true
|
const usePrivateBucket = fileData.isPrivate ?? true
|
||||||
|
|
||||||
this.validatePrivateBucketConfiguration_(usePrivateBucket)
|
this.validatePrivateBucketConfiguration_(usePrivateBucket)
|
||||||
|
|
||||||
@@ -149,10 +148,8 @@ class MinioService extends AbstractFileService implements IFileService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDownloadStream(
|
async getDownloadStream(fileData: GetUploadedFileType) {
|
||||||
fileData: GetUploadedFileType & { usePrivateBucket?: boolean }
|
const usePrivateBucket = fileData.isPrivate ?? true
|
||||||
) {
|
|
||||||
const usePrivateBucket = !!fileData.usePrivateBucket
|
|
||||||
this.validatePrivateBucketConfiguration_(usePrivateBucket)
|
this.validatePrivateBucketConfiguration_(usePrivateBucket)
|
||||||
const client = this.getClient(usePrivateBucket)
|
const client = this.getClient(usePrivateBucket)
|
||||||
|
|
||||||
@@ -164,14 +161,17 @@ class MinioService extends AbstractFileService implements IFileService {
|
|||||||
return client.getObject(params).createReadStream()
|
return client.getObject(params).createReadStream()
|
||||||
}
|
}
|
||||||
|
|
||||||
async getPresignedDownloadUrl({ usePrivateBucket = true, ...fileData }) {
|
async getPresignedDownloadUrl({
|
||||||
this.validatePrivateBucketConfiguration_(usePrivateBucket)
|
isPrivate = true,
|
||||||
const client = this.getClient(usePrivateBucket, {
|
...fileData
|
||||||
|
}: GetUploadedFileType) {
|
||||||
|
this.validatePrivateBucketConfiguration_(isPrivate)
|
||||||
|
const client = this.getClient(isPrivate, {
|
||||||
signatureVersion: "v4",
|
signatureVersion: "v4",
|
||||||
})
|
})
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
Bucket: usePrivateBucket ? this.private_bucket_ : this.bucket_,
|
Bucket: isPrivate ? this.private_bucket_ : this.bucket_,
|
||||||
Key: `${fileData.fileKey}`,
|
Key: `${fileData.fileKey}`,
|
||||||
Expires: this.downloadUrlDuration,
|
Expires: this.downloadUrlDuration,
|
||||||
}
|
}
|
||||||
@@ -179,7 +179,7 @@ class MinioService extends AbstractFileService implements IFileService {
|
|||||||
return await client.getSignedUrlPromise("getObject", params)
|
return await client.getSignedUrlPromise("getObject", params)
|
||||||
}
|
}
|
||||||
|
|
||||||
validatePrivateBucketConfiguration_(usePrivateBucket) {
|
validatePrivateBucketConfiguration_(usePrivateBucket: boolean) {
|
||||||
if (
|
if (
|
||||||
usePrivateBucket &&
|
usePrivateBucket &&
|
||||||
(!this.private_access_key_id_ || !this.private_bucket_)
|
(!this.private_access_key_id_ || !this.private_bucket_)
|
||||||
|
|||||||
@@ -109,9 +109,11 @@ class S3Service extends AbstractFileService implements IFileService {
|
|||||||
const client = this.getClient()
|
const client = this.getClient()
|
||||||
const pass = new stream.PassThrough()
|
const pass = new stream.PassThrough()
|
||||||
|
|
||||||
|
const isPrivate = fileData.isPrivate ?? true // default to private
|
||||||
|
|
||||||
const fileKey = `${fileData.name}.${fileData.ext}`
|
const fileKey = `${fileData.name}.${fileData.ext}`
|
||||||
const params: PutObjectRequest = {
|
const params: PutObjectRequest = {
|
||||||
ACL: fileData.acl ?? "private",
|
ACL: isPrivate ? "private" : "public-read",
|
||||||
Bucket: this.bucket_,
|
Bucket: this.bucket_,
|
||||||
Body: pass,
|
Body: pass,
|
||||||
Key: fileKey,
|
Key: fileKey,
|
||||||
|
|||||||
@@ -82,9 +82,13 @@ class DigitalOceanService extends AbstractFileService {
|
|||||||
|
|
||||||
const pass = new stream.PassThrough()
|
const pass = new stream.PassThrough()
|
||||||
|
|
||||||
|
// default to private
|
||||||
|
const isPrivate =
|
||||||
|
typeof fileData.isPrivate === "undefined" ? true : fileData.isPrivate
|
||||||
|
|
||||||
const fileKey = `${fileData.name}.${fileData.ext}`
|
const fileKey = `${fileData.name}.${fileData.ext}`
|
||||||
const params = {
|
const params = {
|
||||||
ACL: fileData.acl ?? "private",
|
ACL: isPrivate ? "private" : "public-read",
|
||||||
Bucket: this.bucket_,
|
Bucket: this.bucket_,
|
||||||
Body: pass,
|
Body: pass,
|
||||||
Key: fileKey,
|
Key: fileKey,
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ export type FileServiceGetUploadStreamResult = {
|
|||||||
|
|
||||||
export type GetUploadedFileType = {
|
export type GetUploadedFileType = {
|
||||||
fileKey: string
|
fileKey: string
|
||||||
|
isPrivate?: boolean
|
||||||
[x: string]: unknown
|
[x: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,7 +27,7 @@ export type DeleteFileType = {
|
|||||||
export type UploadStreamDescriptorType = {
|
export type UploadStreamDescriptorType = {
|
||||||
name: string
|
name: string
|
||||||
ext?: string
|
ext?: string
|
||||||
acl?: string
|
isPrivate?: boolean
|
||||||
[x: string]: unknown
|
[x: string]: unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user