fix: Remove unzipper as it cannot handle zip within zip (#6162)

This commit is contained in:
Tom Moor
2023-11-15 19:32:17 -05:00
committed by GitHub
parent 68a3d327f6
commit 726613bf1d
11 changed files with 272 additions and 145 deletions

View File

@@ -84,6 +84,17 @@ export default abstract class BaseStorage {
acl?: string;
}): Promise<string | undefined>;
/**
* Returns a file handle for a file from the storage provider.
*
* @param key The path to the file
* @returns The file path and a cleanup function
*/
public abstract getFileHandle(key: string): Promise<{
path: string;
cleanup: () => Promise<void>;
}>;
/**
* Returns a buffer of a file from the storage provider.
*

View File

@@ -128,6 +128,15 @@ export default class LocalStorage extends BaseStorage {
return Promise.resolve(`${env.URL}/api/files.get?sig=${sig}`);
};
public async getFileHandle(key: string) {
return {
path: this.getFilePath(key),
cleanup: async () => {
// no-op, as we're reading the canonical file directly
},
};
}
public getFileStream(key: string) {
return createReadStream(this.getFilePath(key));
}

View File

@@ -1,7 +1,10 @@
import path from "path";
import util from "util";
import AWS, { S3 } from "aws-sdk";
import { createWriteStream, remove } from "fs-extra";
import invariant from "invariant";
import compact from "lodash/compact";
import tmp from "tmp";
import env from "@server/env";
import Logger from "@server/logging/Logger";
import BaseStorage from "./BaseStorage";
@@ -159,6 +162,37 @@ export default class S3Storage extends BaseStorage {
return url;
};
public getFileHandle(key: string): Promise<{
path: string;
cleanup: () => Promise<void>;
}> {
return new Promise((resolve, reject) => {
tmp.dir((err, tmpDir) => {
if (err) {
return reject(err);
}
const tmpFile = path.join(tmpDir, "tmp");
const dest = createWriteStream(tmpFile);
dest.on("error", reject);
dest.on("finish", () =>
resolve({ path: tmpFile, cleanup: () => remove(tmpFile) })
);
const stream = this.getFileStream(key);
if (!stream) {
return reject(new Error("No stream available"));
}
stream
.on("error", (err) => {
dest.end();
reject(err);
})
.pipe(dest);
});
});
}
public getFileStream(key: string) {
invariant(
env.AWS_S3_UPLOAD_BUCKET_NAME,