download.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import type { NextApiRequest, NextApiResponse } from "next";
  2. import path from "path";
  3. // require modules
  4. const fs = require("fs");
  5. const archiver = require("archiver");
  6. interface IData {
  7. buildId: string;
  8. }
  9. export default async function handler(
  10. req: NextApiRequest,
  11. res: NextApiResponse<IData>
  12. ) {
  13. const queryObject = req.query;
  14. console.log("download-id", queryObject);
  15. const storeFile = __dirname + "/luckysheet-custom-build.zip";
  16. // create a file to stream archive data to.
  17. const output = fs.createWriteStream(storeFile);
  18. const archive = archiver("zip", {
  19. zlib: { level: 9 }, // Sets the compression level.
  20. });
  21. // listen for all archive data to be written
  22. // 'close' event is fired only when a file descriptor is involved
  23. output.on("close", ()=> {
  24. console.log(archive.pointer() + " total bytes");
  25. console.log(
  26. "archiver has been finalized and the output file descriptor has closed."
  27. );
  28. res.writeHead(200, {
  29. "Content-Type": "application/octet-stream",
  30. "Content-Disposition": "attachment; filename=luckysheet-custom-build.zip",
  31. "Content-Length": archive.pointer(),
  32. });
  33. fs.createReadStream(storeFile).pipe(res);
  34. });
  35. // This event is fired when the data source is drained no matter what was the data source.
  36. // It is not part of this library but rather from the NodeJS Stream API.
  37. // @see: https://nodejs.org/api/stream.html#stream_event_end
  38. output.on("end", function () {
  39. console.log("Data has been drained");
  40. });
  41. // good practice to catch warnings (ie stat failures and other non-blocking errors)
  42. archive.on("warning", function (err: any) {
  43. if (err.code === "ENOENT") {
  44. // log warning
  45. } else {
  46. // throw error
  47. throw err;
  48. }
  49. });
  50. // good practice to catch this error explicitly
  51. archive.on("error", function (err: any) {
  52. throw err;
  53. });
  54. // pipe archive data to the file
  55. archive.pipe(output);
  56. // append files from a sub-directory, putting its contents at the root of archive
  57. // path.join(CURR_DIR, 'luckysheet-custom-build')
  58. const libFolder = path.join(
  59. __dirname,
  60. `../../../../luckysheet-custom-build-${queryObject.buildId}/lib/`
  61. );
  62. console.log("libFolder", libFolder);
  63. // // append a file from buffer
  64. // const buffer3 = Buffer.from("buff it!");
  65. // archive.append(buffer3, { name: "file3.txt" });
  66. archive.directory(libFolder, false);
  67. // finalize the archive (ie we are done appending files but streams have to finish yet)
  68. // 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
  69. archive.finalize();
  70. // res.status(200).json({ buildId: "download!" });
  71. }