mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 02:16:05 +01:00 
			
		
		
		
	"open" action will save note to temp directory to try it then open with native application, #1304
This commit is contained in:
		| @@ -4,6 +4,8 @@ const protectedSessionService = require('../../services/protected_session'); | ||||
| const repository = require('../../services/repository'); | ||||
| const utils = require('../../services/utils'); | ||||
| const noteRevisionService = require('../../services/note_revisions'); | ||||
| const tmp = require('tmp'); | ||||
| const fs = require('fs'); | ||||
|  | ||||
| function updateFile(req) { | ||||
|     const {noteId} = req.params; | ||||
| @@ -31,6 +33,12 @@ function updateFile(req) { | ||||
|     }; | ||||
| } | ||||
|  | ||||
| function getFilename(note) { | ||||
|     // (one) reason we're not using the originFileName (available as label) is that it's not | ||||
|     // available for older note revisions and thus would be inconsistent | ||||
|     return utils.formatDownloadTitle(note.title, note.type, note.mime); | ||||
| } | ||||
|  | ||||
| function downloadNoteFile(noteId, res, contentDisposition = true) { | ||||
|     const note = repository.getNote(noteId); | ||||
|  | ||||
| @@ -43,9 +51,7 @@ function downloadNoteFile(noteId, res, contentDisposition = true) { | ||||
|     } | ||||
|  | ||||
|     if (contentDisposition) { | ||||
|         // (one) reason we're not using the originFileName (available as label) is that it's not | ||||
|         // available for older note revisions and thus would be inconsistent | ||||
|         const filename = utils.formatDownloadTitle(note.title, note.type, note.mime); | ||||
|         const filename = getFilename(note); | ||||
|  | ||||
|         res.setHeader('Content-Disposition', utils.getContentDisposition(filename)); | ||||
|     } | ||||
| @@ -67,9 +73,29 @@ function openFile(req, res) { | ||||
|     return downloadNoteFile(noteId, res, false); | ||||
| } | ||||
|  | ||||
| function saveToTmpDir(req) { | ||||
|     const noteId = req.params.noteId; | ||||
|  | ||||
|     const note = repository.getNote(noteId); | ||||
|  | ||||
|     if (!note) { | ||||
|         return [404,`Note ${noteId} doesn't exist.`]; | ||||
|     } | ||||
|  | ||||
|     const tmpObj = tmp.fileSync({postfix: getFilename(note)}); | ||||
|  | ||||
|     fs.writeSync(tmpObj.fd, note.getContent()); | ||||
|     fs.closeSync(tmpObj.fd); | ||||
|  | ||||
|     return { | ||||
|         tmpFilePath: tmpObj.name | ||||
|     }; | ||||
| } | ||||
|  | ||||
| module.exports = { | ||||
|     updateFile, | ||||
|     openFile, | ||||
|     downloadFile, | ||||
|     downloadNoteFile | ||||
|     downloadNoteFile, | ||||
|     saveToTmpDir | ||||
| }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user