| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const express = require('express'); | 
					
						
							|  |  |  | const router = express.Router(); | 
					
						
							|  |  |  | const rimraf = require('rimraf'); | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const sql = require('../../services/sql'); | 
					
						
							|  |  |  | const data_dir = require('../../services/data_dir'); | 
					
						
							| 
									
										
										
										
											2017-12-03 09:19:48 -05:00
										 |  |  | const html = require('html'); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | router.get('/:noteId/to/:directory', async (req, res, next) => { | 
					
						
							|  |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  |     const directory = req.params.directory.replace(/[^0-9a-zA-Z_-]/gi, ''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!fs.existsSync(data_dir.EXPORT_DIR)) { | 
					
						
							|  |  |  |         fs.mkdirSync(data_dir.EXPORT_DIR); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const completeExportDir = data_dir.EXPORT_DIR + '/' + directory; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (fs.existsSync(completeExportDir)) { | 
					
						
							|  |  |  |         rimraf.sync(completeExportDir); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fs.mkdirSync(completeExportDir); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 00:10:43 -05:00
										 |  |  |     const noteTreeId = await sql.getSingleValue('SELECT note_tree_id FROM notes_tree WHERE note_id = ?', [noteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await exportNote(noteTreeId, completeExportDir); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 00:10:43 -05:00
										 |  |  | async function exportNote(noteTreeId, dir) { | 
					
						
							|  |  |  |     const noteTree = await sql.getSingleResult("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]); | 
					
						
							|  |  |  |     const note = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteTree.note_id]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |     const pos = (noteTree.note_position + '').padStart(4, '0'); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 09:19:48 -05:00
										 |  |  |     fs.writeFileSync(dir + '/' + pos + '-' + note.note_title + '.html', html.prettyPrint(note.note_text, {indent_size: 2})); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |     const children = await sql.getResults("SELECT * FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0", [note.note_id]); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (children.length > 0) { | 
					
						
							| 
									
										
										
										
											2017-12-03 00:10:43 -05:00
										 |  |  |         const childrenDir = dir + '/' + pos + '-' + note.note_title; | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         fs.mkdirSync(childrenDir); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const child of children) { | 
					
						
							| 
									
										
										
										
											2017-12-03 00:10:43 -05:00
										 |  |  |             await exportNote(child.note_tree_id, childrenDir); | 
					
						
							| 
									
										
										
										
											2017-12-02 21:48:22 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = router; |