| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const sql = require('../../services/sql'); | 
					
						
							| 
									
										
										
										
											2017-12-03 09:19:48 -05:00
										 |  |  | const html = require('html'); | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  | const tar = require('tar-stream'); | 
					
						
							|  |  |  | const sanitize = require("sanitize-filename"); | 
					
						
							| 
									
										
										
										
											2018-03-31 09:07:58 -04:00
										 |  |  | const repository = require("../../services/repository"); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 15:34:07 -04:00
										 |  |  | async function exportNote(req, res) { | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |     const branchId = await sql.getValue('SELECT branchId FROM branches 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-03-31 09:07:58 -04:00
										 |  |  |     const name = await exportNoteInner(branchId, '', 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-03-27 22:11:06 -04:00
										 |  |  |     res.setHeader('Content-Disposition', 'file; filename="' + name + '.tar"'); | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  |     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-03-30 15:34:07 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-31 09:07:58 -04:00
										 |  |  | async function exportNoteInner(branchId, directory, pack) { | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |     const branch = await sql.getRow("SELECT * FROM branches WHERE branchId = ?", [branchId]); | 
					
						
							| 
									
										
										
										
											2018-03-31 09:07:58 -04:00
										 |  |  |     const note = await repository.getEntity("SELECT notes.* FROM notes WHERE noteId = ?", [branch.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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 09:59:44 -04:00
										 |  |  |     if (metadata.labels.find(label => label.name === 'exclude_from_export')) { | 
					
						
							| 
									
										
										
										
											2018-02-26 20:47:34 -05:00
										 |  |  |         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-03-24 21:39:15 -04:00
										 |  |  |     const children = await sql.getRows("SELECT * FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [note.noteId]); | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (children.length > 0) { | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  |         for (const child of children) { | 
					
						
							| 
									
										
										
										
											2018-03-31 09:07:58 -04:00
										 |  |  |             await exportNoteInner(child.branchId, 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-03-03 09:32:21 -05:00
										 |  |  |         version: 1, | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  |         title: note.title, | 
					
						
							|  |  |  |         type: note.type, | 
					
						
							|  |  |  |         mime: note.mime, | 
					
						
							| 
									
										
										
										
											2018-04-01 09:59:44 -04:00
										 |  |  |         labels: (await note.getLabels()).map(label => { | 
					
						
							| 
									
										
										
										
											2018-03-03 09:30:18 -05:00
										 |  |  |             return { | 
					
						
							| 
									
										
										
										
											2018-04-01 09:59:44 -04:00
										 |  |  |                 name: label.name, | 
					
						
							|  |  |  |                 value: label.value | 
					
						
							| 
									
										
										
										
											2018-03-03 09:30:18 -05:00
										 |  |  |             }; | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 15:34:07 -04:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     exportNote | 
					
						
							|  |  |  | }; |