Skip to content

Commit

Permalink
#159 | Fix image count. Create download root per job
Browse files Browse the repository at this point in the history
  • Loading branch information
Suhas Vishwanath committed Oct 3, 2023
1 parent 2a37694 commit e7e7d57
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
23 changes: 15 additions & 8 deletions server/src/media-viewer/media-viewer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -50,23 +51,27 @@ 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,
);
},
),
);
imageCount = downloadResults.filter(
(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;
Expand All @@ -86,7 +91,7 @@ export class MediaViewerService {
fileSizeLabel,
imageCount,
);
await this.cleanup(localZipfilePath, downloadRootFolder);
await this.cleanup(localZipfilePath, downloadRoot);
}),
);

Expand All @@ -104,7 +109,7 @@ export class MediaViewerService {
index: any,
folderName: string,
) {
let downloadSuccessful = false;
let downloadSuccessful: boolean;
const imageUrl = metadata.url;

try {
Expand Down Expand Up @@ -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}`);
Expand Down
3 changes: 2 additions & 1 deletion server/src/utils/file-utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class FileUtility {
}

async folderStructure(
downloadRoot: string,
metadata: any,
locationHierarchy: { name: any }[],
): Promise<string> {
Expand All @@ -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();
Expand Down

0 comments on commit e7e7d57

Please sign in to comment.