Skip to content

Instantly share code, notes, and snippets.

@thesabbir
Last active August 29, 2015 14:09
Show Gist options
  • Save thesabbir/9aa83af063dbd6bf4a80 to your computer and use it in GitHub Desktop.
Save thesabbir/9aa83af063dbd6bf4a80 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var path = require('path');
var movie_path = '/media/thesabbir/Media/Others/Movies/';
var website_url = 'http://localhost:3000/';
//var movie_path = '/home/thesabbir/Documents/mv/';
function fileFilter(files, extension) {
var regExp = new RegExp('\\.' + extension + '$');
return files.filter(regExp.test.bind(regExp));
// Returns an Array of files
}
fs.readdir(movie_path, function (err, fileList) {
"use strict";
// If io erro throw it
if (err) {
throw err;
}
var list = [];
//Loop through each file
fileList.forEach(function (file) {
var mv = path.resolve(movie_path, file);
//Get the stat
var stat = fs.statSync(mv);
//Check if it's a directory
if (stat.isDirectory()) {
//if it is a directory then
//Read it And
// select only video files
var video_files = fileFilter(fs.readdirSync(mv), '(mp4|avi|webm)');
video_files.forEach(function (sfile) {
//Add to the list with some additional data
list.push({
filename : sfile,
stat : stat,
url : encodeURI(website_url + file + '/' + sfile)
});
});
}
//If it is a file and not a directory
else {
//Select video files
if ((/\.(mp4|avi|webm)$/i).test(file)) {
//And add them to the list
list.push({
filename : file,
stat : stat,
url : website_url + encodeURIComponent(file)
});
}
}
});
fs.writeFile('movies.json', JSON.stringify(list), function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment