| 
									
										
										
										
											2014-11-12 05:48:26 +03:00
										 |  |  | /********************************************************************** | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | **********************************************************************/ | 
					
						
							| 
									
										
										
										
											2016-08-21 02:19:24 +03:00
										 |  |  | ((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define) | 
					
						
							| 
									
										
										
										
											2016-09-03 06:40:44 +03:00
										 |  |  | (function(require){ var module={} // make module AMD/node compatible...
 | 
					
						
							| 
									
										
										
										
											2016-08-20 22:49:36 +03:00
										 |  |  | /*********************************************************************/ | 
					
						
							| 
									
										
										
										
											2014-11-14 21:57:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-28 16:29:57 +03:00
										 |  |  | if(typeof(process) != 'undefined'){ | 
					
						
							|  |  |  | 	var pathlib = requirejs('path') | 
					
						
							|  |  |  | 	var events = requirejs('events') | 
					
						
							| 
									
										
										
										
											2014-11-14 21:57:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-28 16:29:57 +03:00
										 |  |  | 	var fse = requirejs('fs-extra') | 
					
						
							| 
									
										
										
										
											2017-02-07 18:58:37 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-28 16:29:57 +03:00
										 |  |  | 	var glob = requirejs('glob') | 
					
						
							| 
									
										
										
										
											2017-02-07 18:58:37 +03:00
										 |  |  | 	var wglob = requirejs('wildglob') | 
					
						
							| 
									
										
										
										
											2014-11-12 05:48:26 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-28 16:29:57 +03:00
										 |  |  | 	var guaranteeEvents = requirejs('guarantee-events') | 
					
						
							| 
									
										
										
										
											2015-12-05 01:19:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-28 16:29:57 +03:00
										 |  |  | } else { | 
					
						
							|  |  |  | 	return module | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-12 05:48:26 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-01 16:12:10 +03:00
										 |  |  | var data = require('imagegrid/data') | 
					
						
							|  |  |  | var images = require('imagegrid/images') | 
					
						
							| 
									
										
										
										
											2015-11-15 01:25:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-29 23:49:01 +03:00
										 |  |  | var util = require('lib/util') | 
					
						
							| 
									
										
										
										
											2014-11-14 21:57:55 +03:00
										 |  |  | var tasks = require('lib/tasks') | 
					
						
							| 
									
										
										
										
											2014-11-12 05:48:26 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 21:57:55 +03:00
										 |  |  | /*********************************************************************/ | 
					
						
							| 
									
										
										
										
											2014-11-12 05:48:26 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 21:57:55 +03:00
										 |  |  | var INDEX_DIR = '.ImageGrid' | 
					
						
							| 
									
										
										
										
											2014-11-12 05:48:26 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 23:49:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 05:48:26 +03:00
										 |  |  | /*********************************************************************/ | 
					
						
							| 
									
										
										
										
											2014-12-28 23:49:42 +03:00
										 |  |  | // helpers...
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-13 01:09:58 +03:00
										 |  |  | // Skip nested indexes from tree...
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2016-05-12 07:56:16 +03:00
										 |  |  | var skipNested = function(paths, index_dir, logger){ | 
					
						
							| 
									
										
										
										
											2016-11-04 23:00:34 +03:00
										 |  |  | 	logger = logger && logger.push('Skipping nested') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-12 07:56:16 +03:00
										 |  |  | 	paths = paths | 
					
						
							|  |  |  | 		.map(function(p){ return p.split(index_dir).shift() }) | 
					
						
							|  |  |  | 		.sort(function(a, b){ return a.length - b.length }) | 
					
						
							|  |  |  | 	for(var i=0; i < paths.length; i++){ | 
					
						
							|  |  |  | 		var p = paths[i] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(p == null){ | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for(var j=i+1; j < paths.length; j++){ | 
					
						
							|  |  |  | 			var o = paths[j].split(p) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if(o[0] == '' && o.length > 1){ | 
					
						
							|  |  |  | 				logger && logger.emit('skipping', paths[j]) | 
					
						
							|  |  |  | 				delete paths[j] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return paths | 
					
						
							|  |  |  | 		.filter(function(p){ return !!p }) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 23:49:42 +03:00
										 |  |  | // Guarantee that the 'end' and 'match' handlers will always get called 
 | 
					
						
							|  |  |  | // with all results at least once...
 | 
					
						
							| 
									
										
										
										
											2014-12-16 04:50:34 +03:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-12-28 23:49:42 +03:00
										 |  |  | // This does two things:
 | 
					
						
							|  |  |  | // 	- every 'end' event handler will get the full result set, regardless
 | 
					
						
							|  |  |  | // 		of when it was set...
 | 
					
						
							|  |  |  | // 	- every 'match' handler will be called for every match found, again
 | 
					
						
							|  |  |  | // 		regardless of whether it was set before or after the time of 
 | 
					
						
							|  |  |  | // 		match.
 | 
					
						
							| 
									
										
										
										
											2014-12-16 04:50:34 +03:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-12-28 23:49:42 +03:00
										 |  |  | // This prevents handlers from missing the event they are waiting for, 
 | 
					
						
							|  |  |  | // essentially making it similar to how Promise/Deferred handle their
 | 
					
						
							|  |  |  | // callbacks.
 | 
					
						
							| 
									
										
										
										
											2014-12-12 03:56:58 +03:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-12-28 20:46:06 +03:00
										 |  |  | var guaranteeGlobEvents = | 
					
						
							|  |  |  | module.guaranteeGlobEvents = | 
					
						
							| 
									
										
										
										
											2014-12-29 18:47:27 +03:00
										 |  |  | function(glob){ return guaranteeEvents('match end', glob) } | 
					
						
							| 
									
										
										
										
											2014-12-28 20:46:06 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-16 17:00:56 +03:00
										 |  |  | var gGlob =  | 
					
						
							|  |  |  | module.gGlob = function(){ | 
					
						
							| 
									
										
										
										
											2016-05-13 01:09:58 +03:00
										 |  |  | 	return guaranteeGlobEvents(glob.apply(null, arguments)) } | 
					
						
							| 
									
										
										
										
											2014-12-28 23:49:42 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-07 18:58:37 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 23:49:42 +03:00
										 |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | // Reader...
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-13 01:09:58 +03:00
										 |  |  | // XXX would be nice to find a way to stop searching a sub tree as soon
 | 
					
						
							|  |  |  | // 		as a match is found.
 | 
					
						
							|  |  |  | // 		...this would be allot faster than getting the whole tree and 
 | 
					
						
							|  |  |  | // 		then pruning through like it is done now...
 | 
					
						
							| 
									
										
										
										
											2015-05-21 04:00:28 +03:00
										 |  |  | var listIndexes = | 
					
						
							|  |  |  | module.listIndexes =  | 
					
						
							| 
									
										
										
										
											2015-12-05 01:19:06 +03:00
										 |  |  | function(base, index_dir){ | 
					
						
							| 
									
										
										
										
											2020-07-11 18:21:06 +03:00
										 |  |  | 	return gGlob(base +'/**/'+ (index_dir || INDEX_DIR), {strict: false}) } | 
					
						
							| 
									
										
										
										
											2014-11-12 05:48:26 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-13 00:59:56 +03:00
										 |  |  | // NOTE: this is similar to listIndexes(..) but will return a promise and
 | 
					
						
							|  |  |  | // 		skip all non-loadable nested indexes...
 | 
					
						
							|  |  |  | var getIndexes = | 
					
						
							|  |  |  | module.getIndexes =  | 
					
						
							|  |  |  | function(base, index_dir, logger){ | 
					
						
							| 
									
										
										
										
											2016-11-04 23:00:34 +03:00
										 |  |  | 	logger = logger && logger.push('Searching') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-13 00:59:56 +03:00
										 |  |  | 	return new Promise(function(resolve, reject){ | 
					
						
							|  |  |  | 		listIndexes(base, index_dir) | 
					
						
							|  |  |  | 			.on('error', function(err){ | 
					
						
							|  |  |  | 				reject(err) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2016-05-13 01:09:58 +03:00
										 |  |  | 			.on('match', function(path){ | 
					
						
							|  |  |  | 				logger && logger.emit('found', path) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2016-05-13 00:59:56 +03:00
										 |  |  | 			.on('end', function(paths){ | 
					
						
							|  |  |  | 				// skip nested indexes...
 | 
					
						
							|  |  |  | 				resolve(skipNested(paths, index_dir, logger)) | 
					
						
							| 
									
										
										
										
											2020-07-11 18:21:06 +03:00
										 |  |  | 			}) }) } | 
					
						
							| 
									
										
										
										
											2016-05-13 00:59:56 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-21 04:00:28 +03:00
										 |  |  | var listPreviews = | 
					
						
							|  |  |  | module.listPreviews =  | 
					
						
							| 
									
										
										
										
											2016-06-03 04:46:51 +03:00
										 |  |  | function(base, img_pattern){ | 
					
						
							|  |  |  | 	//return gGlob(base +'/*px/*jpg')
 | 
					
						
							| 
									
										
										
										
											2020-07-11 18:21:06 +03:00
										 |  |  | 	return gGlob(base +'/*px/'+(img_pattern || '*')+'.jpg', {strict: false}) } | 
					
						
							| 
									
										
										
										
											2015-05-21 04:00:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 20:46:06 +03:00
										 |  |  | // XXX return a promise rather than an event emitter (???)
 | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | var listJSON = | 
					
						
							|  |  |  | module.listJSON = | 
					
						
							|  |  |  | function(path, pattern){ | 
					
						
							| 
									
										
										
										
											2014-12-16 04:50:34 +03:00
										 |  |  | 	pattern = pattern || '*' | 
					
						
							| 
									
										
										
										
											2016-05-29 23:49:01 +03:00
										 |  |  | 	path = util.normalizePath(path) | 
					
						
							| 
									
										
										
										
											2020-07-11 18:21:06 +03:00
										 |  |  | 	return gGlob(path +'/'+ pattern +'.json', {strict: false}) } | 
					
						
							| 
									
										
										
										
											2014-12-16 04:50:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-15 23:32:33 +03:00
										 |  |  | // wrap a node style callback function into a Promise...
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // NOTE: this is inspired by the promise module for node this stopped 
 | 
					
						
							|  |  |  | // 		working, and porting one method was simpler that trying to get
 | 
					
						
							|  |  |  | // 		to the bottom of the issue, especially with native promises...
 | 
					
						
							| 
									
										
										
										
											2016-04-15 02:09:02 +03:00
										 |  |  | // XXX move to someplace generic...
 | 
					
						
							| 
									
										
										
										
											2016-04-15 23:32:33 +03:00
										 |  |  | var denodeify =  | 
					
						
							|  |  |  | module.denodeify = | 
					
						
							|  |  |  | function(func){ | 
					
						
							| 
									
										
										
										
											2016-04-18 01:52:09 +03:00
										 |  |  | 	var that = this | 
					
						
							| 
									
										
										
										
											2016-04-15 02:09:02 +03:00
										 |  |  | 	return function(){ | 
					
						
							| 
									
										
										
										
											2018-11-12 23:04:00 +03:00
										 |  |  | 		var args = [...arguments] | 
					
						
							| 
									
										
										
										
											2016-04-15 02:09:02 +03:00
										 |  |  | 		return new Promise(function(resolve, reject){ | 
					
						
							| 
									
										
										
										
											2016-04-18 01:52:09 +03:00
										 |  |  | 			func.apply(that, args.concat([function(err, res){ | 
					
						
							| 
									
										
										
										
											2019-10-13 04:33:36 +03:00
										 |  |  | 				return err ? reject(err) : resolve(res) }])) }) } } | 
					
						
							| 
									
										
										
										
											2014-12-16 04:50:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-15 02:09:02 +03:00
										 |  |  | var loadFile = denodeify(fse.readFile) | 
					
						
							|  |  |  | var writeFile = denodeify(fse.writeFile) | 
					
						
							|  |  |  | var ensureDir = denodeify(fse.ensureDir) | 
					
						
							| 
									
										
										
										
											2014-12-28 20:46:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // XXX handle errors...
 | 
					
						
							| 
									
										
										
										
											2014-12-16 04:50:34 +03:00
										 |  |  | function loadJSON(path){ | 
					
						
							| 
									
										
										
										
											2016-05-29 23:49:01 +03:00
										 |  |  | 	path = util.normalizePath(path) | 
					
						
							| 
									
										
										
										
											2014-12-28 03:47:44 +03:00
										 |  |  | 	return loadFile(path).then(JSON.parse) | 
					
						
							| 
									
										
										
										
											2014-12-16 04:50:34 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | // Format:
 | 
					
						
							|  |  |  | // 	{
 | 
					
						
							|  |  |  | // 		<date>: [
 | 
					
						
							|  |  |  | // 			<path>,
 | 
					
						
							|  |  |  | // 			...
 | 
					
						
							|  |  |  | // 		],
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // 		...
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // 		// root files -- no date...
 | 
					
						
							|  |  |  | // 		root: [
 | 
					
						
							|  |  |  | // 			<path>,
 | 
					
						
							|  |  |  | // 			...
 | 
					
						
							|  |  |  | // 		]
 | 
					
						
							|  |  |  | // 	}
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2016-05-04 17:25:15 +03:00
										 |  |  | // NOTE: this does not use the fs...
 | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | var groupByDate =  | 
					
						
							|  |  |  | module.groupByDate =  | 
					
						
							|  |  |  | function(list){ | 
					
						
							|  |  |  | 	var res = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	list | 
					
						
							|  |  |  | 		.forEach(function(n){ | 
					
						
							|  |  |  | 			var b = pathlib.basename(n) | 
					
						
							|  |  |  | 			var s = b.split(/[-.]/g).slice(0, -1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// no date set...
 | 
					
						
							|  |  |  | 			if(s.length == 1){ | 
					
						
							|  |  |  | 				res.root = res.root || [] | 
					
						
							|  |  |  | 				res.root.push(n) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				res[s[0]] = res[s[0]] || [] | 
					
						
							|  |  |  | 				res[s[0]].push(n) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return res | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 17:25:15 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | // Group file list by keyword...
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // this will build a structure in the following format:
 | 
					
						
							|  |  |  | // 	{
 | 
					
						
							|  |  |  | // 		<keyword>: [
 | 
					
						
							|  |  |  | // 			// diff files...
 | 
					
						
							|  |  |  | // 			// NOTE: the first argument indicates 
 | 
					
						
							|  |  |  | // 			//	if this is a diff or not, used to 
 | 
					
						
							|  |  |  | // 			//	skip past the last base...
 | 
					
						
							|  |  |  | // 			[true, <filename>],
 | 
					
						
							|  |  |  | // 			...
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // 			// base file (non-diff)
 | 
					
						
							|  |  |  | // 			[false, <filename>]
 | 
					
						
							|  |  |  | // 		],
 | 
					
						
							|  |  |  | // 		...
 | 
					
						
							|  |  |  | // 	}
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This is used to sequence, load and correctly merge 
 | 
					
						
							|  |  |  | // the found JSON files.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // NOTE: all files past the first non-diff are skipped.
 | 
					
						
							| 
									
										
										
										
											2016-05-04 17:25:15 +03:00
										 |  |  | // NOTE: this does not use the fs...
 | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | var groupByKeyword =  | 
					
						
							|  |  |  | module.groupByKeyword =  | 
					
						
							|  |  |  | function(list, from_date, logger){ | 
					
						
							| 
									
										
										
										
											2020-07-09 02:19:43 +03:00
										 |  |  | 	//logger = logger && logger.push('Grouping by keyword')
 | 
					
						
							| 
									
										
										
										
											2016-11-04 23:00:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 	var index = {} | 
					
						
							|  |  |  | 	var queued = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var dates = groupByDate(list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Object.keys(dates) | 
					
						
							|  |  |  | 		.sort() | 
					
						
							|  |  |  | 		.reverse() | 
					
						
							|  |  |  | 		// skip dates before from_date...
 | 
					
						
							|  |  |  | 		// NOTE: from_date is included...
 | 
					
						
							| 
									
										
										
										
											2016-05-04 17:25:15 +03:00
										 |  |  | 		.filter(function(d){ return from_date ? d <= from_date || d == 'root' : true }) | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 		.forEach(function(d){ | 
					
						
							|  |  |  | 			dates[d] | 
					
						
							|  |  |  | 				.sort() | 
					
						
							|  |  |  | 				.reverse() | 
					
						
							|  |  |  | 				.forEach(function(n){ | 
					
						
							|  |  |  | 					var b = pathlib.basename(n) | 
					
						
							|  |  |  | 					var s = b.split(/[-.]/g).slice(0, -1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					// <timestamp>-<keyword>[-diff].json / diff / non-diff
 | 
					
						
							|  |  |  | 					// NOTE: all files without a timestamp are filtered out
 | 
					
						
							|  |  |  | 					// 		by groupByDate(..) into a .root section so we 
 | 
					
						
							|  |  |  | 					// 		do not need to worry about them here...
 | 
					
						
							|  |  |  | 					var k = s[1] | 
					
						
							|  |  |  | 					var d = s[2] == 'diff' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 13:11:54 +03:00
										 |  |  | 					k = d ? k : s.slice(1).join('-') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 					// new keyword...
 | 
					
						
							|  |  |  | 					if(index[k] == null){ | 
					
						
							|  |  |  | 						index[k] = [[d, n]] | 
					
						
							|  |  |  | 						logger && logger.emit('queued', n) | 
					
						
							|  |  |  | 						queued += 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					// do not add anything past the latest non-diff 
 | 
					
						
							|  |  |  | 					// for each keyword...
 | 
					
						
							|  |  |  | 					} else if(index[k].slice(-1)[0][0] == true){ | 
					
						
							|  |  |  | 						index[k].push([d, n]) | 
					
						
							|  |  |  | 						logger && logger.emit('queued', n) | 
					
						
							|  |  |  | 						queued += 1 | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-05-11 15:28:56 +03:00
										 |  |  | 				}) }); | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// add base files back where needed...
 | 
					
						
							|  |  |  | 	// <keyword>.json / non-diff
 | 
					
						
							| 
									
										
										
										
											2016-05-11 15:28:56 +03:00
										 |  |  | 	(dates.root || []) | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 		.forEach(function(n){ | 
					
						
							|  |  |  | 			var b = pathlib.basename(n) | 
					
						
							|  |  |  | 			var k = b.split(/\./g)[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// no diffs...
 | 
					
						
							|  |  |  | 			if(index[k] == null){ | 
					
						
							|  |  |  | 				index[k] = [[false, n]] | 
					
						
							|  |  |  | 				logger && logger.emit('queued', n) | 
					
						
							|  |  |  | 				queued += 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// add root file if no base is found...
 | 
					
						
							|  |  |  | 			} else if(index[k].slice(-1)[0][0] == true){ | 
					
						
							|  |  |  | 				index[k].push([false, n]) | 
					
						
							|  |  |  | 				logger && logger.emit('queued', n) | 
					
						
							|  |  |  | 				queued += 1 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// remove the flags...
 | 
					
						
							|  |  |  | 	for(var k in index){ | 
					
						
							| 
									
										
										
										
											2020-07-09 02:19:43 +03:00
										 |  |  | 		index[k] = index[k] | 
					
						
							|  |  |  | 			.map(function(e){ return e[1] }) } | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	logger && logger.emit('files-queued', queued, index) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return index | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 17:25:15 +03:00
										 |  |  | // Load index data listed by timestamp... 
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // NOTE: this returns data similar to groupByDate(..) but will replace 
 | 
					
						
							|  |  |  | // 		the 'root' key with a timestamp...
 | 
					
						
							|  |  |  | // NOTE: this will include the full history. this is different to what
 | 
					
						
							|  |  |  | // 		groupByKeyword(..) does.
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | // XXX handle errors....
 | 
					
						
							|  |  |  | var loadSaveHistoryList = | 
					
						
							|  |  |  | module.loadSaveHistoryList = | 
					
						
							| 
									
										
										
										
											2016-05-12 07:56:16 +03:00
										 |  |  | function(path, index_dir, logger){ | 
					
						
							| 
									
										
										
										
											2016-11-04 23:00:34 +03:00
										 |  |  | 	logger = logger && logger.push('Save history') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-29 23:49:01 +03:00
										 |  |  | 	path = util.normalizePath(path) | 
					
						
							| 
									
										
										
										
											2016-05-04 17:25:15 +03:00
										 |  |  | 	index_dir = index_dir || INDEX_DIR | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 	return new Promise(function(resolve, reject){ | 
					
						
							|  |  |  | 		// direct index...
 | 
					
						
							|  |  |  | 		if(pathlib.basename(path) == index_dir){ | 
					
						
							|  |  |  | 			listJSON(path) | 
					
						
							|  |  |  | 				// XXX handle errors...
 | 
					
						
							|  |  |  | 				.on('error', function(err){ | 
					
						
							|  |  |  | 					logger && logger.emit('error', err) | 
					
						
							| 
									
										
										
										
											2016-05-11 15:28:56 +03:00
										 |  |  | 					console.error(err) | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 				}) | 
					
						
							|  |  |  | 				.on('end', function(files){ | 
					
						
							| 
									
										
										
										
											2016-05-04 17:25:15 +03:00
										 |  |  | 					var data = groupByDate(files) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					// XXX should we mark the root timestamp in any way???
 | 
					
						
							|  |  |  | 					if('root' in data && data.root.length > 0){ | 
					
						
							|  |  |  | 						// XXX handle stat error...
 | 
					
						
							|  |  |  | 						data[fse.statSync(data.root[0]).birthtime.getTimeStamp()] = data.root | 
					
						
							|  |  |  | 						delete data.root | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 17:25:15 +03:00
										 |  |  | 					resolve(data) | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 				}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// need to locate indexes...
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			var res = {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-13 00:59:56 +03:00
										 |  |  | 			getIndexes(path, index_dir, logger) | 
					
						
							|  |  |  | 				.catch(function(err){ | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 					logger && logger.emit('error', err) | 
					
						
							| 
									
										
										
										
											2020-07-10 04:07:45 +03:00
										 |  |  | 					console.error(err) }) | 
					
						
							| 
									
										
										
										
											2016-05-13 00:59:56 +03:00
										 |  |  | 				.then(function(paths){ | 
					
						
							| 
									
										
										
										
											2016-05-12 07:56:16 +03:00
										 |  |  | 					// start loading...
 | 
					
						
							| 
									
										
										
										
											2020-07-10 04:07:45 +03:00
										 |  |  | 					return Promise | 
					
						
							|  |  |  | 						.all(paths.map(function(p){ | 
					
						
							|  |  |  | 							p = util.normalizePath(p) | 
					
						
							|  |  |  | 							//var path = pathlib.normalize(p +'/'+ index_dir) 
 | 
					
						
							|  |  |  | 							var path = util.normalizePath(p +'/'+ index_dir)  | 
					
						
							|  |  |  | 							return loadSaveHistoryList(path, index_dir) | 
					
						
							|  |  |  | 								.then(function(obj){  | 
					
						
							|  |  |  | 									// NOTE: considering that all the paths within
 | 
					
						
							|  |  |  | 									// 		the index are relative to the preview 
 | 
					
						
							|  |  |  | 									// 		dir (the parent dir to the index root)
 | 
					
						
							|  |  |  | 									// 		we do not need to include the index 
 | 
					
						
							|  |  |  | 									// 		itself in the base path...
 | 
					
						
							|  |  |  | 									res[p] = obj }) })) }) | 
					
						
							|  |  |  | 				.then(function(){ resolve(res) }) } }) } | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-29 18:47:27 +03:00
										 |  |  | // Load index(s)...
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2017-10-17 03:24:08 +03:00
										 |  |  | //	Load path (use default INDEX_DIR)...
 | 
					
						
							| 
									
										
										
										
											2014-12-29 18:47:27 +03:00
										 |  |  | //	loadIndex(path)
 | 
					
						
							|  |  |  | //	loadIndex(path, logger)
 | 
					
						
							|  |  |  | //		-> data
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2017-10-17 03:24:08 +03:00
										 |  |  | //	Load path with custom index_dir...
 | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | //	loadIndex(path, index_dir, logger)
 | 
					
						
							|  |  |  | //		-> data
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2017-10-17 03:24:08 +03:00
										 |  |  | //	Load from date...
 | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | //	loadIndex(path, index_dir, from_date, logger)
 | 
					
						
							|  |  |  | //		-> data
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2017-10-17 03:24:08 +03:00
										 |  |  | //	Load path as-is (do not search for index dir)...
 | 
					
						
							|  |  |  | //	loadIndex(path, false)
 | 
					
						
							|  |  |  | //	loadIndex(path, false, logger)
 | 
					
						
							|  |  |  | //		-> data
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	loadIndex(path, false, from_date, logger)
 | 
					
						
							|  |  |  | //		-> data
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-12-29 18:47:27 +03:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Procedure:
 | 
					
						
							|  |  |  | // 	- locate indexes in path given
 | 
					
						
							|  |  |  | // 	- per each index
 | 
					
						
							|  |  |  | // 		- get all .json files
 | 
					
						
							|  |  |  | // 		- get and load latest base file per keyword
 | 
					
						
							|  |  |  | // 		- merge all later than loaded base diff files per keyword
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2015-05-21 04:00:28 +03:00
										 |  |  | // Merging is done by copying the key-value pairs from diff to the
 | 
					
						
							|  |  |  | // resulting object.
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-12-29 18:47:27 +03:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Index format (input):
 | 
					
						
							| 
									
										
										
										
											2015-12-08 03:57:53 +03:00
										 |  |  | // 	<index-dir>/
 | 
					
						
							| 
									
										
										
										
											2014-12-29 18:47:27 +03:00
										 |  |  | // 		+- [<timestamp>-]<keyword>[-diff].json
 | 
					
						
							|  |  |  | // 		+- ...
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Output format:
 | 
					
						
							|  |  |  | // 	{
 | 
					
						
							|  |  |  | // 		// one per index found...
 | 
					
						
							|  |  |  | // 		<path>/<sub-path>: {
 | 
					
						
							|  |  |  | // 			<keyword>: <kw-data>,
 | 
					
						
							|  |  |  | // 			...
 | 
					
						
							|  |  |  | // 		},
 | 
					
						
							|  |  |  | // 		...
 | 
					
						
							|  |  |  | // 	}
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-12-16 04:50:34 +03:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-12-29 18:47:27 +03:00
										 |  |  | // Events emitted on logger if passed:
 | 
					
						
							| 
									
										
										
										
											2015-11-28 05:31:38 +03:00
										 |  |  | // 	- path <path>				- path currently being processed
 | 
					
						
							|  |  |  | // 	- files-found <n> <files>	- number and list of files found (XXX do we need this?)
 | 
					
						
							|  |  |  | // 	- queued <path>				- json file path queued for loading
 | 
					
						
							|  |  |  | // 	- files-queued <n> <queue>	- number of files queued and index (XXX do we need this?)
 | 
					
						
							|  |  |  | // 	- loaded <keyword> <path>	- done loading json file path
 | 
					
						
							|  |  |  | // 	- loaded <keyword>-diff <path>	
 | 
					
						
							|  |  |  | // 								- done loading json file path (diff file)
 | 
					
						
							|  |  |  | // 	- index <path> <data>		- done loading index at path
 | 
					
						
							|  |  |  | // 	- error <err>				- an error occurred...
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-12-28 05:09:28 +03:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-12-16 04:50:34 +03:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-12-31 19:18:03 +03:00
										 |  |  | // NOTE: this is fairly generic and does not care about the type of data
 | 
					
						
							|  |  |  | // 		or it's format as long as it's JSON and the file names comply
 | 
					
						
							|  |  |  | // 		with the scheme above...
 | 
					
						
							| 
									
										
										
										
											2015-05-21 04:00:28 +03:00
										 |  |  | // NOTE: this only loads the JSON data and does not import or process 
 | 
					
						
							|  |  |  | // 		anything...
 | 
					
						
							| 
									
										
										
										
											2014-12-31 19:18:03 +03:00
										 |  |  | //
 | 
					
						
							|  |  |  | // XXX need to do better error handling -- stop when an error is not recoverable...
 | 
					
						
							| 
									
										
										
										
											2015-11-16 17:00:56 +03:00
										 |  |  | // XXX really overcomplicated, mostly due to promises...
 | 
					
						
							|  |  |  | // 		...see if this can be split into more generic 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 05:09:28 +03:00
										 |  |  | // 		sections...
 | 
					
						
							| 
									
										
										
										
											2015-11-16 17:00:56 +03:00
										 |  |  | // XXX change path handling:
 | 
					
						
							| 
									
										
										
										
											2015-11-17 04:32:59 +03:00
										 |  |  | // 		1) explicit index -- ends with INDEX_DIR (works)
 | 
					
						
							| 
									
										
										
										
											2015-11-16 17:00:56 +03:00
										 |  |  | // 			-> load directly...
 | 
					
						
							| 
									
										
										
										
											2015-11-17 04:32:59 +03:00
										 |  |  | // 		2) implicit index -- path contains INDEX_DIR (works)
 | 
					
						
							| 
									
										
										
										
											2015-11-16 17:00:56 +03:00
										 |  |  | // 			-> append INDEX_DIR and (1)...
 | 
					
						
							| 
									
										
										
										
											2015-11-17 04:32:59 +03:00
										 |  |  | // 		3) path is a pattern (contains glob wildcards) (works)
 | 
					
						
							| 
									
										
										
										
											2015-11-16 17:00:56 +03:00
										 |  |  | // 			-> search + load
 | 
					
						
							|  |  |  | // 		4) non of the above...
 | 
					
						
							|  |  |  | // 			a) error
 | 
					
						
							|  |  |  | // 			b) append '**' (current behavior)
 | 
					
						
							|  |  |  | // 			...(a) seems more logical...
 | 
					
						
							| 
									
										
										
										
											2015-12-08 07:42:07 +03:00
										 |  |  | // XXX do a task version...
 | 
					
						
							| 
									
										
										
										
											2014-12-28 03:47:44 +03:00
										 |  |  | var loadIndex = | 
					
						
							|  |  |  | module.loadIndex =  | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | function(path, index_dir, from_date, logger){ | 
					
						
							| 
									
										
										
										
											2016-11-04 23:00:34 +03:00
										 |  |  | 	logger = logger && logger.push('Index') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-29 23:49:01 +03:00
										 |  |  | 	path = util.normalizePath(path) | 
					
						
							| 
									
										
										
										
											2016-05-30 02:23:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 	if(index_dir && index_dir.emit != null){ | 
					
						
							|  |  |  | 		logger = index_dir | 
					
						
							|  |  |  | 		index_dir = from_date = null | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} else if(from_date && from_date.emit != null){ | 
					
						
							|  |  |  | 		logger = from_date | 
					
						
							|  |  |  | 		from_date = null | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-17 03:24:08 +03:00
										 |  |  | 	//index_dir = index_dir || INDEX_DIR
 | 
					
						
							|  |  |  | 	index_dir = index_dir === false ?  | 
					
						
							|  |  |  | 		index_dir  | 
					
						
							|  |  |  | 		: (index_dir || INDEX_DIR) | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-28 04:55:51 +03:00
										 |  |  | 	// XXX should this be interactive (a-la EventEmitter) or as it is now 
 | 
					
						
							|  |  |  | 	// 		return the whole thing as a block (Promise)...
 | 
					
						
							|  |  |  | 	// 		NOTE: one way to do this is use the logger, it will get
 | 
					
						
							|  |  |  | 	// 				each index data on an index event
 | 
					
						
							| 
									
										
										
										
											2014-12-31 05:42:28 +03:00
										 |  |  | 	return new Promise(function(resolve, reject){ | 
					
						
							| 
									
										
										
										
											2016-06-14 00:04:53 +03:00
										 |  |  | 		// prepare the index_dir and path....
 | 
					
						
							|  |  |  | 		// NOTE: index_dir can be more than a single directory...
 | 
					
						
							| 
									
										
										
										
											2017-10-17 03:24:08 +03:00
										 |  |  | 		var i = index_dir  | 
					
						
							|  |  |  | 			&& util.normalizePath(index_dir).split(/[\\\/]/g) | 
					
						
							| 
									
										
										
										
											2016-06-14 00:04:53 +03:00
										 |  |  | 		var p = util.normalizePath(path).split(/[\\\/]/g).slice(-i.length) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-17 03:24:08 +03:00
										 |  |  | 		var explicit_index_dir = !index_dir  | 
					
						
							|  |  |  | 			|| (i.filter(function(e, j){ return e == p[j] }).length == i.length) | 
					
						
							| 
									
										
										
										
											2017-02-11 04:40:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 04:33:57 +03:00
										 |  |  | 		// we've got an index...
 | 
					
						
							| 
									
										
										
										
											2017-10-17 03:24:08 +03:00
										 |  |  | 		// XXX do we need to check if it's a dir???
 | 
					
						
							| 
									
										
										
										
											2017-02-11 04:40:22 +03:00
										 |  |  | 		if(explicit_index_dir){ | 
					
						
							| 
									
										
										
										
											2015-11-28 04:55:51 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			logger && logger.emit('path', path) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 04:33:57 +03:00
										 |  |  | 			listJSON(path) | 
					
						
							| 
									
										
										
										
											2014-12-28 20:46:06 +03:00
										 |  |  | 				// XXX handle errors...
 | 
					
						
							|  |  |  | 				.on('error', function(err){ | 
					
						
							|  |  |  | 					logger && logger.emit('error', err) | 
					
						
							| 
									
										
										
										
											2016-05-11 15:28:56 +03:00
										 |  |  | 					console.error(err) | 
					
						
							| 
									
										
										
										
											2014-12-28 20:46:06 +03:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2014-12-28 04:33:57 +03:00
										 |  |  | 				.on('end', function(files){ | 
					
						
							|  |  |  | 					var res = {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 					var index = groupByKeyword(files, from_date, logger) | 
					
						
							| 
									
										
										
										
											2016-05-11 15:28:56 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 04:33:57 +03:00
										 |  |  | 					// load...
 | 
					
						
							| 
									
										
										
										
											2014-12-31 05:42:28 +03:00
										 |  |  | 					Promise | 
					
						
							| 
									
										
										
										
											2015-11-16 11:31:15 +03:00
										 |  |  | 						.all(Object.keys(index).map(function(keyword){ | 
					
						
							| 
									
										
										
										
											2014-12-28 04:33:57 +03:00
										 |  |  | 							// get relevant paths...
 | 
					
						
							| 
									
										
										
										
											2015-11-16 11:31:15 +03:00
										 |  |  | 							var diffs = index[keyword] | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 							var latest = diffs.splice(-1)[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							// XXX not sure about this...
 | 
					
						
							|  |  |  | 							if(keyword == '__dates'){ | 
					
						
							|  |  |  | 								res.__dates = index.__dates | 
					
						
							| 
									
										
										
										
											2016-05-11 15:28:56 +03:00
										 |  |  | 								return true | 
					
						
							| 
									
										
										
										
											2016-05-03 19:14:46 +03:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2016-05-04 12:56:15 +03:00
										 |  |  | 							if(keyword == '__date'){ | 
					
						
							|  |  |  | 								res.__date = index.__date | 
					
						
							| 
									
										
										
										
											2016-05-11 15:28:56 +03:00
										 |  |  | 								return true | 
					
						
							| 
									
										
										
										
											2016-05-04 12:56:15 +03:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2014-12-28 04:33:57 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 23:49:42 +03:00
										 |  |  | 							// NOTE: so far I really do not like how nested and
 | 
					
						
							|  |  |  | 							// 		unreadable the Promise/Deferred code becomes
 | 
					
						
							|  |  |  | 							// 		even with a small rise in complexity...
 | 
					
						
							|  |  |  | 							// 		...for example, the following code is quite
 | 
					
						
							|  |  |  | 							// 		simple, but does not look the part.
 | 
					
						
							|  |  |  | 							//
 | 
					
						
							|  |  |  | 							// 		Maybe it's a style thing...
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 04:33:57 +03:00
										 |  |  | 							// load latest...
 | 
					
						
							|  |  |  | 							return loadJSON(latest) | 
					
						
							|  |  |  | 								.then(function(data){ | 
					
						
							| 
									
										
										
										
											2015-11-28 04:55:51 +03:00
										 |  |  | 									logger && logger.emit('loaded', keyword, latest) | 
					
						
							| 
									
										
										
										
											2015-11-16 11:31:15 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 									var loading = {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 04:33:57 +03:00
										 |  |  | 									// handle diffs...
 | 
					
						
							| 
									
										
										
										
											2014-12-31 05:42:28 +03:00
										 |  |  | 									return Promise | 
					
						
							| 
									
										
										
										
											2015-11-16 11:31:15 +03:00
										 |  |  | 										// load diffs...
 | 
					
						
							|  |  |  | 										.all(diffs.map(function(p){ | 
					
						
							|  |  |  | 											return loadJSON(p) | 
					
						
							|  |  |  | 												// XXX handle errors...
 | 
					
						
							|  |  |  | 												// XXX we should abort loading this index...
 | 
					
						
							|  |  |  | 												.catch(function(err){ | 
					
						
							|  |  |  | 													logger && logger.emit('error', err) | 
					
						
							| 
									
										
										
										
											2016-05-11 15:28:56 +03:00
										 |  |  | 													console.error(err) | 
					
						
							| 
									
										
										
										
											2015-11-16 11:31:15 +03:00
										 |  |  | 												}) | 
					
						
							|  |  |  | 												.then(function(json){ | 
					
						
							|  |  |  | 													// NOTE: we can't merge here
 | 
					
						
							|  |  |  | 													// 		as the files can be
 | 
					
						
							|  |  |  | 													// 		read in arbitrary order...
 | 
					
						
							|  |  |  | 													loading[p] = json | 
					
						
							|  |  |  | 												}) | 
					
						
							|  |  |  | 										})) | 
					
						
							|  |  |  | 										// merge diffs...
 | 
					
						
							| 
									
										
										
										
											2014-12-28 04:33:57 +03:00
										 |  |  | 										.then(function(){ | 
					
						
							| 
									
										
										
										
											2015-11-16 11:31:15 +03:00
										 |  |  | 											diffs | 
					
						
							|  |  |  | 												.reverse() | 
					
						
							|  |  |  | 												.forEach(function(p){ | 
					
						
							|  |  |  | 													var json = loading[p] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 													for(var n in json){ | 
					
						
							|  |  |  | 														data[n] = json[n] | 
					
						
							|  |  |  | 													} | 
					
						
							| 
									
										
										
										
											2014-12-28 04:33:57 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-28 04:55:51 +03:00
										 |  |  | 													logger && logger.emit('loaded', keyword+'-diff', p) | 
					
						
							| 
									
										
										
										
											2015-11-16 11:31:15 +03:00
										 |  |  | 												}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 											res[keyword] = data | 
					
						
							| 
									
										
										
										
											2014-12-28 04:33:57 +03:00
										 |  |  | 										}) | 
					
						
							|  |  |  | 								}) | 
					
						
							|  |  |  | 						})) | 
					
						
							|  |  |  | 						.then(function(){ | 
					
						
							|  |  |  | 							logger && logger.emit('index', path, res) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							var d = {} | 
					
						
							|  |  |  | 							d[path] = res | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							resolve(d) | 
					
						
							|  |  |  | 						}) | 
					
						
							| 
									
										
										
										
											2014-12-28 03:47:44 +03:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2014-12-16 04:50:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 04:33:57 +03:00
										 |  |  | 		// no explicit index given -- find all in sub tree...
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			var res = {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-11 04:40:22 +03:00
										 |  |  | 			// special case: root index...
 | 
					
						
							|  |  |  | 			if(fse.existsSync(path +'/'+ index_dir)){ | 
					
						
							|  |  |  | 				var n = path +'/'+ index_dir | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				return loadIndex(n, index_dir, from_date, logger)  | 
					
						
							|  |  |  | 					.then(function(obj){  | 
					
						
							|  |  |  | 						// NOTE: considering that all the paths within
 | 
					
						
							|  |  |  | 						// 		the index are relative to the preview 
 | 
					
						
							|  |  |  | 						// 		dir (the parent dir to the index root)
 | 
					
						
							|  |  |  | 						// 		we do not need to include the index 
 | 
					
						
							|  |  |  | 						// 		itself in the base path...
 | 
					
						
							|  |  |  | 						res[path] = obj[n]  | 
					
						
							|  |  |  | 						resolve(res) | 
					
						
							|  |  |  | 					}) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// full search...
 | 
					
						
							| 
									
										
										
										
											2016-05-13 00:59:56 +03:00
										 |  |  | 			getIndexes(path, index_dir, logger) | 
					
						
							|  |  |  | 				.catch(function(err){ | 
					
						
							| 
									
										
										
										
											2014-12-28 20:46:06 +03:00
										 |  |  | 					logger && logger.emit('error', err) | 
					
						
							| 
									
										
										
										
											2016-05-11 15:28:56 +03:00
										 |  |  | 					console.error(err) | 
					
						
							| 
									
										
										
										
											2014-12-28 20:46:06 +03:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2016-05-13 00:59:56 +03:00
										 |  |  | 				.then(function(paths){ | 
					
						
							| 
									
										
										
										
											2016-05-12 07:56:16 +03:00
										 |  |  | 					// start loading...
 | 
					
						
							|  |  |  | 					Promise.all(paths.map(function(p){ | 
					
						
							| 
									
										
										
										
											2016-06-14 00:04:53 +03:00
										 |  |  | 						p = util.normalizePath(p) | 
					
						
							| 
									
										
										
										
											2016-05-30 02:23:55 +03:00
										 |  |  | 						//var path = pathlib.normalize(p +'/'+ index_dir) 
 | 
					
						
							|  |  |  | 						var path = util.normalizePath(p +'/'+ index_dir)  | 
					
						
							| 
									
										
										
										
											2016-05-12 07:56:16 +03:00
										 |  |  | 						return loadIndex(path, index_dir, from_date, logger)  | 
					
						
							|  |  |  | 							.then(function(obj){  | 
					
						
							|  |  |  | 								// NOTE: considering that all the paths within
 | 
					
						
							|  |  |  | 								// 		the index are relative to the preview 
 | 
					
						
							|  |  |  | 								// 		dir (the parent dir to the index root)
 | 
					
						
							|  |  |  | 								// 		we do not need to include the index 
 | 
					
						
							|  |  |  | 								// 		itself in the base path...
 | 
					
						
							|  |  |  | 								res[p] = obj[path]  | 
					
						
							|  |  |  | 							}) | 
					
						
							|  |  |  | 					})).then(function(){ resolve(res) }) | 
					
						
							| 
									
										
										
										
											2014-12-28 03:47:44 +03:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2014-12-28 04:33:57 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2014-12-16 04:50:34 +03:00
										 |  |  | } | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-21 04:00:28 +03:00
										 |  |  | // get/populate the previews...
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // format:
 | 
					
						
							|  |  |  | // 	{
 | 
					
						
							|  |  |  | // 		<index-base>: {
 | 
					
						
							|  |  |  | // 			<gid>: {
 | 
					
						
							|  |  |  | // 				<resolution>: <local-path>,
 | 
					
						
							|  |  |  | // 				...
 | 
					
						
							|  |  |  | // 			},
 | 
					
						
							|  |  |  | // 			...
 | 
					
						
							|  |  |  | // 		},
 | 
					
						
							|  |  |  | // 		...
 | 
					
						
							|  |  |  | // 	}
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // XXX should this be compatible with loadIndex(..) data???
 | 
					
						
							|  |  |  | // XXX handle errors....
 | 
					
						
							|  |  |  | var loadPreviews = | 
					
						
							|  |  |  | module.loadPreviews = | 
					
						
							| 
									
										
										
										
											2016-06-03 04:46:51 +03:00
										 |  |  | function(base, pattern, previews, index_dir, absolute_path){ | 
					
						
							| 
									
										
										
										
											2015-05-21 04:00:28 +03:00
										 |  |  | 	previews = previews || {} | 
					
						
							| 
									
										
										
										
											2015-12-05 01:19:06 +03:00
										 |  |  | 	index_dir = index_dir || INDEX_DIR | 
					
						
							| 
									
										
										
										
											2016-05-29 23:49:01 +03:00
										 |  |  | 	base = util.normalizePath(base) | 
					
						
							| 
									
										
										
										
											2016-06-03 04:46:51 +03:00
										 |  |  | 	pattern = pattern || '*' | 
					
						
							| 
									
										
										
										
											2015-05-21 04:00:28 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-29 23:49:01 +03:00
										 |  |  | 	// we got an explicit index....
 | 
					
						
							|  |  |  | 	if(pathlib.basename(base) == index_dir){ | 
					
						
							|  |  |  | 		return new Promise(function(resolve, reject){ | 
					
						
							|  |  |  | 			if(!(base in previews)){ | 
					
						
							|  |  |  | 				previews[base] = {} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var images = previews[base] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-03 04:46:51 +03:00
										 |  |  | 			listPreviews(base, pattern) | 
					
						
							| 
									
										
										
										
											2016-05-29 23:49:01 +03:00
										 |  |  | 				// XXX handle errors....
 | 
					
						
							|  |  |  | 				//.on('error', function(err){
 | 
					
						
							|  |  |  | 				//})
 | 
					
						
							|  |  |  | 				// preview name syntax:
 | 
					
						
							|  |  |  | 				// 	<res>px/<gid> - <orig-filename>.jpg
 | 
					
						
							|  |  |  | 				.on('match', function(path){ | 
					
						
							|  |  |  | 					// get the data we need...
 | 
					
						
							|  |  |  | 					var gid = pathlib.basename(path).split(' - ')[0] | 
					
						
							|  |  |  | 					var res = pathlib.basename(pathlib.dirname(path)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					// build the structure if it does not exist...
 | 
					
						
							|  |  |  | 					if(!(gid in images)){ | 
					
						
							|  |  |  | 						images[gid] = {} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if(images[gid].preview == null){ | 
					
						
							|  |  |  | 						images[gid].preview = {} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					// add a preview...
 | 
					
						
							|  |  |  | 					// NOTE: this will overwrite a previews if they are found in
 | 
					
						
							|  |  |  | 					// 		several locations...
 | 
					
						
							|  |  |  | 					images[gid].preview[res] = | 
					
						
							|  |  |  | 						util.normalizePath(index_dir +'/'+ path.split(index_dir)[1]) | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 				.on('end', function(){ | 
					
						
							|  |  |  | 					resolve(previews) | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// find all sub indexes...
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return new Promise(function(resolve, reject){ | 
					
						
							|  |  |  | 			var queue = [] | 
					
						
							|  |  |  | 			listIndexes(base, index_dir) | 
					
						
							|  |  |  | 				// XXX handle errors....
 | 
					
						
							|  |  |  | 				//.on('error', function(err){
 | 
					
						
							|  |  |  | 				//})
 | 
					
						
							|  |  |  | 				.on('match', function(base){ | 
					
						
							| 
									
										
										
										
											2016-06-03 04:46:51 +03:00
										 |  |  | 					queue.push(loadPreviews(base, pattern, previews, index_dir, absolute_path)) | 
					
						
							| 
									
										
										
										
											2016-05-29 23:49:01 +03:00
										 |  |  | 				}) | 
					
						
							|  |  |  | 				.on('end', function(){ | 
					
						
							|  |  |  | 					Promise.all(queue) | 
					
						
							|  |  |  | 						.then(function(){ | 
					
						
							|  |  |  | 							resolve(previews) | 
					
						
							|  |  |  | 						}) | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-05-21 04:00:28 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-08 07:42:07 +03:00
										 |  |  | // XXX
 | 
					
						
							|  |  |  | var copyPreviews = | 
					
						
							|  |  |  | module.copyPreviews =  | 
					
						
							|  |  |  | function(){ | 
					
						
							|  |  |  | 	var q = tasks.Queue.clone() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// XXX
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-16 04:50:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-05 01:19:06 +03:00
										 |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | // Builder...
 | 
					
						
							|  |  |  | // 	- read list
 | 
					
						
							|  |  |  | // 	- generate previews (use config)
 | 
					
						
							|  |  |  | // 	- build images/data
 | 
					
						
							|  |  |  | // 		.loadURLs(..)
 | 
					
						
							|  |  |  | // 	- write (writer)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-28 23:49:42 +03:00
										 |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | // Writer...
 | 
					
						
							| 
									
										
										
										
											2015-12-05 01:19:06 +03:00
										 |  |  | //
 | 
					
						
							|  |  |  | // This is just like the loader, done in two stages:
 | 
					
						
							|  |  |  | // 	- format dependent de-construction (symetric to buildIndex(..))
 | 
					
						
							|  |  |  | // 	- generic writer...
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // NOTE: for now we'll stick to the current format...
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var FILENAME = '${DATE}-${KEYWORD}.${EXT}' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 00:04:53 +03:00
										 |  |  | // NOTE: keyword in index can take the form <path>/<keyword>, in which 
 | 
					
						
							|  |  |  | // 		case the keyword will be saved to the <path>/<file-format>
 | 
					
						
							|  |  |  | // 		NOTE: loadIndex(..) at this point will ignore sub-paths...
 | 
					
						
							| 
									
										
										
										
											2015-12-05 01:19:06 +03:00
										 |  |  | var writeIndex = | 
					
						
							|  |  |  | module.writeIndex =  | 
					
						
							| 
									
										
										
										
											2016-05-06 18:32:00 +03:00
										 |  |  | function(json, path, date, filename_tpl, logger){ | 
					
						
							| 
									
										
										
										
											2016-11-04 23:00:34 +03:00
										 |  |  | 	logger = logger && logger.push('Index') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-29 18:33:59 +03:00
										 |  |  | 	// XXX get this from args/config...
 | 
					
						
							|  |  |  | 	var spaces = null | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-29 23:49:01 +03:00
										 |  |  | 	path = util.normalizePath(path) | 
					
						
							| 
									
										
										
										
											2015-12-05 01:19:06 +03:00
										 |  |  | 	filename_tpl = filename_tpl || FILENAME | 
					
						
							|  |  |  | 	// XXX for some reason this gets the unpatched node.js Date, so we 
 | 
					
						
							|  |  |  | 	// 		get the patched date explicitly...
 | 
					
						
							| 
									
										
										
										
											2016-05-06 18:32:00 +03:00
										 |  |  | 	date = date || window.Date.timeStamp() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-05 01:19:06 +03:00
										 |  |  | 	var files = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// build the path if it does not exist...
 | 
					
						
							|  |  |  | 	return ensureDir(path) | 
					
						
							|  |  |  | 		.catch(function(err){ | 
					
						
							|  |  |  | 			logger && logger.emit('error', err) | 
					
						
							| 
									
										
										
										
											2016-05-11 15:28:56 +03:00
										 |  |  | 			console.error(err) | 
					
						
							| 
									
										
										
										
											2015-12-05 01:19:06 +03:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 		.then(function(){ | 
					
						
							|  |  |  | 			logger && logger.emit('path', path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// write files...
 | 
					
						
							|  |  |  | 			// NOTE: we are not doing this sequencilly as there will not
 | 
					
						
							|  |  |  | 			// 		be too many files...
 | 
					
						
							|  |  |  | 			return Promise | 
					
						
							|  |  |  | 				.all(Object.keys(json).map(function(keyword){ | 
					
						
							| 
									
										
										
										
											2017-12-29 18:33:59 +03:00
										 |  |  | 					//var data = JSON.stringify(json[keyword])
 | 
					
						
							|  |  |  | 					var data = JSON.stringify(json[keyword], null, spaces) | 
					
						
							| 
									
										
										
										
											2016-06-14 00:04:53 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					// get the sub-path and keyword...
 | 
					
						
							|  |  |  | 					var sub_path = keyword.split(/[\\\/]/g) | 
					
						
							|  |  |  | 					keyword = sub_path.pop() | 
					
						
							|  |  |  | 					sub_path = sub_path.join('/') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					var file = path +'/'+ sub_path +'/'+ (filename_tpl | 
					
						
							| 
									
										
										
										
											2015-12-05 01:19:06 +03:00
										 |  |  | 						.replace('${DATE}', date) | 
					
						
							|  |  |  | 						.replace('${KEYWORD}', keyword) | 
					
						
							|  |  |  | 						.replace('${EXT}', 'json')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					return ensureDir(pathlib.dirname(file)) | 
					
						
							|  |  |  | 						.then(function(){ | 
					
						
							|  |  |  | 							files.push(file) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							logger && logger.emit('queued', file) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							return writeFile(file, data, 'utf8') | 
					
						
							|  |  |  | 								.catch(function(err){ | 
					
						
							|  |  |  | 									logger && logger.emit('error', err) | 
					
						
							| 
									
										
										
										
											2016-05-11 15:28:56 +03:00
										 |  |  | 									console.error(err) | 
					
						
							| 
									
										
										
										
											2015-12-05 01:19:06 +03:00
										 |  |  | 								}) | 
					
						
							|  |  |  | 								.then(function(){ | 
					
						
							|  |  |  | 									logger && logger.emit('written', file) | 
					
						
							|  |  |  | 								}) | 
					
						
							|  |  |  | 						}) | 
					
						
							|  |  |  | 				})) | 
					
						
							|  |  |  | 			.then(function(){ | 
					
						
							|  |  |  | 				logger && logger.emit('done', files) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-12-28 23:49:42 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 05:48:26 +03:00
										 |  |  | /********************************************************************** | 
					
						
							| 
									
										
										
										
											2016-08-20 22:49:36 +03:00
										 |  |  | * vim:set ts=4 sw=4 :                               */ return module }) |