commit
501e9e59e0
|
@ -1,14 +1,21 @@
|
||||||
import { GetObjectCommand, GetObjectCommandInput } from "@aws-sdk/client-s3";
|
import {
|
||||||
|
GetObjectCommand,
|
||||||
|
GetObjectCommandInput,
|
||||||
|
S3,
|
||||||
|
} from "@aws-sdk/client-s3";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import s3Client from "./s3Client";
|
import s3Client from "./s3Client";
|
||||||
|
import util from "util";
|
||||||
|
|
||||||
|
type ReturnContentTypes =
|
||||||
|
| "text/plain"
|
||||||
|
| "image/jpeg"
|
||||||
|
| "image/png"
|
||||||
|
| "application/pdf";
|
||||||
|
|
||||||
export default async function readFile({ filePath }: { filePath: string }) {
|
export default async function readFile({ filePath }: { filePath: string }) {
|
||||||
let contentType:
|
let contentType: ReturnContentTypes;
|
||||||
| "text/plain"
|
|
||||||
| "image/jpeg"
|
|
||||||
| "image/png"
|
|
||||||
| "application/pdf";
|
|
||||||
|
|
||||||
if (s3Client) {
|
if (s3Client) {
|
||||||
const bucketParams: GetObjectCommandInput = {
|
const bucketParams: GetObjectCommandInput = {
|
||||||
|
@ -17,26 +24,52 @@ export default async function readFile({ filePath }: { filePath: string }) {
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await s3Client.send(new GetObjectCommand(bucketParams));
|
let returnObject:
|
||||||
const data = await streamToBuffer(response.Body);
|
| {
|
||||||
|
file: Buffer | string;
|
||||||
|
contentType: ReturnContentTypes;
|
||||||
|
}
|
||||||
|
| undefined;
|
||||||
|
|
||||||
if (filePath.endsWith(".pdf")) {
|
const headObjectAsync = util.promisify(
|
||||||
contentType = "application/pdf";
|
s3Client.headObject.bind(s3Client)
|
||||||
} else if (filePath.endsWith(".png")) {
|
);
|
||||||
contentType = "image/png";
|
|
||||||
} else {
|
try {
|
||||||
// if (filePath.endsWith(".jpg"))
|
await headObjectAsync(bucketParams);
|
||||||
contentType = "image/jpeg";
|
} catch (err) {
|
||||||
|
contentType = "text/plain";
|
||||||
|
|
||||||
|
returnObject = {
|
||||||
|
file: "File not found, it's possible that the file you're looking for either doesn't exist or hasn't been created yet.",
|
||||||
|
contentType,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return { file: data, contentType };
|
if (!returnObject) {
|
||||||
|
const response = await (s3Client as S3).send(
|
||||||
|
new GetObjectCommand(bucketParams)
|
||||||
|
);
|
||||||
|
const data = await streamToBuffer(response.Body);
|
||||||
|
|
||||||
|
if (filePath.endsWith(".pdf")) {
|
||||||
|
contentType = "application/pdf";
|
||||||
|
} else if (filePath.endsWith(".png")) {
|
||||||
|
contentType = "image/png";
|
||||||
|
} else {
|
||||||
|
// if (filePath.endsWith(".jpg"))
|
||||||
|
contentType = "image/jpeg";
|
||||||
|
}
|
||||||
|
returnObject = { file: data as Buffer, contentType };
|
||||||
|
}
|
||||||
|
|
||||||
|
return returnObject;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("Error", err);
|
console.log("Error:", err);
|
||||||
|
|
||||||
contentType = "text/plain";
|
contentType = "text/plain";
|
||||||
|
|
||||||
return {
|
return {
|
||||||
file: "File not found, it's possible that the file you're looking for either doesn't exist or hasn't been created yet.",
|
file: "An internal occurred, please contact support.",
|
||||||
contentType,
|
contentType,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
11
package.json
11
package.json
|
@ -14,7 +14,7 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@auth/prisma-adapter": "^1.0.1",
|
"@auth/prisma-adapter": "^1.0.1",
|
||||||
"@aws-sdk/client-s3": "^3.363.0",
|
"@aws-sdk/client-s3": "^3.379.1",
|
||||||
"@fortawesome/fontawesome-svg-core": "^6.4.0",
|
"@fortawesome/fontawesome-svg-core": "^6.4.0",
|
||||||
"@fortawesome/free-regular-svg-icons": "^6.4.0",
|
"@fortawesome/free-regular-svg-icons": "^6.4.0",
|
||||||
"@fortawesome/free-solid-svg-icons": "^6.4.0",
|
"@fortawesome/free-solid-svg-icons": "^6.4.0",
|
||||||
|
@ -31,9 +31,10 @@
|
||||||
"bcrypt": "^5.1.0",
|
"bcrypt": "^5.1.0",
|
||||||
"colorthief": "^2.4.0",
|
"colorthief": "^2.4.0",
|
||||||
"crypto-js": "^4.1.1",
|
"crypto-js": "^4.1.1",
|
||||||
"eslint": "8.44.0",
|
"csstype": "^3.1.2",
|
||||||
|
"eslint": "8.46.0",
|
||||||
"eslint-config-next": "13.4.9",
|
"eslint-config-next": "13.4.9",
|
||||||
"next": "13.1.6",
|
"next": "13.4.12",
|
||||||
"next-auth": "^4.22.1",
|
"next-auth": "^4.22.1",
|
||||||
"nodemailer": "^6.9.3",
|
"nodemailer": "^6.9.3",
|
||||||
"playwright": "^1.35.1",
|
"playwright": "^1.35.1",
|
||||||
|
@ -42,7 +43,7 @@
|
||||||
"react-dom": "18.2.0",
|
"react-dom": "18.2.0",
|
||||||
"react-hot-toast": "^2.4.1",
|
"react-hot-toast": "^2.4.1",
|
||||||
"react-image-file-resizer": "^0.4.8",
|
"react-image-file-resizer": "^0.4.8",
|
||||||
"react-select": "^5.7.3",
|
"react-select": "^5.7.4",
|
||||||
"sharp": "^0.32.1",
|
"sharp": "^0.32.1",
|
||||||
"stripe": "^12.13.0",
|
"stripe": "^12.13.0",
|
||||||
"typescript": "4.9.4",
|
"typescript": "4.9.4",
|
||||||
|
@ -53,7 +54,7 @@
|
||||||
"@types/bcrypt": "^5.0.0",
|
"@types/bcrypt": "^5.0.0",
|
||||||
"autoprefixer": "^10.4.14",
|
"autoprefixer": "^10.4.14",
|
||||||
"postcss": "^8.4.26",
|
"postcss": "^8.4.26",
|
||||||
"prisma": "^4.16.2",
|
"prisma": "^5.1.0",
|
||||||
"tailwindcss": "^3.3.3"
|
"tailwindcss": "^3.3.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Ŝarĝante…
Reference in New Issue