mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 03:29:02 +01:00 
			
		
		
		
	server-ts: Convert routes/api/image
This commit is contained in:
		
							parent
							
								
									b552f40ae8
								
							
						
					
					
						commit
						291b791b67
					
				| @ -222,7 +222,7 @@ class BNote extends AbstractBeccaEntity<BNote> { | |||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * @throws Error in case of invalid JSON */ |      * @throws Error in case of invalid JSON */ | ||||||
|     getJsonContent(): {} | null { |     getJsonContent(): any | null { | ||||||
|         const content = this.getContent(); |         const content = this.getContent(); | ||||||
| 
 | 
 | ||||||
|         if (typeof content !== "string" || !content || !content.trim()) { |         if (typeof content !== "string" || !content || !content.trim()) { | ||||||
|  | |||||||
| @ -1,27 +1,26 @@ | |||||||
| "use strict"; | "use strict"; | ||||||
| 
 | 
 | ||||||
| const imageService = require('../../services/image'); | import imageService = require('../../services/image'); | ||||||
| const becca = require('../../becca/becca'); | import becca = require('../../becca/becca'); | ||||||
| const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR; | const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR; | ||||||
| const fs = require('fs'); | import fs = require('fs'); | ||||||
|  | import { Request, Response } from 'express'; | ||||||
|  | import BNote = require('../../becca/entities/bnote'); | ||||||
|  | import BRevision = require('../../becca/entities/brevision'); | ||||||
| 
 | 
 | ||||||
| function returnImageFromNote(req, res) { | function returnImageFromNote(req: Request, res: Response) { | ||||||
|     const image = becca.getNote(req.params.noteId); |     const image = becca.getNote(req.params.noteId); | ||||||
| 
 | 
 | ||||||
|     return returnImageInt(image, res); |     return returnImageInt(image, res); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function returnImageFromRevision(req, res) { | function returnImageFromRevision(req: Request, res: Response) { | ||||||
|     const image = becca.getRevision(req.params.revisionId); |     const image = becca.getRevision(req.params.revisionId); | ||||||
| 
 | 
 | ||||||
|     return returnImageInt(image, res); |     return returnImageInt(image, res); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /** | function returnImageInt(image: BNote | BRevision | null, res: Response) { | ||||||
|  * @param {BNote|BRevision} image |  | ||||||
|  * @param res |  | ||||||
|  */ |  | ||||||
| function returnImageInt(image, res) { |  | ||||||
|     if (!image) { |     if (!image) { | ||||||
|         res.set('Content-Type', 'image/png'); |         res.set('Content-Type', 'image/png'); | ||||||
|         return res.send(fs.readFileSync(`${RESOURCE_DIR}/db/image-deleted.png`)); |         return res.send(fs.readFileSync(`${RESOURCE_DIR}/db/image-deleted.png`)); | ||||||
| @ -40,12 +39,13 @@ function returnImageInt(image, res) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function renderSvgAttachment(image, res, attachmentName) { | function renderSvgAttachment(image: BNote | BRevision, res: Response, attachmentName: string) { | ||||||
|     let svgString = '<svg/>' |     let svgString = '<svg/>' | ||||||
|     const attachment = image.getAttachmentByTitle(attachmentName); |     const attachment = image.getAttachmentByTitle(attachmentName); | ||||||
| 
 | 
 | ||||||
|     if (attachment) { |     const content = attachment.getContent(); | ||||||
|         svgString = attachment.getContent(); |     if (attachment && typeof content === "string") { | ||||||
|  |         svgString = content; | ||||||
|     } else { |     } else { | ||||||
|         // backwards compatibility, before attachments, the SVG was stored in the main note content as a separate key
 |         // backwards compatibility, before attachments, the SVG was stored in the main note content as a separate key
 | ||||||
|         const contentSvg = image.getJsonContentSafely()?.svg; |         const contentSvg = image.getJsonContentSafely()?.svg; | ||||||
| @ -62,7 +62,7 @@ function renderSvgAttachment(image, res, attachmentName) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| function returnAttachedImage(req, res) { | function returnAttachedImage(req: Request, res: Response) { | ||||||
|     const attachment = becca.getAttachment(req.params.attachmentId); |     const attachment = becca.getAttachment(req.params.attachmentId); | ||||||
| 
 | 
 | ||||||
|     if (!attachment) { |     if (!attachment) { | ||||||
| @ -81,9 +81,9 @@ function returnAttachedImage(req, res) { | |||||||
|     res.send(attachment.getContent()); |     res.send(attachment.getContent()); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function updateImage(req) { | function updateImage(req: Request) { | ||||||
|     const {noteId} = req.params; |     const {noteId} = req.params; | ||||||
|     const {file} = req; |     const {file} = (req as any); | ||||||
| 
 | 
 | ||||||
|     const note = becca.getNoteOrThrow(noteId); |     const note = becca.getNoteOrThrow(noteId); | ||||||
| 
 | 
 | ||||||
| @ -99,7 +99,7 @@ function updateImage(req) { | |||||||
|     return { uploaded: true }; |     return { uploaded: true }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| module.exports = { | export = { | ||||||
|     returnImageFromNote, |     returnImageFromNote, | ||||||
|     returnImageFromRevision, |     returnImageFromRevision, | ||||||
|     returnAttachedImage, |     returnAttachedImage, | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Elian Doran
						Elian Doran