<commit_before>(function( console, exports, require ) {
	'use strict';

	var moviesPath,
		clivlc,
		path = require( 'path' ),
		fs = require( 'fs' ),
		ejs = require( 'ejs' ),

		// ejs 'layouts' (expressjs no longer supports layouts)
		ejsLayout = function( res, view, data ) {
			fs.readFile( 'views/' + view + '.ejs', 'UTF-8', function( err, view ) {
				if ( err ) {
					Error.call( this, err );
				} else {
					data.body = ejs.render( view, data );
					res.render( 'layout',  data );
				}
			});
		}
	;


	// load prefs
	fs.readFile( 'preferences.json', function( err, jsonData ) {
		if ( err ) {
			console.log( err + '\nUsing default preferences.' );
			jsonData = {
				vlc : {
					cmd : '/Applications/VLC.app/Contents/MacOS/VLC'
				},
				media : {
					folders : [ '../fixture/movies' ]
				}
			};
		} else {
			jsonData = JSON.parse( jsonData ).preferences;
		}

		if ( jsonData.vlc && jsonData.vlc.cmd ) {
			clivlc = path.resolve( jsonData.vlc.cmd );
			console.log( 'vlc: ' + clivlc );
		}

		if ( jsonData.media && jsonData.media.folders ) {
			moviesPath = path.resolve( jsonData.media.folders[ 0 ]);
			console.log( 'movies: ' + moviesPath );
		}
	});


	// use for home page
	exports.index = function( req, res ) {
		ejsLayout( res, 'index', {
			title: 'Vulcan media centre'
		});
	};


	// list all movies in folder
	exports.listMovies = function( req, res ) {
		var movies = {},
			i,
			dataFile;

		fs.readdir( moviesPath, function( err, files ) {
			// TODO handle 'no files'
			files = files.filter(function( filename ) {
				// ignore . files
				return ! /^\./.test( filename );
			});

			for ( i = 0; i < files.length; i++ ) {
				// add moviee to list
				movies[ files[ i ]] = { title: files[ i ] };

				// read metadata for movie
				dataFile = path.join( moviesPath, files[ i ], 'metadata.json' );
				if ( fs.existsSync( dataFile )) {
					try {
						movies[ files[ i ]] = JSON.parse( fs.readFileSync( dataFile )).movie;
					} catch ( x ) {
						console.error( dataFile, x );
					}
				}
			}

			ejsLayout( res, 'movie/list', {
				title: 'Movies',
				files: files,
				movies: movies
			});
		});
	};


	// get details for a movie
	exports.getMovie = function( req, res ) {
		var movie = req.params.movie,
			data = { title : movie },
			dataFile = path.join( moviesPath, movie, 'metadata.json' );

		fs.readFile( dataFile, function( err, jsonData ) {
			if ( err ) {
				data.dateCreated = '';
			} else {
				// TODO should merge in with movie title read from file path
				data = JSON.parse( jsonData ).movie;
			}
			ejsLayout( res, 'movie/get', {
				title: movie,
				movie: data
			});
		});
	};


	// get details for a movie
	exports.getMovieData = function( req, res ) {
		var movie = req.params.movie,
			data = { title : movie },
			dataFile = path.join( moviesPath, movie, 'metadata.json' );

		fs.readFile( dataFile, function( err, jsonData ) {
			if ( err ) {
				data.dateCreated = '';
			} else {
				// TODO should merge in with movie title read from file path
				data = JSON.parse( jsonData ).movie;
			}
			ejsLayout( res, 'movie/metadata', {
				title: movie,
				movie: data
			});
		});
	};


	// save details for a movie
	exports.postMovieData = function( req, res ) {
		var movie = String( req.params.movie ),
			dataFile = path.join( moviesPath, movie, 'metadata.json' );

		// write to metadata.json file
		// console.log( req.body );
		fs.writeFile( dataFile, JSON.stringify({ movie: req.body }, null, 4 ) );

		// redirect to metadata page
		// could use 'back' but relies on referrer
		res.redirect( '/movie/' + movie + '/data' );
		
	};


	// get poster image for a movie
	exports.getMoviePoster = function( req, res ) {
		var movie = req.params.movie;

		fs.readdir( path.join( moviesPath, movie ), function( err, files ) {
			var i = 0;

			// look for first image
			while ( i < files.length && ! /\.jpg$/.test( files[ i ] )) {
				i++;
			}

			if ( i < files.length ) {
				res.sendfile( path.join( moviesPath, movie, files[ i ] ));<commit_msg>handle errors in getMoviePoster<commit_after>(function( console, exports, require ) {
	'use strict';

	var moviesPath,
		clivlc,
		path = require( 'path' ),
		fs = require( 'fs' ),
		ejs = require( 'ejs' ),

		// ejs 'layouts' (expressjs no longer supports layouts)
		ejsLayout = function( res, view, data ) {
			fs.readFile( 'views/' + view + '.ejs', 'UTF-8', function( err, view ) {
				if ( err ) {
					Error.call( this, err );
				} else {
					data.body = ejs.render( view, data );
					res.render( 'layout',  data );
				}
			});
		}
	;


	// load prefs
	fs.readFile( 'preferences.json', function( err, jsonData ) {
		if ( err ) {
			console.log( err + '\nUsing default preferences.' );
			jsonData = {
				vlc : {
					cmd : '/Applications/VLC.app/Contents/MacOS/VLC'
				},
				media : {
					folders : [ '../fixture/movies' ]
				}
			};
		} else {
			jsonData = JSON.parse( jsonData ).preferences;
		}

		if ( jsonData.vlc && jsonData.vlc.cmd ) {
			clivlc = path.resolve( jsonData.vlc.cmd );
			console.log( 'vlc: ' + clivlc );
		}

		if ( jsonData.media && jsonData.media.folders ) {
			moviesPath = path.resolve( jsonData.media.folders[ 0 ]);
			console.log( 'movies: ' + moviesPath );
		}
	});


	// use for home page
	exports.index = function( req, res ) {
		ejsLayout( res, 'index', {
			title: 'Vulcan media centre'
		});
	};


	// list all movies in folder
	exports.listMovies = function( req, res ) {
		var movies = {},
			i,
			dataFile;

		fs.readdir( moviesPath, function( err, files ) {
			// TODO handle 'no files'
			files = files.filter(function( filename ) {
				// ignore . files
				return ! /^\./.test( filename );
			});

			for ( i = 0; i < files.length; i++ ) {
				// add moviee to list
				movies[ files[ i ]] = { title: files[ i ] };

				// read metadata for movie
				dataFile = path.join( moviesPath, files[ i ], 'metadata.json' );
				if ( fs.existsSync( dataFile )) {
					try {
						movies[ files[ i ]] = JSON.parse( fs.readFileSync( dataFile )).movie;
					} catch ( x ) {
						console.error( dataFile, x );
					}
				}
			}

			ejsLayout( res, 'movie/list', {
				title: 'Movies',
				files: files,
				movies: movies
			});
		});
	};


	// get details for a movie
	exports.getMovie = function( req, res ) {
		var movie = req.params.movie,
			data = { title : movie },
			dataFile = path.join( moviesPath, movie, 'metadata.json' );

		fs.readFile( dataFile, function( err, jsonData ) {
			if ( err ) {
				data.dateCreated = '';
			} else {
				// TODO should merge in with movie title read from file path
				data = JSON.parse( jsonData ).movie;
			}
			ejsLayout( res, 'movie/get', {
				title: movie,
				movie: data
			});
		});
	};


	// get details for a movie
	exports.getMovieData = function( req, res ) {
		var movie = req.params.movie,
			data = { title : movie },
			dataFile = path.join( moviesPath, movie, 'metadata.json' );

		fs.readFile( dataFile, function( err, jsonData ) {
			if ( err ) {
				data.dateCreated = '';
			} else {
				// TODO should merge in with movie title read from file path
				data = JSON.parse( jsonData ).movie;
			}
			ejsLayout( res, 'movie/metadata', {
				title: movie,
				movie: data
			});
		});
	};


	// save details for a movie
	exports.postMovieData = function( req, res ) {
		var movie = String( req.params.movie ),
			dataFile = path.join( moviesPath, movie, 'metadata.json' );

		// write to metadata.json file
		// console.log( req.body );
		fs.writeFile( dataFile, JSON.stringify({ movie: req.body }, null, 4 ) );

		// redirect to metadata page
		// could use 'back' but relies on referrer
		res.redirect( '/movie/' + movie + '/data' );
		
	};


	// get poster image for a movie
	exports.getMoviePoster = function( req, res ) {
		var movie = req.params.movie;

		fs.readdir( path.join( moviesPath, movie ), function( err, files ) {
			if ( err ) {
				console.log( err );
				return;
			}

			var i = 0;

			// look for first image
			while ( files && i < files.length && ! /\.jpg$/.test( files[ i ] )) {
				i++;
			}

			if ( i < files.length ) {
				res.sendfile( path.join( moviesPath, movie, files[ i ] ));
{'fraction_non_alphanumeric': 0.1594060620935084, 'fraction_numerical': 0.0012271444348999877, 'mean_word_length': 2.427249789739277, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '766950', 'n_tokens_mistral': 3411, 'n_tokens_neox': 2790, 'n_words': 775}