Skip to content

Instantly share code, notes, and snippets.

@oantyneskul
Last active July 21, 2020 09:22
Show Gist options
  • Save oantyneskul/f07171ec0a40313f3aec7d4c986a783b to your computer and use it in GitHub Desktop.
Save oantyneskul/f07171ec0a40313f3aec7d4c986a783b to your computer and use it in GitHub Desktop.
import express from "express";
import request from "request";
import async from "async";
import archiver from "archiver";
const app = express();
const port = 3003;
app.get("/", async (req, res) => {
console.time("zip");
const urls = [
"https://picsum.photos/800?random=1",
"https://picsum.photos/800?random=2",
"https://picsum.photos/800?random=3",
"https://picsum.photos/800?random=4",
"https://picsum.photos/800?random=5",
"https://picsum.photos/800?random=6",
"https://picsum.photos/800?random=7",
"https://picsum.photos/800?random=8",
"https://picsum.photos/800?random=9",
"https://picsum.photos/800?random=10",
"https://picsum.photos/800?random=11",
"https://picsum.photos/800?random=12",
"https://picsum.photos/800?random=13",
"https://picsum.photos/800?random=14",
"https://picsum.photos/800?random=15",
"https://picsum.photos/800?random=16",
"https://picsum.photos/800?random=17",
"https://picsum.photos/800?random=18",
"https://picsum.photos/800?random=19",
"https://picsum.photos/800?random=20",
"https://picsum.photos/800?random=21",
"https://picsum.photos/800?random=22",
"https://picsum.photos/800?random=23",
"https://picsum.photos/800?random=24",
"https://picsum.photos/800?random=25",
"https://picsum.photos/800?random=26",
];
res.attachment("example.zip");
await zipMaterials(res, urls);
});
async function zipMaterials(res, urls) {
const zipArchive = archiver("zip");
zipArchive.pipe(res);
async.eachLimit(
urls,
25,
function (url, done) {
var stream = request.get(url);
stream
.on("error", function (err) {
return done(err);
})
.on("end", function () {
return done();
});
zipArchive.append(stream, { name: `${url.replace(/^.*\//, "")}.png` });
},
function (err) {
if (err) throw err;
zipArchive.finalize();
console.timeEnd("zip");
}
);
}
app.listen(port, () =>
console.log(`Example app listening at http://localhost:${port}`)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment