feat: add needed methods to the file module and providers (#12325)
This commit is contained in:
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user