| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const html = require('html'); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | const repository = require('../repository'); | 
					
						
							| 
									
										
										
										
											2018-11-24 20:58:38 +01:00
										 |  |  | const tar = require('tar-stream'); | 
					
						
							| 
									
										
										
										
											2018-11-30 22:43:03 +01:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  | const mimeTypes = require('mime-types'); | 
					
						
							|  |  |  | const TurndownService = require('turndown'); | 
					
						
							| 
									
										
										
										
											2018-11-27 10:31:55 +01:00
										 |  |  | const packageInfo = require('../../../package.json'); | 
					
						
							| 
									
										
										
										
											2019-01-13 10:22:17 +01:00
										 |  |  | const utils = require('../utils'); | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param format - 'html' or 'markdown' | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | async function exportToTar(branch, format, res) { | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     let turndownService = format === 'markdown' ? new TurndownService() : null; | 
					
						
							| 
									
										
										
										
											2018-11-25 10:26:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-24 20:58:38 +01:00
										 |  |  |     const pack = tar.pack(); | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:38:09 +01:00
										 |  |  |     const noteIdToMeta = {}; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     function getUniqueFilename(existingFileNames, fileName) { | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  |         const lcFileName = fileName.toLowerCase(); | 
					
						
							| 
									
										
										
										
											2018-11-25 10:26:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         if (lcFileName in existingFileNames) { | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  |             let index; | 
					
						
							|  |  |  |             let newName; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             do { | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |                 index = existingFileNames[lcFileName]++; | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 newName = lcFileName + "_" + index; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             while (newName in existingFileNames); | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return fileName + "_" + index; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             existingFileNames[lcFileName] = 1; | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return fileName; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-25 10:26:45 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     function getDataFileName(note, baseFileName, existingFileNames) { | 
					
						
							|  |  |  |         let extension; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (note.type === 'text' && format === 'markdown') { | 
					
						
							|  |  |  |             extension = 'md'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-30 22:43:03 +01:00
										 |  |  |         else if (note.mime === 'application/x-javascript' || note.mime === 'text/javascript') { | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             extension = 'js'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             extension = mimeTypes.extension(note.mime) || "dat"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let fileName = baseFileName; | 
					
						
							| 
									
										
										
										
											2018-11-30 22:43:03 +01:00
										 |  |  |         const existingExtension = path.extname(fileName).toLowerCase(); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-30 22:43:03 +01:00
										 |  |  |         // if the note is already named with extension (e.g. "jquery.js"), then it's silly to append exact same extension again
 | 
					
						
							|  |  |  |         if (existingExtension !== extension) { | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             fileName += "." + extension; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return getUniqueFilename(existingFileNames, fileName); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function getNote(branch, existingFileNames) { | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |         const note = await branch.getNote(); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (await note.hasLabel('excludeFromExport')) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const baseFileName = branch.prefix ? (branch.prefix + ' - ' + note.title) : note.title; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:38:09 +01:00
										 |  |  |         if (note.noteId in noteIdToMeta) { | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             const sanitizedFileName = sanitize(baseFileName + ".clone"); | 
					
						
							|  |  |  |             const fileName = getUniqueFilename(existingFileNames, sanitizedFileName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return { | 
					
						
							| 
									
										
										
										
											2018-11-26 14:47:46 +01:00
										 |  |  |                 isClone: true, | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |                 noteId: note.noteId, | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |                 prefix: branch.prefix, | 
					
						
							|  |  |  |                 dataFileName: fileName | 
					
						
							|  |  |  |             }; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         const meta = { | 
					
						
							| 
									
										
										
										
											2018-11-26 14:47:46 +01:00
										 |  |  |             isClone: false, | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |             noteId: note.noteId, | 
					
						
							|  |  |  |             title: note.title, | 
					
						
							| 
									
										
										
										
											2018-11-26 23:39:43 +01:00
										 |  |  |             notePosition: branch.notePosition, | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |             prefix: branch.prefix, | 
					
						
							|  |  |  |             isExpanded: branch.isExpanded, | 
					
						
							|  |  |  |             type: note.type, | 
					
						
							|  |  |  |             mime: note.mime, | 
					
						
							|  |  |  |             // we don't export dateCreated and dateModified of any entity since that would be a bit misleading
 | 
					
						
							|  |  |  |             attributes: (await note.getOwnedAttributes()).map(attribute => { | 
					
						
							|  |  |  |                 return { | 
					
						
							|  |  |  |                     type: attribute.type, | 
					
						
							|  |  |  |                     name: attribute.name, | 
					
						
							|  |  |  |                     value: attribute.value, | 
					
						
							|  |  |  |                     isInheritable: attribute.isInheritable, | 
					
						
							|  |  |  |                     position: attribute.position | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  |             }), | 
					
						
							|  |  |  |             links: (await note.getLinks()).map(link => { | 
					
						
							|  |  |  |                 return { | 
					
						
							|  |  |  |                     type: link.type, | 
					
						
							|  |  |  |                     targetNoteId: link.targetNoteId | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  |         if (note.type === 'text') { | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             meta.format = format; | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:38:09 +01:00
										 |  |  |         noteIdToMeta[note.noteId] = meta; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const childBranches = await note.getChildBranches(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         // if it's a leaf then we'll export it even if it's empty
 | 
					
						
							|  |  |  |         if (note.content.length > 0 || childBranches.length === 0) { | 
					
						
							|  |  |  |             meta.dataFileName = getDataFileName(note, baseFileName, existingFileNames); | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         if (childBranches.length > 0) { | 
					
						
							|  |  |  |             meta.dirFileName = getUniqueFilename(existingFileNames, baseFileName); | 
					
						
							|  |  |  |             meta.children = []; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 22:22:16 +01:00
										 |  |  |             // namespace is shared by children in the same note
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             const childExistingNames = {}; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             for (const childBranch of childBranches) { | 
					
						
							| 
									
										
										
										
											2018-11-26 22:22:16 +01:00
										 |  |  |                 const note = await getNote(childBranch, childExistingNames); | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |                 // can be undefined if export is disabled for this note
 | 
					
						
							|  |  |  |                 if (note) { | 
					
						
							|  |  |  |                     meta.children.push(note); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         return meta; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function prepareContent(note, format) { | 
					
						
							|  |  |  |         if (format === 'html') { | 
					
						
							|  |  |  |             return html.prettyPrint(note.content, {indent_size: 2}); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (format === 'markdown') { | 
					
						
							|  |  |  |             return turndownService.turndown(note.content); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             return note.content; | 
					
						
							| 
									
										
										
										
											2018-11-25 10:26:45 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     // noteId => file path
 | 
					
						
							|  |  |  |     const notePaths = {}; | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     async function saveNote(noteMeta, path) { | 
					
						
							| 
									
										
										
										
											2018-11-26 14:47:46 +01:00
										 |  |  |         if (noteMeta.isClone) { | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             const content = "Note is present at " + notePaths[noteMeta.noteId]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 22:22:16 +01:00
										 |  |  |             pack.entry({name: path + noteMeta.dataFileName, size: content.length}, content); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         const note = await repository.getNote(noteMeta.noteId); | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 22:22:16 +01:00
										 |  |  |         notePaths[note.noteId] = path + (noteMeta.dataFileName || noteMeta.dirFileName); | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         if (noteMeta.dataFileName) { | 
					
						
							|  |  |  |             const content = prepareContent(note, noteMeta.format); | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 22:22:16 +01:00
										 |  |  |             pack.entry({name: path + noteMeta.dataFileName, size: content.length}, content); | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         if (noteMeta.children && noteMeta.children.length > 0) { | 
					
						
							| 
									
										
										
										
											2018-11-26 22:22:16 +01:00
										 |  |  |             const directoryPath = path + noteMeta.dirFileName; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             pack.entry({name: directoryPath, type: 'directory'}); | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             for (const childMeta of noteMeta.children) { | 
					
						
							| 
									
										
										
										
											2018-11-26 22:22:16 +01:00
										 |  |  |                 await saveNote(childMeta, directoryPath + '/'); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     const metaFile = { | 
					
						
							| 
									
										
										
										
											2018-11-27 10:31:55 +01:00
										 |  |  |         formatVersion: 1, | 
					
						
							|  |  |  |         appVersion: packageInfo.version, | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         files: [ | 
					
						
							|  |  |  |             await getNote(branch, []) | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:38:09 +01:00
										 |  |  |     for (const noteMeta of Object.values(noteIdToMeta)) { | 
					
						
							|  |  |  |         // filter out relations and links which are not inside this export
 | 
					
						
							|  |  |  |         noteMeta.attributes = noteMeta.attributes.filter(attr => attr.type !== 'relation' || attr.value in noteIdToMeta); | 
					
						
							|  |  |  |         noteMeta.links = noteMeta.links.filter(link => link.targetNoteId in noteIdToMeta); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     if (!metaFile.files[0]) { // corner case of disabled export for exported note
 | 
					
						
							|  |  |  |         res.sendStatus(400); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     const metaFileJson = JSON.stringify(metaFile, null, '\t'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pack.entry({name: "!!!meta.json", size: metaFileJson.length}, metaFileJson); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await saveNote(metaFile.files[0], ''); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |     pack.finalize(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     const note = await branch.getNote(); | 
					
						
							| 
									
										
										
										
											2019-01-13 10:22:17 +01:00
										 |  |  |     const tarFileName = (branch.prefix ? (branch.prefix + " - ") : "") + note.title + ".tar"; | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 10:22:17 +01:00
										 |  |  |     res.setHeader('Content-Disposition', utils.getContentDisposition(tarFileName)); | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |     res.setHeader('Content-Type', 'application/tar'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pack.pipe(res); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     exportToTar | 
					
						
							|  |  |  | }; |