mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 06:59:31 +01:00
web: add logging related to attachments
This commit is contained in:
committed by
Abdullah Atta
parent
647ff7ae0e
commit
0f5ce465c3
@@ -18,12 +18,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { lazify } from "../utils/lazify";
|
import { lazify } from "../utils/lazify";
|
||||||
|
import { logger } from "../utils/logger";
|
||||||
import { showToast } from "../utils/toast";
|
import { showToast } from "../utils/toast";
|
||||||
import { db } from "./db";
|
import { db } from "./db";
|
||||||
|
|
||||||
async function download(hash: string, groupId?: string) {
|
async function download(hash: string, groupId?: string) {
|
||||||
const attachment = await db.attachments.attachment(hash);
|
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
|
const downloadResult = await db
|
||||||
.fs()
|
.fs()
|
||||||
.downloadFile(
|
.downloadFile(
|
||||||
@@ -68,6 +72,7 @@ export async function downloadAttachment<
|
|||||||
type: TType,
|
type: TType,
|
||||||
groupId?: string
|
groupId?: string
|
||||||
): Promise<TOutputType | undefined> {
|
): Promise<TOutputType | undefined> {
|
||||||
|
logger.debug("downloading attachment", { hash, type, groupId });
|
||||||
try {
|
try {
|
||||||
const response = await download(hash, groupId);
|
const response = await download(hash, groupId);
|
||||||
if (!response) return;
|
if (!response) return;
|
||||||
@@ -85,6 +90,7 @@ export async function downloadAttachment<
|
|||||||
isUploaded: !!attachment.dateUploaded
|
isUploaded: !!attachment.dateUploaded
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
logger.debug("Attachment decrypted", { hash });
|
||||||
|
|
||||||
if (!blob) return;
|
if (!blob) return;
|
||||||
return blob as TOutputType;
|
return blob as TOutputType;
|
||||||
|
|||||||
@@ -488,12 +488,25 @@ export function Editor(props: EditorProps) {
|
|||||||
editor?.attachFile(attachment);
|
editor?.attachFile(attachment);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onGetAttachmentData={(attachment) => {
|
onGetAttachmentData={async (attachment) => {
|
||||||
return downloadAttachment(
|
logger.debug("Getting attachment data", {
|
||||||
|
hash: attachment.hash,
|
||||||
|
type: attachment.type
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await downloadAttachment(
|
||||||
attachment.hash,
|
attachment.hash,
|
||||||
attachment.type === "web-clip" ? "text" : "base64",
|
attachment.type === "web-clip" ? "text" : "base64",
|
||||||
id?.toString()
|
id?.toString()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!result)
|
||||||
|
logger.debug("Got no result after downloading attachment", {
|
||||||
|
hash: attachment.hash,
|
||||||
|
type: attachment.type
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
}}
|
}}
|
||||||
onAttachFiles={async (files) => {
|
onAttachFiles={async (files) => {
|
||||||
const editor = useEditorManager.getState().getEditor(id)?.editor;
|
const editor = useEditorManager.getState().getEditor(id)?.editor;
|
||||||
|
|||||||
@@ -463,7 +463,7 @@ async function downloadFile(
|
|||||||
requestOptions: RequestOptionsWithSignal
|
requestOptions: RequestOptionsWithSignal
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
console.log("DOWNLOADING FILE", filename);
|
logger.debug("DOWNLOADING FILE", { filename });
|
||||||
const { url, headers, chunkSize, signal } = requestOptions;
|
const { url, headers, chunkSize, signal } = requestOptions;
|
||||||
const handle = await streamablefs.readFile(filename);
|
const handle = await streamablefs.readFile(filename);
|
||||||
|
|
||||||
@@ -475,6 +475,7 @@ async function downloadFile(
|
|||||||
else if (handle) await handle.delete();
|
else if (handle) await handle.delete();
|
||||||
|
|
||||||
const attachment = await db.attachments.attachment(filename);
|
const attachment = await db.attachments.attachment(filename);
|
||||||
|
if (!attachment) throw new Error("Attachment doesn't exist.");
|
||||||
|
|
||||||
reportProgress(
|
reportProgress(
|
||||||
{ total: 100, loaded: 0 },
|
{ total: 100, loaded: 0 },
|
||||||
@@ -488,10 +489,14 @@ async function downloadFile(
|
|||||||
})
|
})
|
||||||
).data;
|
).data;
|
||||||
|
|
||||||
|
logger.debug("Got attachment signed url", { filename });
|
||||||
|
|
||||||
const response = await fetch(signedUrl, {
|
const response = await fetch(signedUrl, {
|
||||||
signal
|
signal
|
||||||
});
|
});
|
||||||
|
|
||||||
|
logger.debug("Got attachment", { filename });
|
||||||
|
|
||||||
const contentType = response.headers.get("content-type");
|
const contentType = response.headers.get("content-type");
|
||||||
if (contentType === "application/xml") {
|
if (contentType === "application/xml") {
|
||||||
const error = parseS3Error(await response.text());
|
const error = parseS3Error(await response.text());
|
||||||
@@ -548,9 +553,10 @@ async function downloadFile(
|
|||||||
)
|
)
|
||||||
.pipeTo(fileHandle.writeable);
|
.pipeTo(fileHandle.writeable);
|
||||||
|
|
||||||
|
logger.debug("Attachment downloaded", { filename });
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
logger.error(e, "Could not download file", { filename });
|
||||||
showError(toS3Error(e), "Could not download file");
|
showError(toS3Error(e), "Could not download file");
|
||||||
reportProgress(undefined, { type: "download", hash: filename });
|
reportProgress(undefined, { type: "download", hash: filename });
|
||||||
return false;
|
return false;
|
||||||
@@ -597,9 +603,11 @@ export async function streamingDecryptFile(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function saveFile(filename: string, fileMetadata: FileMetadata) {
|
export async function saveFile(filename: string, fileMetadata: FileMetadata) {
|
||||||
|
logger.debug("Saving file", { filename });
|
||||||
const { name, type, isUploaded } = fileMetadata;
|
const { name, type, isUploaded } = fileMetadata;
|
||||||
|
|
||||||
const decrypted = await decryptFile(filename, fileMetadata);
|
const decrypted = await decryptFile(filename, fileMetadata);
|
||||||
|
logger.debug("Decrypting file", { filename, result: !!decrypted });
|
||||||
if (decrypted) saveAs(decrypted, getFileNameWithExtension(name, type));
|
if (decrypted) saveAs(decrypted, getFileNameWithExtension(name, type));
|
||||||
|
|
||||||
if (isUploaded && isAttachmentDeletable(type))
|
if (isUploaded && isAttachmentDeletable(type))
|
||||||
|
|||||||
@@ -100,6 +100,9 @@ export class Attachments implements ICollection {
|
|||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
await this.collection.init();
|
await this.collection.init();
|
||||||
|
logger.debug("attachments initialized", {
|
||||||
|
total: await this.collection.count()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async add(
|
async add(
|
||||||
@@ -199,8 +202,12 @@ export class Attachments implements ICollection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async remove(hashOrId: string, localOnly: boolean) {
|
async remove(hashOrId: string, localOnly: boolean) {
|
||||||
|
logger.debug("Removing attachment", { hashOrId, localOnly });
|
||||||
const attachment = await this.attachment(hashOrId);
|
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)))
|
if (!localOnly && !(await this.canDetach(attachment)))
|
||||||
throw new Error("This attachment is inside a locked note.");
|
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> {
|
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)])
|
eb.or([eb("id", "==", hashOrId), eb("hash", "==", hashOrId)])
|
||||||
);
|
);
|
||||||
|
if (attachment) logger.debug("attachment exists", { hashOrId });
|
||||||
|
return attachment;
|
||||||
}
|
}
|
||||||
|
|
||||||
markAsUploaded(id: string) {
|
markAsUploaded(id: string) {
|
||||||
|
|||||||
@@ -140,6 +140,8 @@ export class FileStorage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async downloadFile(groupId: string, filename: string, chunkSize: number) {
|
async downloadFile(groupId: string, filename: string, chunkSize: number) {
|
||||||
|
logger.debug("[downloadFile] downloading", { filename, groupId });
|
||||||
|
|
||||||
const url = `${hosts.API_HOST}/s3?name=${filename}`;
|
const url = `${hosts.API_HOST}/s3?name=${filename}`;
|
||||||
const token = await this.tokenManager.getAccessToken();
|
const token = await this.tokenManager.getAccessToken();
|
||||||
const { execute, cancel } = this.fs.downloadFile(filename, {
|
const { execute, cancel } = this.fs.downloadFile(filename, {
|
||||||
|
|||||||
Reference in New Issue
Block a user