diff --git a/packages/core/types/src/file/service.ts b/packages/core/types/src/file/service.ts index 64120aa1ae..ccb517e6b8 100644 --- a/packages/core/types/src/file/service.ts +++ b/packages/core/types/src/file/service.ts @@ -10,9 +10,9 @@ export interface IFileModuleService extends IModuleService { /** * This method returns the service of the configured File Module Provider in `medusa-config.ts`. This is useful * if you want to execute custom methods defined in the provider's service or you need direct access to it. - * + * * @returns {IFileProvider} An instance of the File Module Provider's service. - * + * * @example * const s3ProviderService = fileModuleService.getProvider() * // TODO: perform custom actions with the provider @@ -135,17 +135,17 @@ export interface IFileModuleService extends IModuleService { ): Promise /** - * This method is used to retrieve a file by ID, similarly to `retrieve`. It doesn't retrieve multiple files, but it's added to support retrieving files with [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query). + * This method is used to list files. It only supports filtering by ID. * * @param {FilterableFileProps} filters - The filters to apply on the retrieved files. * @param {FindConfig} config - * The configurations determining how the files are retrieved. Its properties, such as `select` or `relations`, accept the * attributes or relations associated with a file. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. - * @returns {Promise} The list of files. In this case, it will have at most one file. + * @returns {Promise} The list of files. * * @example - * const files = await fileModuleService.listFiles({ id: "file_123" }) + * const files = await fileModuleService.listFiles({ id: ["file_123", "file_456"] }) */ listFiles( filters?: FilterableFileProps, @@ -154,14 +154,14 @@ export interface IFileModuleService extends IModuleService { ): Promise /** - * This method is used to retrieve a file by ID, similarly to `retrieve`. It doesn't retrieve multiple files, but it's added to support retrieving files with [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query). + * This method is used to list files and their count. It only supports filtering by ID. * * @param {FilterableFileProps} filters - The filters to apply on the retrieved files. * @param {FindConfig} config - * The configurations determining how the files are retrieved. Its properties, such as `select` or `relations`, accept the * attributes or relations associated with a file. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. - * @returns {Promise<[FileDTO[], number]>} The list of files and their count. In this case, it will have at most one file. + * @returns {Promise<[FileDTO[], number]>} The list of files and their count. * * @example * const [files] = await fileModuleService.listAndCountFiles({ id: "file_123" }) @@ -175,9 +175,9 @@ export interface IFileModuleService extends IModuleService { /** * This method retrieves a file by its ID and returns a stream to download the file. Under the hood, it will use the * file provider that was used to upload the file to retrievethe stream. - * + * * @version 2.8.0 - * + * * @param {string} id - The ID of the file. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @returns {Promise} A readable stream of the file contents. @@ -191,9 +191,9 @@ export interface IFileModuleService extends IModuleService { /** * This method retrieves a file by its ID and returns the file contents as a buffer. Under the hood, it will use the * file provider that was used to upload the file to retrieve the buffer. - * + * * @version 2.8.0 - * + * * @param {string} id - The ID of the file. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module. * @returns {Promise} A buffer of the file contents. diff --git a/packages/modules/file/src/services/file-module-service.ts b/packages/modules/file/src/services/file-module-service.ts index e1a9f01d13..def65a53b6 100644 --- a/packages/modules/file/src/services/file-module-service.ts +++ b/packages/modules/file/src/services/file-module-service.ts @@ -104,28 +104,25 @@ export default class FileModuleService implements FileTypes.IFileModuleService { config?: FindConfig, sharedContext?: Context ): Promise { - const id = Array.isArray(filters?.id) ? filters?.id?.[0] : filters?.id - if (!id) { + if (!filters?.id) { throw new MedusaError( MedusaError.Types.INVALID_DATA, "Listing of files is only supported when filtering by ID." ) } - const res = await this.fileProviderService_.getPresignedDownloadUrl({ - fileKey: id, - }) + const ids = Array.isArray(filters?.id) ? filters?.id : [filters?.id] - if (!res) { - return [] - } + const res = await Promise.all( + ids.map(async (id) => { + const res = await this.fileProviderService_.getPresignedDownloadUrl({ + fileKey: id, + }) + return { id, url: res } + }) + ) - return [ - { - id, - url: res, - }, - ] + return res } async listAndCountFiles( @@ -133,31 +130,25 @@ export default class FileModuleService implements FileTypes.IFileModuleService { config?: FindConfig, sharedContext?: Context ): Promise<[FileDTO[], number]> { - const id = Array.isArray(filters?.id) ? filters?.id?.[0] : filters?.id - if (!id) { + if (!filters?.id) { throw new MedusaError( MedusaError.Types.INVALID_DATA, - "Listing and counting of files is only supported when filtering by ID." + "Listing of files is only supported when filtering by ID." ) } - const res = await this.fileProviderService_.getPresignedDownloadUrl({ - fileKey: id, - }) + const ids = Array.isArray(filters?.id) ? filters?.id : [filters?.id] - if (!res) { - return [[], 0] - } + const res = await Promise.all( + ids.map(async (id) => { + const res = await this.fileProviderService_.getPresignedDownloadUrl({ + fileKey: id, + }) + return { id, url: res } + }) + ) - return [ - [ - { - id, - url: res, - }, - ], - 1, - ] + return [res, res.length] } /**