feat: add needed methods to the file module and providers (#12325)

This commit is contained in:
Harminder Virk
2025-04-30 13:17:31 +05:30
committed by GitHub
parent 3539066146
commit ceb504db2c
8 changed files with 169 additions and 6 deletions

View File

@@ -17,6 +17,7 @@ import {
MedusaError,
} from "@medusajs/framework/utils"
import path from "path"
import { Readable } from "stream"
import { ulid } from "ulid"
type InjectedDependencies = {
@@ -215,4 +216,42 @@ export class S3FileService extends AbstractFileProviderService {
key: fileKey,
}
}
async getAsStream(file: FileTypes.ProviderGetFileDTO): Promise<Readable> {
if (!file?.filename) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`No filename provided`
)
}
const fileKey = `${this.config_.prefix}${file.filename}`
const response = await this.client_.send(
new GetObjectCommand({
Key: fileKey,
Bucket: this.config_.bucket,
})
)
return response.Body! as Readable
}
async getAsBuffer(file: FileTypes.ProviderGetFileDTO): Promise<Buffer> {
if (!file?.filename) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`No filename provided`
)
}
const fileKey = `${this.config_.prefix}${file.filename}`
const response = await this.client_.send(
new GetObjectCommand({
Key: fileKey,
Bucket: this.config_.bucket,
})
)
return Buffer.from(await response.Body!.transformToByteArray())
}
}