| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  | const protectedSessionService = require('../../services/protected_session'); | 
					
						
							| 
									
										
										
										
											2019-01-13 10:22:17 +01:00
										 |  |  | const utils = require('../../services/utils'); | 
					
						
							| 
									
										
										
										
											2021-04-24 21:56:44 +02:00
										 |  |  | const log = require('../../services/log'); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  | const noteRevisionService = require('../../services/note_revisions'); | 
					
						
							| 
									
										
										
										
											2020-10-13 22:50:45 +02:00
										 |  |  | const tmp = require('tmp'); | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2021-04-17 22:35:47 +02:00
										 |  |  | const { Readable } = require('stream'); | 
					
						
							| 
									
										
										
										
											2021-04-24 11:39:59 +02:00
										 |  |  | const chokidar = require('chokidar'); | 
					
						
							|  |  |  | const ws = require('../../services/ws'); | 
					
						
							| 
									
										
										
										
											2021-06-29 22:15:57 +02:00
										 |  |  | const becca = require("../../becca/becca"); | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function updateFile(req) { | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |     const {noteId} = req.params; | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |     const file = req.file; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 11:23:58 +02:00
										 |  |  |     const note = becca.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |     if (!note) { | 
					
						
							|  |  |  |         return [404, `Note ${noteId} doesn't exist.`]; | 
					
						
							| 
									
										
										
										
											2018-02-14 23:31:20 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     noteRevisionService.createNoteRevision(note); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     note.mime = file.mimetype.toLowerCase(); | 
					
						
							| 
									
										
										
										
											2020-08-18 22:20:47 +02:00
										 |  |  |     note.save(); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     note.setContent(file.buffer); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     note.setLabel('originalFileName', file.originalname); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     noteRevisionService.protectNoteRevisions(note); | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |         uploaded: true | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 22:50:45 +02:00
										 |  |  | 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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function downloadNoteFile(noteId, res, contentDisposition = true) { | 
					
						
							| 
									
										
										
										
											2021-05-02 11:23:58 +02:00
										 |  |  |     const note = becca.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!note) { | 
					
						
							| 
									
										
										
										
											2018-02-23 22:58:24 -05:00
										 |  |  |         return res.status(404).send(`Note ${noteId} doesn't exist.`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 21:27:46 -04:00
										 |  |  |     if (note.isProtected && !protectedSessionService.isProtectedSessionAvailable()) { | 
					
						
							| 
									
										
										
										
											2019-01-27 15:47:40 +01:00
										 |  |  |         return res.status(401).send("Protected session not available"); | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-08 18:06:24 +01:00
										 |  |  |     if (contentDisposition) { | 
					
						
							| 
									
										
										
										
											2020-10-13 22:50:45 +02:00
										 |  |  |         const filename = getFilename(note); | 
					
						
							| 
									
										
										
										
											2020-05-12 10:28:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         res.setHeader('Content-Disposition', utils.getContentDisposition(filename)); | 
					
						
							| 
									
										
										
										
											2020-03-08 18:06:24 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 20:24:42 +01:00
										 |  |  |     res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  |     res.setHeader('Content-Type', note.mime); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     res.send(note.getContent()); | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function downloadFile(req, res) { | 
					
						
							| 
									
										
										
										
											2019-01-27 15:47:40 +01:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     return downloadNoteFile(noteId, res); | 
					
						
							| 
									
										
										
										
											2020-03-08 18:06:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function openFile(req, res) { | 
					
						
							| 
									
										
										
										
											2020-03-08 18:06:24 +01:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							| 
									
										
										
										
											2019-01-27 15:47:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     return downloadNoteFile(noteId, res, false); | 
					
						
							| 
									
										
										
										
											2019-01-27 15:47:40 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-17 22:35:47 +02:00
										 |  |  | function fileContentProvider(req) { | 
					
						
							|  |  |  |     // Read file name from route params.
 | 
					
						
							| 
									
										
										
										
											2021-05-02 11:23:58 +02:00
										 |  |  |     const note = becca.getNote(req.params.noteId); | 
					
						
							| 
									
										
										
										
											2021-04-17 22:35:47 +02:00
										 |  |  |     const fileName = getFilename(note); | 
					
						
							|  |  |  |     let content = note.getContent(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (typeof content === "string") { | 
					
						
							|  |  |  |        content = Buffer.from(content, 'utf8'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const totalSize = content.byteLength; | 
					
						
							|  |  |  |     const mimeType = note.mime; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const getStream = range => { | 
					
						
							|  |  |  |         if (!range) { | 
					
						
							|  |  |  |             // Request if for complete content.
 | 
					
						
							|  |  |  |             return Readable.from(content); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // Partial content request.
 | 
					
						
							|  |  |  |         const { start, end } = range; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return Readable.from(content.slice(start, end + 1)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         fileName, | 
					
						
							|  |  |  |         totalSize, | 
					
						
							|  |  |  |         mimeType, | 
					
						
							|  |  |  |         getStream | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 22:50:45 +02:00
										 |  |  | function saveToTmpDir(req) { | 
					
						
							|  |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 11:23:58 +02:00
										 |  |  |     const note = becca.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2020-10-13 22:50:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-24 21:56:44 +02:00
										 |  |  |     log.info(`Saved temporary file for note ${noteId} into ${tmpObj.name}`); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-24 11:39:59 +02:00
										 |  |  |     if (utils.isElectron()) { | 
					
						
							|  |  |  |         chokidar.watch(tmpObj.name).on('change', (path, stats) => { | 
					
						
							|  |  |  |             ws.sendMessageToAllClients({ | 
					
						
							|  |  |  |                 type: 'openedFileUpdated', | 
					
						
							|  |  |  |                 noteId: noteId, | 
					
						
							|  |  |  |                 lastModifiedMs: stats.atimeMs, | 
					
						
							|  |  |  |                 filePath: tmpObj.name | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 22:50:45 +02:00
										 |  |  |     return { | 
					
						
							|  |  |  |         tmpFilePath: tmpObj.name | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 17:29:13 -04:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |     updateFile, | 
					
						
							| 
									
										
										
										
											2020-03-08 18:06:24 +01:00
										 |  |  |     openFile, | 
					
						
							| 
									
										
										
										
											2021-04-17 22:35:47 +02:00
										 |  |  |     fileContentProvider, | 
					
						
							| 
									
										
										
										
											2019-01-27 15:47:40 +01:00
										 |  |  |     downloadFile, | 
					
						
							| 
									
										
										
										
											2020-10-13 22:50:45 +02:00
										 |  |  |     downloadNoteFile, | 
					
						
							|  |  |  |     saveToTmpDir | 
					
						
							| 
									
										
										
										
											2020-05-12 10:28:31 +02:00
										 |  |  | }; |