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
|
||||
|
||||
Reference in New Issue
Block a user