From 10406757819d86dc89fce62932e84f8193212dbf Mon Sep 17 00:00:00 2001 From: pedr Date: Fri, 17 May 2024 05:42:16 -0300 Subject: [PATCH] Chore: Improve logs to help debugging (#10432) --- packages/lib/downloadController.ts | 19 ++++++++----------- packages/lib/services/rest/routes/notes.ts | 13 +++++++------ 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/packages/lib/downloadController.ts b/packages/lib/downloadController.ts index baedc8940..01e048c2a 100644 --- a/packages/lib/downloadController.ts +++ b/packages/lib/downloadController.ts @@ -23,21 +23,20 @@ export class LimitedDownloadController implements DownloadController { private imagesCount_ = 0; // how many images links the content has private imageCountExpected_ = 0; - private isLimitExceeded_ = false; + private requestId = ''; private maxTotalBytes = 0; public readonly maxImagesCount: number; - private ownerId = ''; - public constructor(ownerId: string, maxTotalBytes: number, maxImagesCount: number) { - this.ownerId = ownerId; + public constructor(maxTotalBytes: number, maxImagesCount: number, requestId: string) { this.maxTotalBytes = maxTotalBytes; this.maxImagesCount = maxImagesCount; + this.requestId = requestId; } public set totalBytes(value: number) { if (this.totalBytes_ >= this.maxTotalBytes) { - throw new JoplinError(`Total bytes stored (${this.totalBytes_}) has exceeded the amount established (${this.maxTotalBytes})`, ErrorCode.DownloadLimiter); + throw new JoplinError(`${this.requestId}: Total bytes stored (${this.totalBytes_}) has exceeded the amount established (${this.maxTotalBytes})`, ErrorCode.DownloadLimiter); } this.totalBytes_ = value; } @@ -48,7 +47,7 @@ export class LimitedDownloadController implements DownloadController { public set imagesCount(value: number) { if (this.imagesCount_ > this.maxImagesCount) { - throw new JoplinError(`Total images to be stored (${this.imagesCount_}) has exceeded the amount established (${this.maxImagesCount})`, ErrorCode.DownloadLimiter); + throw new JoplinError(`${this.requestId}: Total images to be stored (${this.imagesCount_}) has exceeded the amount established (${this.maxImagesCount})`, ErrorCode.DownloadLimiter); } this.imagesCount_ = value; } @@ -78,12 +77,10 @@ export class LimitedDownloadController implements DownloadController { } public printStats() { - if (!this.isLimitExceeded_) return; - - const owner = `Owner id: ${this.ownerId}`; - const totalBytes = `Total bytes stored: ${this.totalBytes}. Maximum: ${this.maxTotalBytes}`; + const totalBytes = `Total downloaded: ${bytesToHuman(this.totalBytes)}. Maximum: ${bytesToHuman(this.maxTotalBytes)}`; const totalImages = `Images initiated for download: ${this.imagesCount_}. Maximum: ${this.maxImagesCount}. Expected: ${this.imageCountExpected}`; - logger.info(`${owner} - ${totalBytes} - ${totalImages}`); + logger.info(`${this.requestId}: ${totalBytes}`); + logger.info(`${this.requestId}: ${totalImages}`); } public limitMessage() { diff --git a/packages/lib/services/rest/routes/notes.ts b/packages/lib/services/rest/routes/notes.ts index 563a6a9ef..0e77d8c3f 100644 --- a/packages/lib/services/rest/routes/notes.ts +++ b/packages/lib/services/rest/routes/notes.ts @@ -249,8 +249,6 @@ const isValidUrl = (url: string, isDataUrl: boolean, urlProtocol?: string, allow }; export async function downloadMediaFile(url: string, fetchOptions?: FetchOptions, allowedProtocols?: string[]) { - logger.info('Downloading media file', url); - // The URL we get to download have been extracted from the Markdown document url = markdownUtils.unescapeLinkUrl(url); @@ -429,7 +427,7 @@ async function attachImageFromDataUrl(note: any, imageDataUrl: string, cropRect: export const extractNoteFromHTML = async ( requestNote: RequestNote, - requestId: number, + requestId: string, // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied imageSizes: any, fetchOptions?: FetchOptions, @@ -443,10 +441,13 @@ export const extractNoteFromHTML = async ( const mediaFiles = await downloadMediaFiles(mediaUrls, fetchOptions, allowedProtocols); - logger.info(`Request (${requestId}): Creating resources from paths: ${mediaFiles.length}`); - + logger.info(`Request (${requestId}): Creating resources from paths (resizing images): ${mediaFiles.length}`); const resources = await createResourcesFromPaths(mediaFiles); + + logger.info(`Request (${requestId}): Deleting temporary files`); await removeTempFiles(resources); + + logger.info(`Request (${requestId}): Replacing urls by resources`); note.body = replaceUrlsByResources(note.markup_language, note.body, resources, imageSizes); logger.info(`Request (${requestId}): Saving note...`); @@ -499,7 +500,7 @@ export default async function(request: Request, id: string = null, link: string const allowedProtocolsForDownloadMediaFiles = ['http:', 'https:', 'file:', 'data:']; const extracted = await extractNoteFromHTML( requestNote, - requestId, + String(requestId), imageSizes, undefined, allowedProtocolsForDownloadMediaFiles,