| 
									
										
										
										
											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-04-08 13:14:30 -04:00
										 |  |  |     const exportedNoteIds = []; | 
					
						
							|  |  |  |     const name = await exportNoteInner(branchId, ''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function exportNoteInner(branchId, directory) { | 
					
						
							|  |  |  |         const branch = await repository.getBranch(branchId); | 
					
						
							|  |  |  |         const note = await branch.getNote(); | 
					
						
							|  |  |  |         const childFileName = directory + sanitize(note.title); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (exportedNoteIds.includes(note.noteId)) { | 
					
						
							|  |  |  |             saveMetadataFile(childFileName, { | 
					
						
							|  |  |  |                 version: 1, | 
					
						
							|  |  |  |                 clone: true, | 
					
						
							|  |  |  |                 noteId: note.noteId, | 
					
						
							|  |  |  |                 prefix: branch.prefix | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const metadata = { | 
					
						
							|  |  |  |             version: 1, | 
					
						
							|  |  |  |             clone: false, | 
					
						
							|  |  |  |             noteId: note.noteId, | 
					
						
							|  |  |  |             title: note.title, | 
					
						
							|  |  |  |             prefix: branch.prefix, | 
					
						
							|  |  |  |             type: note.type, | 
					
						
							|  |  |  |             mime: note.mime, | 
					
						
							|  |  |  |             labels: (await note.getLabels()).map(label => { | 
					
						
							|  |  |  |                 return { | 
					
						
							|  |  |  |                     name: label.name, | 
					
						
							|  |  |  |                     value: label.value | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (metadata.labels.find(label => label.name === 'excludeFromExport')) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         saveMetadataFile(childFileName, metadata); | 
					
						
							|  |  |  |         saveDataFile(childFileName, note); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         exportedNoteIds.push(note.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const child of await note.getChildBranches()) { | 
					
						
							|  |  |  |             await exportNoteInner(child.branchId, childFileName + "/"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return childFileName; | 
					
						
							| 
									
										
										
										
											2018-02-26 00:07:43 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-08 13:14:30 -04:00
										 |  |  |     function saveDataFile(childFileName, note) { | 
					
						
							|  |  |  |         const content = note.type === 'text' ? html.prettyPrint(note.content, {indent_size: 2}) : note.content; | 
					
						
							| 
									
										
										
										
											2018-02-26 20:47:34 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-08 13:14:30 -04:00
										 |  |  |         pack.entry({name: childFileName + ".dat", size: content.length}, content); | 
					
						
							| 
									
										
										
										
											2018-02-26 20:47:34 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-08 13:14:30 -04:00
										 |  |  |     function saveMetadataFile(childFileName, metadata) { | 
					
						
							|  |  |  |         const metadataJson = JSON.stringify(metadata, null, '\t'); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-08 13:14:30 -04:00
										 |  |  |         pack.entry({name: childFileName + ".meta", size: metadataJson.length}, metadataJson); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-08 13:14:30 -04:00
										 |  |  |     pack.finalize(); | 
					
						
							| 
									
										
										
										
											2018-02-25 10:55:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-08 13:14:30 -04:00
										 |  |  |     res.setHeader('Content-Disposition', 'file; filename="' + name + '.tar"'); | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'application/tar'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pack.pipe(res); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 15:34:07 -04:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     exportNote | 
					
						
							|  |  |  | }; |