Passed
Push — master ( c8e729...4d8c57 )
by Evgeny
01:45
created

lib/file.js (3 issues)

1
var args = require('./args.js');
2
3
var S3 = null;
4
var fs = null;
5
6
if (args.s3) {
7
	var AWS = require('aws-sdk');
8
	S3 = new AWS.S3();
9
} else {
10
	fs = require('fs');
11
	
12
	// Otherwise, it'll created automatically
13
	if (!fs.existsSync(args.folder)){
14
		return fs.mkdirSync(args.folder, {recursive: true});
15
	}
16
}
17
18
19
module.exports = {
20
	'readFile': function(file, callback) {
21
		if (args.s3) {
22
			return S3.getObject({Bucket: args.s3, Key: args.folder + file}, function(err, data) {
23
				return callback(err, !err && Buffer.from(data.Body).toString('utf8'));
24
			});
25
		}
26
		return fs.readFile(file, callback);
27
	},
28
	'writeFile': function(file, data, callback) {
29
		if (args.s3) {
30
			return S3.putObject({Bucket: args.s3, Key: args.folder + file, Body: data}, function(err, data) {
0 ignored issues
show
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
31
				return callback(err);
32
			});
33
		}
34
		return fs.writeFile(file, data, callback);
35
	},
36
	'unlink': function(file, callback) {
37
		if (args.s3) {
38
			return S3.deleteObject({Bucket: args.s3, Key: args.folder + file}, function(err, data) {
0 ignored issues
show
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
39
				return callback(err);
40
			});
41
		}
42
		return fs.unlink(file, callback);
43
	},
44
	'readdir': function(dir, callback) {
45
		if (args.s3) {
46
			return S3.listObjectsV2({Bucket: args.s3, Prefix: args.folder + file}, function(err, data) {
0 ignored issues
show
The variable file seems to be never declared. If this is a global, consider adding a /** global: file */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
47
				return callback(err, !err && data.Contents.map(function(v) { return v.Key; }));
48
			});
49
		}
50
		return fs.readdir(dir, callback);
51
	}
52
};