UNPKG

3.17 kBJavaScriptView Raw
1var async = require("async"),
2 defaults = require("./options"),
3 CRUD = require("./crud"),
4 fs = require("fs"),
5 image = require("./image"),
6 video = require("./video"),
7 utils = require("./utils");
8
9// Constructor
10
11var Artycles = function( options ){
12 // extending default options
13 options = options || {};
14 this.options = utils.extend( defaults, options );
15 // init crud
16 this.store = new CRUD( this.options );
17}
18
19// Methods
20
21Artycles.prototype = {
22
23 constructor: Artycles,
24
25 // API
26
27 image: image,
28
29 video: video,
30
31 // options
32 set: function( options ){
33 // prerequisites
34 if( typeof options != "object" ) return;
35 this.options = utils.extend( this.options, options );
36 },
37
38 get: function( key ){
39 return this.options[key];
40 },
41
42 // exposing the random name generator
43 name: function(){
44 return utils.filename();
45 },
46
47 // return the format type of a file
48 format: function( file ){
49 // get extension
50 var ext = file.substr( file.lastIndexOf(".")+1 );
51 // image
52 for(var i in this.options.image.formats ){
53 var format = this.options.image.formats[i];
54 if( ext == format ) return "image";
55 }
56 // video
57 for(var j in this.options.video.formats ){
58 var format = this.options.video.formats[j];
59 if( ext == format ) return "video";
60 }
61 // other?
62 //...
63 return false;
64 },
65
66 // events
67 onCompileComplete: function( data, callback ){
68 // fallabcks
69 data = data || {};
70 //
71 var self = this;
72 var output = [];
73 //read all files in the folder...
74 // use synchronous method?
75 fs.readdir(data.dir, function(err, files){
76 // error control...
77 //if( err )
78 files = ( files instanceof Array ) ? files : [];
79 var item = {};
80
81 async.each(files, function( file, next) {
82 // skip original file
83 if( file == data.name+data.ext && !self.options.source.copy ) return next();
84
85 var options = {
86 source: data.dir+file,
87 destination: self.options.path + file //path.basename( file );
88 };
89 // group resolutions
90 if( file !== data.name+data.ext && self.options.files.group ){
91 options.destination = self.options.path + data.name +"/"+ self.options.files.prefix + file.replace(data.name+"-", "");
92 }
93 // save destination item
94 if( file == data.name+data.ext ){
95 item["original"] = options.destination;
96 } else {
97 // this is a resized image
98 var key = file.replace(data.name+"-", "").replace(data.ext, "");
99 item[key] = options.destination;
100 }
101 // copy file to store
102 self.store.create( options, function(){
103 // error control?
104 next();
105 });
106
107 }, function(err){
108 // if any of the file processing produced an error, err would equal that error
109 if( err ) {
110 // One of the iterations produced an error.
111 // All processing will now stop.
112 console.log('A file failed to process');
113 } else {
114 output.push(item);
115 // remove tmp dir (wait?)
116 utils.rmdir(data.dir);
117 // optionally remove source file
118 if( self.options.source.remove ){
119 fs.unlinkSync(data.source);
120 }
121 // execute original callback
122 if(typeof callback == "function") callback( output );
123 }
124 });
125
126 });
127
128 }
129
130}
131
132
133module.exports = Artycles;