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

@@ -1,3 +1,4 @@
import { Readable } from "stream"
import { FileAccessPermission } from "./common"
/**
@@ -144,4 +145,14 @@ export interface IFileProvider {
getPresignedUploadUrl?(
fileData: ProviderGetPresignedUploadUrlDTO
): Promise<ProviderFileResultDTO>
/**
* Get the file contents as a readable stream.
*/
getAsStream(fileData: ProviderGetFileDTO): Promise<Readable>
/**
* Get the file contents as a Node.js Buffer
*/
getAsBuffer(fileData: ProviderGetFileDTO): Promise<Buffer>
}

View File

@@ -1,10 +1,17 @@
import { Readable } from "stream"
import { IModuleService } from "../modules-sdk"
import { FileDTO, FilterableFileProps, UploadFileUrlDTO } from "./common"
import { FindConfig } from "../common"
import { Context } from "../shared-context"
import { IFileProvider } from "./provider"
import { CreateFileDTO, GetUploadFileUrlDTO } from "./mutations"
export interface IFileModuleService extends IModuleService {
/**
* Returns a reference to the file provider in use
*/
getProvider(): IFileProvider
/**
* This method uploads files to the designated file storage system.
*
@@ -157,4 +164,22 @@ export interface IFileModuleService extends IModuleService {
config?: FindConfig<FileDTO>,
sharedContext?: Context
): Promise<[FileDTO[], number]>
/**
* Get the file contents as a readable stream.
*
* @example
* const stream = await fileModuleService.getAsStream("file_123")
* writeable.pipe(stream)
*/
getAsStream(id: string, sharedContext?: Context): Promise<Readable>
/**
* Get the file contents as a Node.js Buffer
*
* @example
* const contents = await fileModuleService.getAsBuffer("file_123")
* contents.toString('utf-8')
*/
getAsBuffer(id: string, sharedContext?: Context): Promise<Buffer>
}

View File

@@ -1,3 +1,4 @@
import type { Readable } from "stream"
import { FileTypes, IFileProvider } from "@medusajs/types"
/**
@@ -49,9 +50,9 @@ import { FileTypes, IFileProvider } from "@medusajs/types"
export class AbstractFileProviderService implements IFileProvider {
/**
* Each file provider has a unique ID used to identify it. The provider's ID
* will be stored as `fs_{identifier}_{id}`, where `{id}` is the provider's `id`
* will be stored as `fs_{identifier}_{id}`, where `{id}` is the provider's `id`
* property in the `medusa-config.ts`.
*
*
* @example
* class MyFileProviderService extends AbstractFileProviderService {
* static identifier = "my-file"
@@ -63,11 +64,11 @@ export class AbstractFileProviderService implements IFileProvider {
/**
* This method validates the options of the provider set in `medusa-config.ts`.
* Implementing this method is optional. It's useful if your provider requires custom validation.
*
*
* If the options aren't valid, throw an error.
*
*
* @param options - The provider's options.
*
*
* @example
* class MyFileProviderService extends AbstractFileProviderService {
* static validateOptions(options: Record<any, any>) {
@@ -92,7 +93,7 @@ export class AbstractFileProviderService implements IFileProvider {
/**
* This method uploads a file using your provider's custom logic. In this method, you can upload the file
* into your provider's storage, and return the uploaded file's details.
*
*
* This method will be used when uploading product images, CSV files for imports, or other
* custom file uploads.
*
@@ -174,4 +175,34 @@ export class AbstractFileProviderService implements IFileProvider {
): Promise<string> {
throw Error("getPresignedDownloadUrl must be overridden by the child class")
}
/**
* Get the file contents as a readable stream.
*
* @example
* class MyFileProviderService extends AbstractFileProviderService {
* // ...
* async getAsStream(file: ProviderDeleteFileDTO): Promise<Readable> {
* this.client.getAsStream(file.fileKey)
* }
* }
*/
getAsStream(fileData: FileTypes.ProviderGetFileDTO): Promise<Readable> {
throw Error("getAsStream must be overridden by the child class")
}
/**
* Get the file contents as a Node.js Buffer
*
* @example
* class MyFileProviderService extends AbstractFileProviderService {
* // ...
* async getAsBuffer(file: ProviderDeleteFileDTO): Promise<Buffer> {
* this.client.getAsBuffer(file.fileKey)
* }
* }
*/
getAsBuffer(fileData: FileTypes.ProviderGetFileDTO): Promise<Buffer> {
throw Error("getAsBuffer must be overridden by the child class")
}
}

View File

@@ -1,3 +1,4 @@
import type { Readable } from "stream"
import {
Context,
CreateFileDTO,
@@ -28,6 +29,10 @@ export default class FileModuleService implements FileTypes.IFileModuleService {
return joinerConfig
}
getProvider() {
return this.fileProviderService_
}
createFiles(
data: CreateFileDTO[],
sharedContext?: Context
@@ -154,4 +159,26 @@ export default class FileModuleService implements FileTypes.IFileModuleService {
1,
]
}
/**
* Get the file contents as a readable stream.
*
* @example
* const stream = await fileModuleService.getAsStream("file_123")
* writeable.pipe(stream)
*/
getAsStream(id: string): Promise<Readable> {
return this.fileProviderService_.getAsStream({ fileKey: id })
}
/**
* Get the file contents as a Node.js Buffer
*
* @example
* const contents = await fileModuleService.getAsBuffer("file_123")
* contents.toString('utf-8')
*/
getAsBuffer(id: string): Promise<Buffer> {
return this.fileProviderService_.getAsBuffer({ fileKey: id })
}
}

View File

@@ -1,3 +1,4 @@
import type { Readable } from "stream"
import { Constructor, FileTypes } from "@medusajs/framework/types"
import { MedusaError } from "@medusajs/framework/utils"
import { FileProviderRegistrationPrefix } from "@types"
@@ -68,4 +69,12 @@ export default class FileProviderService {
return this.fileProvider_.getPresignedUploadUrl(fileData)
}
getAsStream(fileData: FileTypes.ProviderGetFileDTO): Promise<Readable> {
return this.fileProvider_.getAsStream(fileData)
}
getAsBuffer(fileData: FileTypes.ProviderGetFileDTO): Promise<Buffer> {
return this.fileProvider_.getAsBuffer(fileData)
}
}

View File

@@ -1,5 +1,6 @@
import { ModuleProvider, Modules } from "@medusajs/framework/utils"
import { LocalFileService } from "./services/local-file"
export { LocalFileService }
const services = [LocalFileService]

View File

@@ -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

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())
}
}