-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile.js
260 lines (208 loc) · 6.68 KB
/
File.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
var fs = require('fs'),
async = require('async'),
path = require('path'),
spawn = require('child_process').spawn,
execFile = require('child_process').execFile;
// WARNING:
// Caching doesn't necessarily
// improve performance
// V8 already does a ton of caching
// to to prevent subsequent system calls
// no performance improvement
// can even harm performance!!!
function File( options ) {
var stats,
mimeType,
lastModified,
parentDirectory;
this._absolutePath = undefined;
this._fileName = undefined;
this._displayName = undefined;
this._isDirectory = undefined;
this._parentDirectory = undefined;
// console.log('File pre init this:', this);
(function init( options ){
if ( ! options || ! options.absoluteFilePath )
throw new Error( 'Missing options!' );
this._absolutePath = options.absoluteFilePath;
this._displayName = options.displayName; // can be undefined
this._isDirectory = options.isDirectory; // can be undefined
this._fileName = path.basename( this._absolutePath );
this._parentDirectory = path.dirname( this._absolutePath ) + '/';
}.bind(this)( options ));
this._getStats = function ( callback ) {
if ( stats ) {
callback(null, stats);
return;
}
this.getAbsolutePath(function( err, absolutePath ) {
fs.stat(absolutePath, function( err, st ) {
stats = st;
if ( err )
console.error("STATS ERROR:", err);
callback( err, stats );
});
});
}.bind(this);
this.getCachedLastModified = function() {
if ( ! lastModified ) throw new Error( "'lastModified' not cached!" );
return lastModified;
};
// FIXME:
// getMimeType is very slow
// requires some kind of caching
this.getMimeType = function( callback ) {
this.getAbsolutePath(function( err, absolutePath ) {
if (err) {
callback( err, null );
return;
}
// CAUTION:
// There is a max process limit
// With large directory this limit
// can be exceeded
//
// FIXME: Need to find single process solution!
execFile('mimedb', [absolutePath], function(error, stdout, stderr){
if ( ! error )
mimeType = stdout.trim();
callback( error, mimeType );
});
});
}.bind(this);
this.getCachedMimeType = function() {
if ( ! mimeType ) throw new Error( "'mimeType' not cached!" );
return mimeType;
}.bind(this);
this.getLastModified = function( callback ) {
if ( lastModified ) {
callback( null, lastModified );
return;
}
this._getStats(function(err, stats){
if ( err ) {
callback( err, null );
return;
}
lastModified = stats.mtime;
callback( null, lastModified );
});
}.bind(this);
}
File.prototype.cachedIsDirectory = function() {
if ( typeof this._isDirectory === 'undefined' )
throw new Error( "'this._isDirectory' not cached!" );
return this._isDirectory;
};
File.prototype.isDirectory = function( callback ) {
if ( typeof this._isDirectory !== 'undefined' ) {
callback( null, this._isDirectory );
return;
}
this._getStats(function(err, stats){
if ( err ) {
callback( err, null );
return;
}
this._isDirectory = stats.isDirectory();
callback( null, this._isDirectory );
}.bind(this));
};
File.prototype.getFileName = function( callback ) {
callback( null, this._fileName );
};
File.prototype.getCachedFileName = function() {
if ( typeof this._fileName === 'undefined' )
throw new Error( "'this._fileName' not cached!" );
return this._fileName;
};
File.prototype.getAbsolutePath = function( callback ) {
callback ( null, this._absolutePath );
};
File.prototype.getCachedAbsolutePath = function() {
if ( typeof this._absolutePath === 'undefined' )
throw new Error( "'this._absolutePath' not cached!" );
return this._absolutePath;
};
File.prototype.isHidden = function( callback ) {
if ( typeof this._isHidden === 'undefined' )
this._isHidden = !! this._fileName.match(/^\./);
callback(null, this._isHidden);
};
File.prototype.cachedIsHidden = function() {
if ( typeof this._isHidden === 'undefined' )
throw new Error( "'this._isHidden' not cached!" );
return this._isHidden;
};
File.prototype.getParentDirectory = function( callback ) {
callback( null, this._parentDirectory );
};
// 'displayName'is the name that should be used
// when rendering the file
// also e.g. when sorting by name
// e.g. for DesktopFile the 'fileName'
// isn't relevatn to the user
// but 'displayName' is
// == (The app name set inside .desktop file),
// OR usefull for setting custom display name
// e.g. instead of (/home/)<username> only 'Home'
// or for virtual directories like applications:
File.prototype.getDisplayName = function( callback ) {
// check if manually overriden in constructor
// else set displayName equal to fileName
if ( typeof this._displayName === 'undefined' ) {
this._displayName = this._fileName;
}
callback(null, this._displayName);
};
File.prototype.getCachedDisplayName = function() {
if ( typeof this._displayName === 'undefined' )
throw new Error( "'this._displayName' not cached!" );
return this._displayName;
};
File.prototype.isWritable = function( callback ) {
this._getStats(function( err, stats ) {
if ( err ) {
callback( new Error ('Can not determine if file is writable.' +
'Failed to aquire stats!\n' + err),
null );
return;
}
var isOwner = process.getuid() === stats.uid,
inGroup = process.getgid() === stats.gid,
mode = stats.mode,
isWritable = isOwner && (mode & 00200) || // User is owner and owner can write.
inGroup && (mode & 00020) || // User is in group and group can write.
(mode & 00002); // Anyone can write.
callback( null, isWritable );
});
};
File.prototype.open = function( asRoot ) {
this.getAbsolutePath(function(err, absPath) {
if ( err ) {
console.error('Failed to open file. Missing absolute Path.', err);
return;
}
var env = process.env;
// xdg-open has this very silly
// feature / bug that if it doesn't
// know the desktop environment
// it will always try to open
// a file in the browser
// so by setting gnome as the
// desktop environment
// environment variable
// we can fix this behaviour
env.DE = 'gnome';
var command = asRoot ? 'gksudo' : '/usr/bin/xdg-open',
commandArgs = asRoot ? ['/usr/bin/xdg-open', absPath] : [absPath],
app = spawn( command, commandArgs, {
detached: true,
env: env
});
app.on('error', function ( err ) {
console.error('child process error ' + err);
});
}.bind(this));
};
module.exports = File;