mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 13:01:31 +08:00 
			
		
		
		
	note import from directory
This commit is contained in:
		
							parent
							
								
									119d085856
								
							
						
					
					
						commit
						cd3c6d7e3b
					
				
							
								
								
									
										86
									
								
								routes/api/import.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										86
									
								
								routes/api/import.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,86 @@ | |||||||
|  | "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'); | ||||||
|  | const utils = require('../../services/utils'); | ||||||
|  | const sync_table = require('../../services/sync_table'); | ||||||
|  | 
 | ||||||
|  | router.get('/:directory/to/:parentNoteId', async (req, res, next) => { | ||||||
|  |     const directory = req.params.directory.replace(/[^0-9a-zA-Z_-]/gi, ''); | ||||||
|  |     const parentNoteId = req.params.parentNoteId; | ||||||
|  | 
 | ||||||
|  |     const dir = data_dir.EXPORT_DIR + '/' + directory; | ||||||
|  | 
 | ||||||
|  |     await sql.doInTransaction(async () => await importNotes(dir, parentNoteId)); | ||||||
|  | 
 | ||||||
|  |     res.send({}); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | async function importNotes(dir, parentNoteId) { | ||||||
|  |     const parent = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [parentNoteId]); | ||||||
|  | 
 | ||||||
|  |     if (!parent) { | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     const fileList = fs.readdirSync(dir); | ||||||
|  | 
 | ||||||
|  |     for (const file of fileList) { | ||||||
|  |         const path = dir + '/' + file; | ||||||
|  | 
 | ||||||
|  |         if (fs.lstatSync(path).isDirectory()) { | ||||||
|  |             continue; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         if (!file.endsWith('.html')) { | ||||||
|  |             continue; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         const noteTitle = file.substr(0, file.length - 5); | ||||||
|  |         const noteText = fs.readFileSync(path, "utf8"); | ||||||
|  | 
 | ||||||
|  |         let maxPos = await sql.getSingleValue("SELECT MAX(note_pos) FROM notes_tree WHERE note_pid = ? AND is_deleted = 0", [parentNoteId]); | ||||||
|  |         if (!maxPos) { | ||||||
|  |             maxPos = 1; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         const noteId = utils.newNoteId(); | ||||||
|  |         const noteTreeId = utils.newNoteHistoryId(); | ||||||
|  | 
 | ||||||
|  |         await sql.insert('notes_tree', { | ||||||
|  |             note_tree_id: noteTreeId, | ||||||
|  |             note_id: noteId, | ||||||
|  |             note_pid: parentNoteId, | ||||||
|  |             note_pos: maxPos + 1, | ||||||
|  |             is_expanded: 0, | ||||||
|  |             is_deleted: 0, | ||||||
|  |             date_modified: utils.nowTimestamp() | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         await sync_table.addNoteTreeSync(noteTreeId); | ||||||
|  | 
 | ||||||
|  |         await sql.insert('notes', { | ||||||
|  |             note_id: noteId, | ||||||
|  |             note_title: noteTitle, | ||||||
|  |             note_text: noteText, | ||||||
|  |             is_deleted: 0, | ||||||
|  |             is_protected: 0, | ||||||
|  |             date_created: utils.nowTimestamp(), | ||||||
|  |             date_modified: utils.nowTimestamp() | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         await sync_table.addNoteSync(noteId); | ||||||
|  | 
 | ||||||
|  |         const noteDir = dir + '/' + noteTitle; | ||||||
|  | 
 | ||||||
|  |         if (fs.existsSync(noteDir) && fs.lstatSync(noteDir).isDirectory()) { | ||||||
|  |             await importNotes(noteDir, noteId); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | module.exports = router; | ||||||
| @ -18,6 +18,7 @@ const eventLogRoute = require('./api/event_log'); | |||||||
| const recentNotesRoute = require('./api/recent_notes'); | const recentNotesRoute = require('./api/recent_notes'); | ||||||
| const appInfoRoute = require('./api/app_info'); | const appInfoRoute = require('./api/app_info'); | ||||||
| const exportRoute = require('./api/export'); | const exportRoute = require('./api/export'); | ||||||
|  | const importRoute = require('./api/import'); | ||||||
| 
 | 
 | ||||||
| function register(app) { | function register(app) { | ||||||
|     app.use('/', indexRoute); |     app.use('/', indexRoute); | ||||||
| @ -39,6 +40,7 @@ function register(app) { | |||||||
|     app.use('/api/recent-notes', recentNotesRoute); |     app.use('/api/recent-notes', recentNotesRoute); | ||||||
|     app.use('/api/app-info', appInfoRoute); |     app.use('/api/app-info', appInfoRoute); | ||||||
|     app.use('/api/export', exportRoute); |     app.use('/api/export', exportRoute); | ||||||
|  |     app.use('/api/import', importRoute); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| module.exports = { | module.exports = { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 azivner
						azivner