lib/file.js   A
last analyzed

Complexity

Total Complexity 23
Complexity/F 1.92

Size

Lines of Code 69
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 23
eloc 47
mnd 11
bc 11
fnc 12
dl 0
loc 69
rs 10
bpm 0.9166
cpm 1.9166
noi 1
c 0
b 0
f 0
1
var args = require('./args.js');
2
var log = require('./logger.js').get();
3
4
var S3 = null;
5
var fs = null;
6
var redis = null;
7
8
if (args.redis) {
9
	var REDIS = require('redis');
10
	redis = REDIS.createClient(args.db_port, args.redis);
11
	redis.on('error', log.error);
12
} else if (args.s3) {
13
	var AWS = require('aws-sdk');
14
	S3 = new AWS.S3();
15
} else {
16
	fs = require('fs');
17
	
18
	if (!fs.existsSync(args.folder)){
19
		return fs.mkdirSync(args.folder, {recursive: true});
20
	}
21
}
22
23
24
module.exports = {
25
	'get': function(file, callback) {
26
		if (args.redis) {
27
			return redis.get(file, callback);
28
		} else if (args.s3) {
29
			return S3.getObject({Bucket: args.s3, Key: file}, function(err, data) {
30
				return callback(err, !err && Buffer.from(data.Body).toString('utf8'));
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ 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...
31
			});
32
		}
33
		return fs.readFile(file, callback);
34
	},
35
	'set': function(file, data, callback) {
36
		if (args.redis) {
37
			return redis.set(file, data, function(err) {
38
				return !err && redis.expire(file, args.expiry_seconds);
39
			});
40
		} else if (args.s3) {
41
			return S3.putObject({Bucket: args.s3, Key: file, Body: data}, function(err) {
42
				return callback(err);
43
			});
44
		}
45
		return fs.writeFile(file, data, callback);
46
	},
47
	'del': function(file, callback) {
48
		if (args.redis) {
49
			return redis.expire(file, -1, callback);
50
		} else if (args.s3) {
51
			return S3.deleteObject({Bucket: args.s3, Key: file}, function(err) {
52
				return callback(err);
53
			});
54
		}
55
		return fs.unlink(file, callback);
56
	},
57
	'list': function(dir, callback) {
58
		if (args.redis) {
59
			return redis.keys(dir + '*', function(err, data) {
60
				return callback(err, !err && data.map(function(v) { return v.replace(args.folder, ''); }));
61
			});
62
		} else if (args.s3) {
63
			return S3.listObjectsV2({Bucket: args.s3, Prefix: dir}, function(err, data) {
64
				return callback(err, !err && data.Contents.map(function(v) { return v.Key; }));
65
			});
66
		}
67
		return fs.readdir(dir, callback);
68
	}
69
};