You've already forked joplin
							
							
				mirror of
				https://github.com/laurent22/joplin.git
				synced 2025-10-31 00:07:48 +02:00 
			
		
		
		
	All: Improved file and memory drivers to make it easier to use same sync method for all the drivers
This commit is contained in:
		| @@ -151,12 +151,6 @@ class FileApiDriverLocal { | ||||
| 			} else { | ||||
| 				output = await fs.readFile(path, options.encoding); | ||||
| 			} | ||||
|  | ||||
| 			// if (options.encoding == 'binary') { | ||||
| 			// 	output = await fs.readFile(path); | ||||
| 			// } else { | ||||
| 			// 	output = await fs.readFile(path, options.encoding); | ||||
| 			// } | ||||
| 		} catch (error) { | ||||
| 			if (error.code == 'ENOENT') return null; | ||||
| 			throw this.fsErrorToJsError_(error); | ||||
| @@ -184,7 +178,11 @@ class FileApiDriverLocal { | ||||
| 		}); | ||||
| 	} | ||||
|  | ||||
| 	put(path, content) { | ||||
| 	async put(path, content, options = null) { | ||||
| 		if (!options) options = {}; | ||||
|  | ||||
| 		if (options.source === 'file') content = await fs.readFile(options.path); | ||||
|  | ||||
| 		return new Promise((resolve, reject) => { | ||||
| 			fs.writeFile(path, content, function(error) { | ||||
| 				if (error) { | ||||
|   | ||||
| @@ -1,4 +1,5 @@ | ||||
| const { time } = require('lib/time-utils.js'); | ||||
| const fs = require('fs-extra'); | ||||
|  | ||||
| class FileApiDriverMemory { | ||||
|  | ||||
| @@ -7,6 +8,18 @@ class FileApiDriverMemory { | ||||
| 		this.deletedItems_ = []; | ||||
| 	} | ||||
|  | ||||
| 	encodeContent_(content) { | ||||
| 		if (content instanceof Buffer) { | ||||
| 			return content.toString('base64'); | ||||
| 		} else { | ||||
| 			return Buffer.from(content).toString('base64'); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	decodeContent_(content) { | ||||
| 		return Buffer.from(content, 'base64').toString('ascii'); | ||||
| 	} | ||||
|  | ||||
| 	itemIndexByPath(path) { | ||||
| 		for (let i = 0; i < this.items_.length; i++) { | ||||
| 			if (this.items_[i].path == path) return i; | ||||
| @@ -65,11 +78,20 @@ class FileApiDriverMemory { | ||||
| 		}); | ||||
| 	} | ||||
|  | ||||
| 	get(path) { | ||||
| 	async get(path, options) { | ||||
| 		let item = this.itemByPath(path); | ||||
| 		if (!item) return Promise.resolve(null); | ||||
| 		if (item.isDir) return Promise.reject(new Error(path + ' is a directory, not a file')); | ||||
| 		return Promise.resolve(item.content); | ||||
|  | ||||
| 		let output = null; | ||||
| 		if (options.target === 'file') { | ||||
| 			await fs.writeFile(options.path, Buffer.from(item.content, 'base64')); | ||||
| 		} else { | ||||
| 			const content = this.decodeContent_(item.content); | ||||
| 			output = Promise.resolve(content); | ||||
| 		} | ||||
|  | ||||
| 		return output; | ||||
| 	} | ||||
|  | ||||
| 	mkdir(path) { | ||||
| @@ -79,14 +101,18 @@ class FileApiDriverMemory { | ||||
| 		return Promise.resolve(); | ||||
| 	} | ||||
|  | ||||
| 	put(path, content) { | ||||
| 	async put(path, content, options = null) { | ||||
| 		if (!options) options = {}; | ||||
|  | ||||
| 		if (options.source === 'file') content = await fs.readFile(options.path); | ||||
|  | ||||
| 		let index = this.itemIndexByPath(path); | ||||
| 		if (index < 0) { | ||||
| 			let item = this.newItem(path, false); | ||||
| 			item.content = content; | ||||
| 			item.content = this.encodeContent_(content); | ||||
| 			this.items_.push(item); | ||||
| 		} else { | ||||
| 			this.items_[index].content = content; | ||||
| 			this.items_[index].content = this.encodeContent_(content); | ||||
| 			this.items_[index].updated_time = time.unix(); | ||||
| 		} | ||||
| 		return Promise.resolve(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user