From e7e7d570c7bd13c28db3c4ef7645f89199dce48b Mon Sep 17 00:00:00 2001 From: Suhas Vishwanath Date: Tue, 3 Oct 2023 12:51:40 +0530 Subject: [PATCH] avniproject/avni-media#159 | Fix image count. Create download root per job --- .../src/media-viewer/media-viewer.service.ts | 23 ++++++++++++------- server/src/utils/file-utility.ts | 3 ++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/server/src/media-viewer/media-viewer.service.ts b/server/src/media-viewer/media-viewer.service.ts index 4c9e86e..a99293a 100644 --- a/server/src/media-viewer/media-viewer.service.ts +++ b/server/src/media-viewer/media-viewer.service.ts @@ -32,10 +32,11 @@ export class MediaViewerService { const parsedData = JSON.parse(jsonData); - let downloadRootFolder = ''; let imageCount = 0; await Promise.all( Object.keys(parsedData).map(async (id) => { + const jobId = parsedData[id].id; + const downloadRoot = `job${jobId}`; const locationHierarchy = parsedData[id].location_level; const downloadResults = await Promise.all( parsedData[id].image_metadata.map( @@ -50,11 +51,15 @@ export class MediaViewerService { i: any, ) => { const folderName = await this.fileUtility.folderStructure( + downloadRoot, metadata, locationHierarchy, ); - downloadRootFolder = this.getRootFolder(folderName); - await this.downloadMediaItemToFilesystem(metadata, i, folderName); + return await this.downloadMediaItemToFilesystem( + metadata, + i, + folderName, + ); }, ), ); @@ -62,11 +67,11 @@ export class MediaViewerService { (downloadResult) => downloadResult, ).length; this.logger.log( - `Completed media downloads for job ${parsedData[id].id}`, + `Completed media downloads for job ${jobId} with ${imageCount} images`, ); const { zipFileName, localZipfilePath, fileSizeInBytes } = - await this.createZip(parsedData[id].id, downloadRootFolder); + await this.createZip(jobId, downloadRoot); const fileSizeLabel = this.fileUtility.getFileSizeText(fileSizeInBytes); const s3FileName = 'media-zipped-files/' + zipFileName; @@ -86,7 +91,7 @@ export class MediaViewerService { fileSizeLabel, imageCount, ); - await this.cleanup(localZipfilePath, downloadRootFolder); + await this.cleanup(localZipfilePath, downloadRoot); }), ); @@ -104,7 +109,7 @@ export class MediaViewerService { index: any, folderName: string, ) { - let downloadSuccessful = false; + let downloadSuccessful: boolean; const imageUrl = metadata.url; try { @@ -140,7 +145,9 @@ export class MediaViewerService { downloadSuccessful = true; } else { downloadSuccessful = false; - console.error(`Could not download ${imageUrl} with parts ${parts}`); + this.logger.error( + `Skipping download of ${imageUrl}, missing required prefix in path`, + ); } } catch (error) { this.logger.error(`Error downloading image from ${imageUrl}: ${error}`); diff --git a/server/src/utils/file-utility.ts b/server/src/utils/file-utility.ts index a80517d..7fe5a58 100644 --- a/server/src/utils/file-utility.ts +++ b/server/src/utils/file-utility.ts @@ -34,6 +34,7 @@ export class FileUtility { } async folderStructure( + downloadRoot: string, metadata: any, locationHierarchy: { name: any }[], ): Promise { @@ -43,7 +44,7 @@ export class FileUtility { const encounterType = metadata.encounterTypeName; const keys = locationHierarchy.map((index: { name: any }) => index.name); const jsonAddress = JSON.parse(address); - const pathPartsArray = []; + const pathPartsArray = [downloadRoot]; let val = ''; for (const key of keys) { val = key.toString();