| 
									
										
										
										
											2017-12-02 23:41:18 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  | const repository = require('../../services/repository'); | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | const enexImportService = require('../../services/import/enex'); | 
					
						
							|  |  |  | const opmlImportService = require('../../services/import/opml'); | 
					
						
							|  |  |  | const tarImportService = require('../../services/import/tar'); | 
					
						
							| 
									
										
										
										
											2018-11-26 23:47:02 +01:00
										 |  |  | const singleImportService = require('../../services/import/single'); | 
					
						
							| 
									
										
										
										
											2018-11-26 22:27:57 +01:00
										 |  |  | const cls = require('../../services/cls'); | 
					
						
							| 
									
										
										
										
											2018-02-26 00:07:43 -05:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:32:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | async function importToBranch(req) { | 
					
						
							|  |  |  |     const parentNoteId = req.params.parentNoteId; | 
					
						
							|  |  |  |     const file = req.file; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-10 23:41:11 +02:00
										 |  |  |     if (!file) { | 
					
						
							| 
									
										
										
										
											2018-09-03 21:06:24 +02:00
										 |  |  |         return [400, "No file has been uploaded"]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-29 20:32:13 -04:00
										 |  |  |     const parentNote = await repository.getNote(parentNoteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!parentNote) { | 
					
						
							|  |  |  |         return [404, `Note ${parentNoteId} doesn't exist.`]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const extension = path.extname(file.originalname).toLowerCase(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 22:27:57 +01:00
										 |  |  |     // running all the event handlers on imported notes (and attributes) is slow
 | 
					
						
							|  |  |  |     // and may produce unintended consequences
 | 
					
						
							|  |  |  |     cls.disableEntityEvents(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-29 20:32:13 -04:00
										 |  |  |     if (extension === '.tar') { | 
					
						
							| 
									
										
										
										
											2018-11-16 14:36:50 +01:00
										 |  |  |         return await tarImportService.importTar(file.buffer, parentNote); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:32:13 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (extension === '.opml') { | 
					
						
							| 
									
										
										
										
											2018-11-16 14:36:50 +01:00
										 |  |  |         return await opmlImportService.importOpml(file.buffer, parentNote); | 
					
						
							| 
									
										
										
										
											2018-05-29 20:32:13 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-03 13:40:40 +02:00
										 |  |  |     else if (extension === '.md') { | 
					
						
							| 
									
										
										
										
											2018-11-26 23:47:02 +01:00
										 |  |  |         return await singleImportService.importMarkdown(file, parentNote); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (extension === '.html' || extension === '.htm') { | 
					
						
							|  |  |  |         return await singleImportService.importHtml(file, parentNote); | 
					
						
							| 
									
										
										
										
											2018-11-05 00:06:17 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (extension === '.enex') { | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |         return await enexImportService.importEnex(file, parentNote); | 
					
						
							| 
									
										
										
										
											2018-09-03 13:40:40 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-29 20:32:13 -04:00
										 |  |  |     else { | 
					
						
							|  |  |  |         return [400, `Unrecognized extension ${extension}, must be .tar or .opml`]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 15:34:07 -04:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2018-05-29 20:32:13 -04:00
										 |  |  |     importToBranch | 
					
						
							| 
									
										
										
										
											2018-03-30 15:34:07 -04:00
										 |  |  | }; |