Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

error handling missing avatar

Daniel1fc0804cbf38
diff --git a/lib/api/storage/readFile.ts b/lib/api/storage/readFile.ts
index 237fac55..0cad83b4 100644
--- a/lib/api/storage/readFile.ts
+++ b/lib/api/storage/readFile.ts
@@ -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 path from "path";
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 }) {
- let contentType:
- | "text/plain"
- | "image/jpeg"
- | "image/png"
- | "application/pdf";
+ let contentType: ReturnContentTypes;
if (s3Client) {
const bucketParams: GetObjectCommandInput = {
@@ -17,26 +24,52 @@ export default async function readFile({ filePath }: { filePath: string }) {
};
try {
- const response = await s3Client.send(new GetObjectCommand(bucketParams));
- const data = await streamToBuffer(response.Body);
+ let returnObject:
+ | {
+ file: Buffer | string;
+ contentType: ReturnContentTypes;
+ }
+ | undefined;
+
+ const headObjectAsync = util.promisify(
+ s3Client.headObject.bind(s3Client)
+ );
- if (filePath.endsWith(".pdf")) {
- contentType = "application/pdf";
- } else if (filePath.endsWith(".png")) {
- contentType = "image/png";
- } else {
- // if (filePath.endsWith(".jpg"))
- contentType = "image/jpeg";
+ 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,
+ };
}
- 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) {
- console.log("Error", err);
+ console.log("Error:", err);
contentType = "text/plain";
-
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,
};
}
diff --git a/package.json b/package.json
index f397219b..cf937dbe 100644
--- a/package.json
+++ b/package.json
@@ -31,6 +31,7 @@
"bcrypt": "^5.1.0",
"colorthief": "^2.4.0",
"crypto-js": "^4.1.1",
+ "csstype": "^3.1.2",
"eslint": "8.44.0",
"eslint-config-next": "13.4.9",
"next": "13.1.6",
diff --git a/yarn.lock b/yarn.lock
index c0ae3897..1dff74b2 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2073,6 +2073,11 @@ csstype@^3.0.2:
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.1.tgz#841b532c45c758ee546a11d5bd7b7b473c8c30b9"
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:
version "1.1.3"
resolved "https://registry.yarnpkg.com/cwise-compiler/-/cwise-compiler-1.1.3.tgz#f4d667410e850d3a313a7d2db7b1e505bb034cc5"