error handling missing avatar
This commit is contained in:
parent
4c28e211ec
commit
1fc0804cbf
|
@ -1,15 +1,22 @@
|
||||||
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";
|
||||||
|
|
||||||
export default async function readFile({ filePath }: { filePath: string }) {
|
type ReturnContentTypes =
|
||||||
let contentType:
|
|
||||||
| "text/plain"
|
| "text/plain"
|
||||||
| "image/jpeg"
|
| "image/jpeg"
|
||||||
| "image/png"
|
| "image/png"
|
||||||
| "application/pdf";
|
| "application/pdf";
|
||||||
|
|
||||||
|
export default async function readFile({ filePath }: { filePath: string }) {
|
||||||
|
let contentType: ReturnContentTypes;
|
||||||
|
|
||||||
if (s3Client) {
|
if (s3Client) {
|
||||||
const bucketParams: GetObjectCommandInput = {
|
const bucketParams: GetObjectCommandInput = {
|
||||||
Bucket: process.env.BUCKET_NAME,
|
Bucket: process.env.BUCKET_NAME,
|
||||||
|
@ -17,7 +24,32 @@ export default async function readFile({ filePath }: { filePath: string }) {
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await s3Client.send(new GetObjectCommand(bucketParams));
|
let returnObject:
|
||||||
|
| {
|
||||||
|
file: Buffer | string;
|
||||||
|
contentType: ReturnContentTypes;
|
||||||
|
}
|
||||||
|
| undefined;
|
||||||
|
|
||||||
|
const headObjectAsync = util.promisify(
|
||||||
|
s3Client.headObject.bind(s3Client)
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await headObjectAsync(bucketParams);
|
||||||
|
} 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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!returnObject) {
|
||||||
|
const response = await (s3Client as S3).send(
|
||||||
|
new GetObjectCommand(bucketParams)
|
||||||
|
);
|
||||||
const data = await streamToBuffer(response.Body);
|
const data = await streamToBuffer(response.Body);
|
||||||
|
|
||||||
if (filePath.endsWith(".pdf")) {
|
if (filePath.endsWith(".pdf")) {
|
||||||
|
@ -28,15 +60,16 @@ export default async function readFile({ filePath }: { filePath: string }) {
|
||||||
// if (filePath.endsWith(".jpg"))
|
// if (filePath.endsWith(".jpg"))
|
||||||
contentType = "image/jpeg";
|
contentType = "image/jpeg";
|
||||||
}
|
}
|
||||||
|
returnObject = { file: data as Buffer, contentType };
|
||||||
|
}
|
||||||
|
|
||||||
return { file: data, 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,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
"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",
|
||||||
|
"csstype": "^3.1.2",
|
||||||
"eslint": "8.44.0",
|
"eslint": "8.44.0",
|
||||||
"eslint-config-next": "13.4.9",
|
"eslint-config-next": "13.4.9",
|
||||||
"next": "13.1.6",
|
"next": "13.1.6",
|
||||||
|
|
|
@ -2073,6 +2073,11 @@ csstype@^3.0.2:
|
||||||
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.1.tgz#841b532c45c758ee546a11d5bd7b7b473c8c30b9"
|
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.1.tgz#841b532c45c758ee546a11d5bd7b7b473c8c30b9"
|
||||||
integrity sha512-DJR/VvkAvSZW9bTouZue2sSxDwdTN92uHjqeKVm+0dAqdfNykRzQ95tay8aXMBAAPpUiq4Qcug2L7neoRh2Egw==
|
integrity sha512-DJR/VvkAvSZW9bTouZue2sSxDwdTN92uHjqeKVm+0dAqdfNykRzQ95tay8aXMBAAPpUiq4Qcug2L7neoRh2Egw==
|
||||||
|
|
||||||
|
csstype@^3.1.2:
|
||||||
|
version "3.1.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.2.tgz#1d4bf9d572f11c14031f0436e1c10bc1f571f50b"
|
||||||
|
integrity sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==
|
||||||
|
|
||||||
cwise-compiler@^1.1.2:
|
cwise-compiler@^1.1.2:
|
||||||
version "1.1.3"
|
version "1.1.3"
|
||||||
resolved "https://registry.yarnpkg.com/cwise-compiler/-/cwise-compiler-1.1.3.tgz#f4d667410e850d3a313a7d2db7b1e505bb034cc5"
|
resolved "https://registry.yarnpkg.com/cwise-compiler/-/cwise-compiler-1.1.3.tgz#f4d667410e850d3a313a7d2db7b1e505bb034cc5"
|
||||||
|
|
Ŝarĝante…
Reference in New Issue