Skip to content

Commit

Permalink
feat(wado-rs): send content-length of zip
Browse files Browse the repository at this point in the history
- Use writable to write zip file into buffer
- Response buffer instead of `archive.pipe(res)`

# Chore
- Specify the name of single file that the file is at the root in zip
  • Loading branch information
Chinlinlee committed Apr 28, 2023
1 parent 93596fd commit 281927f
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions api/dicom-web/controller/WADO-RS/service/WADOZip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const archiver = require("archiver");
const wadoService = require("./WADO-RS.service");
const path = require("path");
const dicomModel = require("../../../../../models/mongodb/models/dicom");
const fs = require("fs");
const uuid = require("uuid");
const { Writable } = require("stream");

class WADOZip {
constructor(iParam, iRes) {
Expand Down Expand Up @@ -72,12 +75,39 @@ class WADOZip {
}
}

/**
*
* @param {import('express').Response} res
* @param {string[]} folders
* @param {string} filename
* @returns
*/
function toZip(res, folders = [], filename = "") {
return new Promise((resolve) => {

const bufferArray = [];
const converter = new Writable();
converter._write = (chunk, encoding, cb) => {
bufferArray.push(chunk);
process.nextTick(cb);
};

converter.on('finish', () => {
let fullBuffer = Buffer.concat(bufferArray);
res.setHeader("Content-Length", fullBuffer.length);
res.write(fullBuffer);
resolve({
status: true,
code: 200,
data: "success"
});
});

let archive = archiver('zip', {
gzip: true,
zlib: { level: 9 } // Sets the compression level.
});

archive.on('error', function (err) {
console.error(err);
resolve({
Expand All @@ -86,23 +116,22 @@ function toZip(res, folders = [], filename = "") {
data: err
});
});
archive.pipe(res);

archive.pipe(converter);

if (folders.length > 0) {
for (let i = 0; i < folders.length; i++) {
let folderName = path.basename(folders[i]);
//archive.append(null, {name : folderName});
archive.glob("*.dcm", { cwd: folders[i] }, { prefix: folderName });
}
} else {
archive.file(filename);
}
archive.finalize().then(() => {
resolve({
status: true,
code: 200,
data: "success"
archive.file(filename, {
name: path.basename(filename)
});
});
}

archive.finalize();
});
}

Expand Down

0 comments on commit 281927f

Please sign in to comment.