1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00

Chore: Improve logs to help debugging (#10432)

This commit is contained in:
pedr 2024-05-17 05:42:16 -03:00 committed by GitHub
parent 24a37e0fef
commit 1040675781
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 17 deletions

View File

@ -23,21 +23,20 @@ export class LimitedDownloadController implements DownloadController {
private imagesCount_ = 0; private imagesCount_ = 0;
// how many images links the content has // how many images links the content has
private imageCountExpected_ = 0; private imageCountExpected_ = 0;
private isLimitExceeded_ = false; private requestId = '';
private maxTotalBytes = 0; private maxTotalBytes = 0;
public readonly maxImagesCount: number; public readonly maxImagesCount: number;
private ownerId = '';
public constructor(ownerId: string, maxTotalBytes: number, maxImagesCount: number) { public constructor(maxTotalBytes: number, maxImagesCount: number, requestId: string) {
this.ownerId = ownerId;
this.maxTotalBytes = maxTotalBytes; this.maxTotalBytes = maxTotalBytes;
this.maxImagesCount = maxImagesCount; this.maxImagesCount = maxImagesCount;
this.requestId = requestId;
} }
public set totalBytes(value: number) { public set totalBytes(value: number) {
if (this.totalBytes_ >= this.maxTotalBytes) { 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; this.totalBytes_ = value;
} }
@ -48,7 +47,7 @@ export class LimitedDownloadController implements DownloadController {
public set imagesCount(value: number) { public set imagesCount(value: number) {
if (this.imagesCount_ > this.maxImagesCount) { 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; this.imagesCount_ = value;
} }
@ -78,12 +77,10 @@ export class LimitedDownloadController implements DownloadController {
} }
public printStats() { public printStats() {
if (!this.isLimitExceeded_) return; const totalBytes = `Total downloaded: ${bytesToHuman(this.totalBytes)}. Maximum: ${bytesToHuman(this.maxTotalBytes)}`;
const owner = `Owner id: ${this.ownerId}`;
const totalBytes = `Total bytes stored: ${this.totalBytes}. Maximum: ${this.maxTotalBytes}`;
const totalImages = `Images initiated for download: ${this.imagesCount_}. Maximum: ${this.maxImagesCount}. Expected: ${this.imageCountExpected}`; 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() { public limitMessage() {

View File

@ -249,8 +249,6 @@ const isValidUrl = (url: string, isDataUrl: boolean, urlProtocol?: string, allow
}; };
export async function downloadMediaFile(url: string, fetchOptions?: FetchOptions, allowedProtocols?: string[]) { 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 // The URL we get to download have been extracted from the Markdown document
url = markdownUtils.unescapeLinkUrl(url); url = markdownUtils.unescapeLinkUrl(url);
@ -429,7 +427,7 @@ async function attachImageFromDataUrl(note: any, imageDataUrl: string, cropRect:
export const extractNoteFromHTML = async ( export const extractNoteFromHTML = async (
requestNote: RequestNote, requestNote: RequestNote,
requestId: number, requestId: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
imageSizes: any, imageSizes: any,
fetchOptions?: FetchOptions, fetchOptions?: FetchOptions,
@ -443,10 +441,13 @@ export const extractNoteFromHTML = async (
const mediaFiles = await downloadMediaFiles(mediaUrls, fetchOptions, allowedProtocols); 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); const resources = await createResourcesFromPaths(mediaFiles);
logger.info(`Request (${requestId}): Deleting temporary files`);
await removeTempFiles(resources); await removeTempFiles(resources);
logger.info(`Request (${requestId}): Replacing urls by resources`);
note.body = replaceUrlsByResources(note.markup_language, note.body, resources, imageSizes); note.body = replaceUrlsByResources(note.markup_language, note.body, resources, imageSizes);
logger.info(`Request (${requestId}): Saving note...`); 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 allowedProtocolsForDownloadMediaFiles = ['http:', 'https:', 'file:', 'data:'];
const extracted = await extractNoteFromHTML( const extracted = await extractNoteFromHTML(
requestNote, requestNote,
requestId, String(requestId),
imageSizes, imageSizes,
undefined, undefined,
allowedProtocolsForDownloadMediaFiles, allowedProtocolsForDownloadMediaFiles,