Skip to content

Instantly share code, notes, and snippets.

@eivindingebrigtsen
Created December 11, 2018 20:47
Show Gist options
  • Save eivindingebrigtsen/135544b47119533571c0e20e6af62937 to your computer and use it in GitHub Desktop.
Save eivindingebrigtsen/135544b47119533571c0e20e6af62937 to your computer and use it in GitHub Desktop.
var sendToContentful = (fileSend)=>{
var name = '';
var data = {};
var names = {};
var addEntryToData = (pathname)=>{
var filename = path.basename(pathname);
var longlocale = filename.substr(0,5);
if(exactMatches.indexOf(longlocale.toUpperCase()) != -1){
names[longlocale] = filename.substr(5);
data[longlocale] = {
contentType: mime.lookup(filename),
fileName: filename,
file: fs.createReadStream(pathname)
}
console.log(names, data);
}else{
var locale = filename.substr(0,2).toUpperCase();
for(var a in localeNames[locale]){
names[localeNames[locale][a]] = filename.substr(3);
data[localeNames[locale][a]] = {
contentType: mime.lookup(filename),
fileName: filename,
file: fs.createReadStream(pathname)
}
}
}
}
for(var a in fileSend){
addEntryToData(fileSend[a]);
}
console.time('UPLOAD')
client.getSpace(space)
.then((space) => space.createAssetFromFiles({fields: { title: names, file: data }}))
.then((asset) => {
asset.processForAllLocales();
console.log(asset);
console.timeEnd('UPLOAD')
})
.then((asset) => {
console.groupEnd('Uploading');
console.timeEnd('Time')
})
.catch(console.error)
}
var folderWithFiles = process.argv[2];
var walkSync = function(dir, filelist) {
files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function(file) {
if (fs.statSync(dir + file).isDirectory()) {
filelist = walkSync(dir + file + '/', filelist);
}
else {
filelist.push(file);
}
});
return filelist;
};
var list = walkSync(folderWithFiles);
for (var i = list.length - 1; i >= 0; i--) {
if(list[i].match('desktop')){
files.desktop.push(folderWithFiles+'/'+list[i]);
}
if(list[i].match('tablet_v')){
files.tablet_v.push(folderWithFiles+'/'+list[i]);
}
if(list[i].match('tablet_h')){
files.tablet_h.push(folderWithFiles+'/'+list[i]);
}
if(list[i].match('mobile')){
files.mobile.push(folderWithFiles+'/'+list[i]);
}
}
if(files.desktop.length){
sendToContentful(files.desktop);
}
if(files.tablet_h.length){
sendToContentful(files.tablet_h);
}
if(files.tablet_v.length){
sendToContentful(files.tablet_v);
}
if(files.mobile.length){
sendToContentful(files.mobile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment