You've already forked joplin
							
							
				mirror of
				https://github.com/laurent22/joplin.git
				synced 2025-10-31 00:07:48 +02:00 
			
		
		
		
	Chore: Improve logs to help debugging (#10432)
This commit is contained in:
		| @@ -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() { | ||||
|   | ||||
| @@ -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, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user