1 | var 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 |
|
10 |
|
11 | var Artycles = function( options ){
|
12 |
|
13 | options = options || {};
|
14 | this.options = utils.extend( defaults, options );
|
15 |
|
16 | this.store = new CRUD( this.options );
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 | Artycles.prototype = {
|
22 |
|
23 | constructor: Artycles,
|
24 |
|
25 | // API
|
26 |
|
27 | image: image,
|
28 |
|
29 | video: video,
|
30 |
|
31 | // options
|
32 | set: function( options ){
|
33 |
|
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 |
|
43 | name: function(){
|
44 | return utils.filename();
|
45 | },
|
46 |
|
47 |
|
48 | format: function( file ){
|
49 |
|
50 | var ext = file.substr( file.lastIndexOf(".")+1 );
|
51 |
|
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 |
|
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 |
|
62 |
|
63 | return false;
|
64 | },
|
65 |
|
66 |
|
67 | onCompileComplete: function( data, callback ){
|
68 |
|
69 | data = data || {};
|
70 |
|
71 | var self = this;
|
72 | var output = [];
|
73 |
|
74 |
|
75 | fs.readdir(data.dir, function(err, files){
|
76 |
|
77 |
|
78 | files = ( files instanceof Array ) ? files : [];
|
79 | var item = {};
|
80 |
|
81 | async.each(files, function( file, next) {
|
82 |
|
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
|
88 | };
|
89 |
|
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 |
|
94 | if( file == data.name+data.ext ){
|
95 | item["original"] = options.destination;
|
96 | } else {
|
97 |
|
98 | var key = file.replace(data.name+"-", "").replace(data.ext, "");
|
99 | item[key] = options.destination;
|
100 | }
|
101 |
|
102 | self.store.create( options, function(){
|
103 |
|
104 | next();
|
105 | });
|
106 |
|
107 | }, function(err){
|
108 |
|
109 | if( err ) {
|
110 |
|
111 |
|
112 | console.log('A file failed to process');
|
113 | } else {
|
114 | output.push(item);
|
115 |
|
116 | utils.rmdir(data.dir);
|
117 |
|
118 | if( self.options.source.remove ){
|
119 | fs.unlinkSync(data.source);
|
120 | }
|
121 |
|
122 | if(typeof callback == "function") callback( output );
|
123 | }
|
124 | });
|
125 |
|
126 | });
|
127 |
|
128 | }
|
129 |
|
130 | }
|
131 |
|
132 |
|
133 | module.exports = Artycles;
|