Last active
August 29, 2015 14:14
-
-
Save fotisp/526d6f6050acecf6f914 to your computer and use it in GitHub Desktop.
Apostrophe2 Import Files Task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* You will need the following dependencies | |
* | |
* npm install --save recursive-readdir | |
* npm install --save lodash | |
* npm install --save async | |
* | |
* I am sure this can become better javascripty code but it does the job for now | |
*/ | |
var recursive = require("recursive-readdir"); | |
var _= require("lodash"); | |
var async= require("async"); | |
var fs = require("fs"); | |
module.exports = function(){ | |
console.log("Setting up local tasks"); | |
return { | |
importFiles: function (site, apos, argv, callback) { | |
if(!argv.files){ | |
return callback("You need to specify a --files option pointing to a directory"); | |
} | |
var _basePath = argv.files; | |
console.log("Importing files from "+_basePath); | |
// ignore files named 'foo.cs' or files that end in '.html'. | |
function extractMeta(file,callback){ | |
var _clean = file.replace(_basePath,""); | |
var _tags = _clean.substr(1).split("/"); | |
var _file = _tags.pop(); | |
var _fs = fs.statSync(file); | |
apos.md5File(file,function(err,d){ | |
if(err){ | |
return callback(err); | |
} | |
callback(null, | |
{ | |
md5:d, | |
name:_file, | |
length:_fs["size"], | |
path:file, | |
tags:_tags | |
} | |
); | |
}); | |
} | |
recursive(_basePath, function (err, files) { | |
var _toAccept=[]; | |
async.each(files,function(file,fileDone){ | |
if (file.match(/\.(jpg|jpeg|png)$/g)) { | |
extractMeta(file,function(err,meta){ | |
console.log(" + ", file); | |
_toAccept.push(meta); | |
fileDone(err); | |
}); | |
} else { | |
console.log(" - ", file); | |
fileDone(null); | |
} | |
},function(err){ | |
if(err){ | |
return callback(err); | |
} | |
if(_toAccept.length <=0){ | |
return callback(null); | |
} | |
var _req =apos.getTaskReq(); | |
apos.acceptFiles(_req,_toAccept,function(err,acceptedFiles){ | |
//tag | |
if(err){ | |
return callback(err); | |
} | |
async.each(acceptedFiles,function(f,cb){ | |
var _foundTag = _.find(_toAccept,function(needle){ | |
return needle.md5 == f.md5; | |
}); | |
if(!_foundTag || _foundTag.tags.length <=0){ | |
return cb(null); | |
} | |
f.tags = _foundTag.tags; | |
f.searchText = apos.fileSearchText(f); | |
apos.files.update({"_id": f._id},f,function(err){ | |
return cb(err); | |
}); | |
},function(err){ | |
callback(err); | |
}); | |
}) | |
}); | |
}); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment