Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: add logging related to attachments

Abdullah Atta0f5ce465c361
diff --git a/apps/web/src/common/attachments.ts b/apps/web/src/common/attachments.ts
index 1adb56ef5..4054262c8 100644
--- a/apps/web/src/common/attachments.ts
+++ b/apps/web/src/common/attachments.ts
@@ -18,12 +18,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { lazify } from "../utils/lazify";
+import { logger } from "../utils/logger";
import { showToast } from "../utils/toast";
import { db } from "./db";
async function download(hash: string, groupId?: string) {
const attachment = await db.attachments.attachment(hash);
- if (!attachment) return;
+ if (!attachment) {
+ logger.debug("could not find attachment for download", { hash, groupId });
+ return;
+ }
const downloadResult = await db
.fs()
.downloadFile(
@@ -68,6 +72,7 @@ export async function downloadAttachment<
type: TType,
groupId?: string
): Promise<TOutputType | undefined> {
+ logger.debug("downloading attachment", { hash, type, groupId });
try {
const response = await download(hash, groupId);
if (!response) return;
@@ -85,6 +90,7 @@ export async function downloadAttachment<
isUploaded: !!attachment.dateUploaded
})
);
+ logger.debug("Attachment decrypted", { hash });
if (!blob) return;
return blob as TOutputType;
diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx
index 98ffd4cfe..fa833699a 100644
--- a/apps/web/src/components/editor/index.tsx
+++ b/apps/web/src/components/editor/index.tsx
@@ -488,12 +488,25 @@ export function Editor(props: EditorProps) {
editor?.attachFile(attachment);
}
}}
- onGetAttachmentData={(attachment) => {
- return downloadAttachment(
+ onGetAttachmentData={async (attachment) => {
+ logger.debug("Getting attachment data", {
+ hash: attachment.hash,
+ type: attachment.type
+ });
+
+ const result = await downloadAttachment(
attachment.hash,
attachment.type === "web-clip" ? "text" : "base64",
id?.toString()
);
+
+ if (!result)
+ logger.debug("Got no result after downloading attachment", {
+ hash: attachment.hash,
+ type: attachment.type
+ });
+
+ return result;
}}
onAttachFiles={async (files) => {
const editor = useEditorManager.getState().getEditor(id)?.editor;
diff --git a/apps/web/src/interfaces/fs.ts b/apps/web/src/interfaces/fs.ts
index 755e7bda8..def02295e 100644
--- a/apps/web/src/interfaces/fs.ts
+++ b/apps/web/src/interfaces/fs.ts
@@ -463,7 +463,7 @@ async function downloadFile(
requestOptions: RequestOptionsWithSignal
) {
try {
- console.log("DOWNLOADING FILE", filename);
+ logger.debug("DOWNLOADING FILE", { filename });
const { url, headers, chunkSize, signal } = requestOptions;
const handle = await streamablefs.readFile(filename);
@@ -475,6 +475,7 @@ async function downloadFile(
else if (handle) await handle.delete();
const attachment = await db.attachments.attachment(filename);
+ if (!attachment) throw new Error("Attachment doesn't exist.");
reportProgress(
{ total: 100, loaded: 0 },
@@ -488,10 +489,14 @@ async function downloadFile(
})
).data;
+ logger.debug("Got attachment signed url", { filename });
+
const response = await fetch(signedUrl, {
signal
});
+ logger.debug("Got attachment", { filename });
+
const contentType = response.headers.get("content-type");
if (contentType === "application/xml") {
const error = parseS3Error(await response.text());
@@ -548,9 +553,10 @@ async function downloadFile(
)
.pipeTo(fileHandle.writeable);
+ logger.debug("Attachment downloaded", { filename });
return true;
} catch (e) {
- console.error(e);
+ logger.error(e, "Could not download file", { filename });
showError(toS3Error(e), "Could not download file");
reportProgress(undefined, { type: "download", hash: filename });
return false;
@@ -597,9 +603,11 @@ export async function streamingDecryptFile(
}
export async function saveFile(filename: string, fileMetadata: FileMetadata) {
+ logger.debug("Saving file", { filename });
const { name, type, isUploaded } = fileMetadata;
const decrypted = await decryptFile(filename, fileMetadata);
+ logger.debug("Decrypting file", { filename, result: !!decrypted });
if (decrypted) saveAs(decrypted, getFileNameWithExtension(name, type));
if (isUploaded && isAttachmentDeletable(type))
diff --git a/packages/core/src/collections/attachments.ts b/packages/core/src/collections/attachments.ts
index e5d02181b..d7c536cdf 100644
--- a/packages/core/src/collections/attachments.ts
+++ b/packages/core/src/collections/attachments.ts
@@ -100,6 +100,9 @@ export class Attachments implements ICollection {
async init() {
await this.collection.init();
+ logger.debug("attachments initialized", {
+ total: await this.collection.count()
+ });
}
async add(
@@ -199,8 +202,12 @@ export class Attachments implements ICollection {
}
async remove(hashOrId: string, localOnly: boolean) {
+ logger.debug("Removing attachment", { hashOrId, localOnly });
const attachment = await this.attachment(hashOrId);
- if (!attachment) return false;
+ if (!attachment) {
+ logger.debug("Attachment not found", { hashOrId, localOnly });
+ return false;
+ }
if (!localOnly && !(await this.canDetach(attachment)))
throw new Error("This attachment is inside a locked note.");
@@ -326,9 +333,11 @@ export class Attachments implements ICollection {
}
async attachment(hashOrId: string): Promise<Attachment | undefined> {
- return this.all.find((eb) =>
+ const attachment = await this.all.find((eb) =>
eb.or([eb("id", "==", hashOrId), eb("hash", "==", hashOrId)])
);
+ if (attachment) logger.debug("attachment exists", { hashOrId });
+ return attachment;
}
markAsUploaded(id: string) {
diff --git a/packages/core/src/database/fs.ts b/packages/core/src/database/fs.ts
index b0e41076e..9d65d1a31 100644
--- a/packages/core/src/database/fs.ts
+++ b/packages/core/src/database/fs.ts
@@ -140,6 +140,8 @@ export class FileStorage {
}
async downloadFile(groupId: string, filename: string, chunkSize: number) {
+ logger.debug("[downloadFile] downloading", { filename, groupId });
+
const url = `${hosts.API_HOST}/s3?name=${filename}`;
const token = await this.tokenManager.getAccessToken();
const { execute, cancel } = this.fs.downloadFile(filename, {