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