fix: Correctly list files for all ids that are passed (#12575)

This commit is contained in:
Stevche Radevski
2025-05-22 14:52:05 +02:00
committed by GitHub
parent 3c6aa979a6
commit 98798f305c
2 changed files with 34 additions and 43 deletions
+11 -11
View File
@@ -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 * 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. * 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. * @returns {IFileProvider} An instance of the File Module Provider's service.
* *
* @example * @example
* const s3ProviderService = fileModuleService.getProvider() * const s3ProviderService = fileModuleService.getProvider()
* // TODO: perform custom actions with the provider * // TODO: perform custom actions with the provider
@@ -135,17 +135,17 @@ export interface IFileModuleService extends IModuleService {
): Promise<FileDTO> ): Promise<FileDTO>
/** /**
* 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 {FilterableFileProps} filters - The filters to apply on the retrieved files.
* @param {FindConfig<FileDTO>} config - * @param {FindConfig<FileDTO>} config -
* The configurations determining how the files are retrieved. Its properties, such as `select` or `relations`, accept the * The configurations determining how the files are retrieved. Its properties, such as `select` or `relations`, accept the
* attributes or relations associated with a file. * 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. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<FileDTO[]>} The list of files. In this case, it will have at most one file. * @returns {Promise<FileDTO[]>} The list of files.
* *
* @example * @example
* const files = await fileModuleService.listFiles({ id: "file_123" }) * const files = await fileModuleService.listFiles({ id: ["file_123", "file_456"] })
*/ */
listFiles( listFiles(
filters?: FilterableFileProps, filters?: FilterableFileProps,
@@ -154,14 +154,14 @@ export interface IFileModuleService extends IModuleService {
): Promise<FileDTO[]> ): Promise<FileDTO[]>
/** /**
* 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 {FilterableFileProps} filters - The filters to apply on the retrieved files.
* @param {FindConfig<FileDTO>} config - * @param {FindConfig<FileDTO>} config -
* The configurations determining how the files are retrieved. Its properties, such as `select` or `relations`, accept the * The configurations determining how the files are retrieved. Its properties, such as `select` or `relations`, accept the
* attributes or relations associated with a file. * 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. * @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 * @example
* const [files] = await fileModuleService.listAndCountFiles({ id: "file_123" }) * 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 * 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. * file provider that was used to upload the file to retrievethe stream.
* *
* @version 2.8.0 * @version 2.8.0
* *
* @param {string} id - The ID of the file. * @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. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<Readable>} A readable stream of the file contents. * @returns {Promise<Readable>} 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 * 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. * file provider that was used to upload the file to retrieve the buffer.
* *
* @version 2.8.0 * @version 2.8.0
* *
* @param {string} id - The ID of the file. * @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. * @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<Buffer>} A buffer of the file contents. * @returns {Promise<Buffer>} A buffer of the file contents.
@@ -104,28 +104,25 @@ export default class FileModuleService implements FileTypes.IFileModuleService {
config?: FindConfig<FileDTO>, config?: FindConfig<FileDTO>,
sharedContext?: Context sharedContext?: Context
): Promise<FileDTO[]> { ): Promise<FileDTO[]> {
const id = Array.isArray(filters?.id) ? filters?.id?.[0] : filters?.id if (!filters?.id) {
if (!id) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_DATA, MedusaError.Types.INVALID_DATA,
"Listing 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({ const ids = Array.isArray(filters?.id) ? filters?.id : [filters?.id]
fileKey: id,
})
if (!res) { const res = await Promise.all(
return [] ids.map(async (id) => {
} const res = await this.fileProviderService_.getPresignedDownloadUrl({
fileKey: id,
})
return { id, url: res }
})
)
return [ return res
{
id,
url: res,
},
]
} }
async listAndCountFiles( async listAndCountFiles(
@@ -133,31 +130,25 @@ export default class FileModuleService implements FileTypes.IFileModuleService {
config?: FindConfig<FileDTO>, config?: FindConfig<FileDTO>,
sharedContext?: Context sharedContext?: Context
): Promise<[FileDTO[], number]> { ): Promise<[FileDTO[], number]> {
const id = Array.isArray(filters?.id) ? filters?.id?.[0] : filters?.id if (!filters?.id) {
if (!id) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_DATA, 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({ const ids = Array.isArray(filters?.id) ? filters?.id : [filters?.id]
fileKey: id,
})
if (!res) { const res = await Promise.all(
return [[], 0] ids.map(async (id) => {
} const res = await this.fileProviderService_.getPresignedDownloadUrl({
fileKey: id,
})
return { id, url: res }
})
)
return [ return [res, res.length]
[
{
id,
url: res,
},
],
1,
]
} }
/** /**