feat: add needed methods to the file module and providers (#12325)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { ModuleProvider, Modules } from "@medusajs/framework/utils"
|
||||
import { LocalFileService } from "./services/local-file"
|
||||
export { LocalFileService }
|
||||
|
||||
const services = [LocalFileService]
|
||||
|
||||
|
||||
@@ -3,8 +3,10 @@ import {
|
||||
AbstractFileProviderService,
|
||||
MedusaError,
|
||||
} from "@medusajs/framework/utils"
|
||||
import { createReadStream } from "fs"
|
||||
import fs from "fs/promises"
|
||||
import path from "path"
|
||||
import type { Readable } from "stream"
|
||||
|
||||
export class LocalFileService extends AbstractFileProviderService {
|
||||
static identifier = "localfs"
|
||||
@@ -83,6 +85,24 @@ export class LocalFileService extends AbstractFileProviderService {
|
||||
return
|
||||
}
|
||||
|
||||
async getAsStream(file: FileTypes.ProviderGetFileDTO): Promise<Readable> {
|
||||
const baseDir = file.fileKey.startsWith("private-")
|
||||
? this.privateUploadDir_
|
||||
: this.uploadDir_
|
||||
|
||||
const filePath = this.getUploadFilePath(baseDir, file.fileKey)
|
||||
return createReadStream(filePath)
|
||||
}
|
||||
|
||||
async getAsBuffer(file: FileTypes.ProviderGetFileDTO): Promise<Buffer> {
|
||||
const baseDir = file.fileKey.startsWith("private-")
|
||||
? this.privateUploadDir_
|
||||
: this.uploadDir_
|
||||
|
||||
const filePath = this.getUploadFilePath(baseDir, file.fileKey)
|
||||
return fs.readFile(filePath)
|
||||
}
|
||||
|
||||
// The local file provider doesn't support presigned URLs for private files (i.e files not placed in /static).
|
||||
async getPresignedDownloadUrl(
|
||||
file: FileTypes.ProviderGetFileDTO
|
||||
|
||||
@@ -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