| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const express = require('express'); | 
					
						
							|  |  |  | const router = express.Router(); | 
					
						
							|  |  |  | const sql = require('../../services/sql'); | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  | const attributes = require('../../services/attributes'); | 
					
						
							| 
									
										
										
										
											2017-12-03 09:19:48 -05:00
										 |  |  | const html = require('html'); | 
					
						
							| 
									
										
										
										
											2017-12-23 09:57:20 -05:00
										 |  |  | const auth = require('../../services/auth'); | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | const wrap = require('express-promise-wrap').wrap; | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  | const tar = require('tar-stream'); | 
					
						
							|  |  |  | const sanitize = require("sanitize-filename"); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-27 09:47:05 -05:00
										 |  |  | router.get('/:noteId/', auth.checkApiAuthOrElectron, wrap(async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  |     const noteTreeId = await sql.getValue('SELECT noteTreeId FROM note_tree WHERE noteId = ?', [noteId]); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  |     const pack = tar.pack(); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  |     const name = await exportNote(noteTreeId, '', pack); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  |     pack.finalize(); | 
					
						
							| 
									
										
										
										
											2017-12-03 00:10:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  |     res.setHeader('Content-Disposition', 'attachment; filename="' + name + '.tar"'); | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'application/tar'); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  |     pack.pipe(res); | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | })); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  | async function exportNote(noteTreeId, directory, pack) { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const noteTree = await sql.getRow("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]); | 
					
						
							|  |  |  |     const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteTree.noteId]); | 
					
						
							| 
									
										
										
										
											2017-12-03 00:10:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-26 00:07:43 -05:00
										 |  |  |     if (note.isProtected) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-26 20:47:34 -05:00
										 |  |  |     const metadata = await getMetadata(note); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ('exclude_from_export' in metadata.attributes) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-26 20:47:34 -05:00
										 |  |  |     const metadataJson = JSON.stringify(metadata, null, '\t'); | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  |     const childFileName = directory + sanitize(note.title); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-26 20:47:34 -05:00
										 |  |  |     pack.entry({ name: childFileName + ".meta", size: metadataJson.length }, metadataJson); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-26 20:47:34 -05:00
										 |  |  |     const content = note.type === 'text' ? html.prettyPrint(note.content, {indent_size: 2}) : note.content; | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-26 20:47:34 -05:00
										 |  |  |     pack.entry({ name: childFileName + ".dat", size: content.length }, content); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  |     const children = await sql.getRows("SELECT * FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [note.noteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (children.length > 0) { | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  |         for (const child of children) { | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  |             await exportNote(child.noteTreeId, childFileName + "/", pack); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return childFileName; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getMetadata(note) { | 
					
						
							| 
									
										
										
										
											2018-02-26 20:47:34 -05:00
										 |  |  |     return { | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  |         title: note.title, | 
					
						
							|  |  |  |         type: note.type, | 
					
						
							|  |  |  |         mime: note.mime, | 
					
						
							|  |  |  |         attributes: await attributes.getNoteAttributeMap(note.noteId) | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = router; |