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 { 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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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, {
|
||||
|
||||
Reference in New Issue
Block a user