| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  | var fsapi = require("desirae/lib/node-adapters").fsapi; | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  | module.exports.create = function (options) { | 
					
						
							| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  |   var restful = {}; | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |   //
 | 
					
						
							|  |  |  |   // Required for desirae
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   restful.walk = function (req, res, next) { | 
					
						
							|  |  |  |     if (!(/^GET$/i.test(req.method) || /^GET$/i.test(req.query._method))) { | 
					
						
							|  |  |  |       next(); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  |     var opts = {}, | 
					
						
							|  |  |  |       dirnames = | 
					
						
							|  |  |  |         (req.query.dir && [req.query.dir]) || | 
					
						
							|  |  |  |         (req.query.dirs && req.query.dirs.split(/,/g)) || | 
					
						
							|  |  |  |         req.body.dirs; | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |     if (!dirnames || !dirnames.length) { | 
					
						
							| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  |       res.send({ | 
					
						
							|  |  |  |         error: | 
					
						
							|  |  |  |           "please specify GET w/ req.query.dir or POST w/ _method=GET&dirs=path/to/thing,...", | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  |     if ( | 
					
						
							|  |  |  |       !dirnames.every(function (dirname) { | 
					
						
							|  |  |  |         return "string" === typeof dirname; | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2015-01-14 05:14:11 -05:00
										 |  |  |       res.send({ error: "malformed request: " + JSON.stringify(dirnames) }); | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* | 
					
						
							|  |  |  |     if (req.query.excludes) { | 
					
						
							|  |  |  |       opts.excludes = req.query.excludes.split(','); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (req.query.extensions) { | 
					
						
							|  |  |  |       opts.extensions = req.query.extensions.split(/,/g); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  |     if ("true" === req.query.dotfiles) { | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |       opts.dotfiles = true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  |     if ("false" === req.query.sha1sum) { | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |       opts.sha1sum = false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  |     if ("true" === req.query.contents) { | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |       opts.contents = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // TODO opts.contents?
 | 
					
						
							|  |  |  |     fsapi.walk.walkDirs(options.blogdir, dirnames, opts).then(function (stats) { | 
					
						
							|  |  |  |       if (!req.body.dirs && !req.query.dirs) { | 
					
						
							| 
									
										
										
										
											2015-01-14 05:14:11 -05:00
										 |  |  |         res.send(stats[dirnames[0]]); | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2015-01-14 05:14:11 -05:00
										 |  |  |         res.send(stats); | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   restful.getFiles = function (req, res, next) { | 
					
						
							|  |  |  |     if (!(/^GET$/i.test(req.method) || /^GET$/i.test(req.query._method))) { | 
					
						
							|  |  |  |       next(); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  |     var filepaths = | 
					
						
							|  |  |  |       (req.query.path && [req.query.path]) || | 
					
						
							|  |  |  |       (req.query.paths && req.query.paths.split(/,/g)) || | 
					
						
							|  |  |  |       req.body.paths; | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |     if (!filepaths || !filepaths.length) { | 
					
						
							| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  |       res.send({ | 
					
						
							|  |  |  |         error: | 
					
						
							|  |  |  |           "please specify GET w/ req.query.path or POST _method=GET&paths=path/to/thing,...", | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return fsapi.getfs(options.blogdir, filepaths).then(function (files) { | 
					
						
							|  |  |  |       if (!req.body.paths && !req.query.paths) { | 
					
						
							| 
									
										
										
										
											2015-01-14 05:14:11 -05:00
										 |  |  |         res.send(files[0]); | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         res.send(files); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   restful.putFiles = function (req, res, next) { | 
					
						
							| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  |     if ( | 
					
						
							|  |  |  |       !(/^POST|PUT$/i.test(req.method) || /^POST|PUT$/i.test(req.query._method)) | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |       next(); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  |     var opts = {}, | 
					
						
							|  |  |  |       files = req.body.files; | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |     if (!files || !files.length) { | 
					
						
							| 
									
										
										
										
											2015-01-14 05:14:11 -05:00
										 |  |  |       res.send({ error: "please specify POST w/ req.body.files" }); | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     opts.tmpdir = options.tmpdir; | 
					
						
							|  |  |  |     return fsapi.putfs(options.blogdir, files, opts).then(function (results) { | 
					
						
							| 
									
										
										
										
											2015-01-14 05:14:11 -05:00
										 |  |  |       res.send(results); | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   restful.copy = function (req, res, next) { | 
					
						
							| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  |     if ( | 
					
						
							|  |  |  |       !(/^POST|PUT$/i.test(req.method) || /^POST|PUT$/i.test(req.query._method)) | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |       next(); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-08 20:30:15 -07:00
										 |  |  |     var opts = {}, | 
					
						
							|  |  |  |       files = req.body.files; | 
					
						
							|  |  |  |     if ("object" !== typeof files || !Object.keys(files).length) { | 
					
						
							| 
									
										
										
										
											2015-01-14 05:14:11 -05:00
										 |  |  |       res.send({ error: "please specify POST w/ req.body.files" }); | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return fsapi.copyfs(options.blogdir, files, opts).then(function (results) { | 
					
						
							| 
									
										
										
										
											2015-01-14 05:14:11 -05:00
										 |  |  |       res.send(results); | 
					
						
							| 
									
										
										
										
											2015-01-13 14:50:17 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // end Desirae API
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return restful; | 
					
						
							|  |  |  | }; |