-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_tools.js
160 lines (159 loc) · 5.3 KB
/
file_tools.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
/**
* @fileoverview Collection of functions that deal with reading/writing files
* @author Donald Elrod
* @version 1.0.0
*/
var fs = require('fs');
/**
* Collection of functions that deal with reading/writing files
* @exports file_tools
*/
module.exports = {
/**
* Returns the children of the directory, with the absolute path attached
* @param {string} folder the path to the folder to get the children of
* @return {string[]} the children files of the given directory, returns empty array if not a directory
*/
getChildren: function(folder) {
if (!this.checkIfDir(folder))
return [];
files = fs.readdirSync(folder);
for (i = 0; i < files.length; i++) {
files[i] = folder+'/'+files[i];
}
return files;
},
/**
* Returns parsed JSON object from file at specified path
* @param {string} file the path to the JSON file
* @return {Promise<JSON>} promise to JSON at file location
*/
readJSONFile: async function(file) {
return new Promise(function(resolve, reject) {
try {
fs.readFile(file, function(err, data) {
//console.log(file);
//console.log('read JSON');
resolve(JSON.parse(data));
});
} catch (error) {
reject('no file found');
}
});
},
/**
* Writes the given JSON object to file
* @param {string} file path of the file to write
* @param {JSON} json that JSON object to write to file
* @param {function(json)} callback the function to call when the JSON has been written to file
*/
writeJSONFile: function(file, json, callback) {
fs.writeFile(file, JSON.stringify(json), function (err) {
if (err)
throw err;
else {
console.log('wrote JSON');
callback(json);
}
});
},
/**
* Loads the user's config file, or creates a new one if it does not exist
* @param {function} callback the function to call when the config has been loaded
* @return {JSON} returns the settings object for the user
*/
loadConfig: function(folderpath, callback) {
var configFile = __dirname + '/settings.conf';
if (!fs.existsSync(configFile)) { //if the config file doesn't exist
settings = {
user: {
name: "Donald"
},
general: {
masterDir: folderpath,
coverDir: './covers'
},
plex: {
info: false,
token: false,
url: '',
port: 0,
username: '',
password: '',
X_Auth_Token: ''
}
};
this.writeJSONFile(configFile, settings, callback);
} else { //or if it does exist...
console.log('settings exist')
settings = this.readJSONFile(configFile, callback);
}
//console.log(settings);
return settings;
},
/**
* Checks if the given path is a file or directory
* @param {string} file path to check if directory or not
* @return {boolean} returns true if the path is a directory, false if it is anything else
*/
checkIfDir: function(file) {
var stat = fs.statSync(file)
return stat.isDirectory();
},
/**
* Returns the file type of the provided file
* @param {string} file the name of the file
* @return {string} the file type
*/
getFiletype: function(file) {
return file.slice(file.lastIndexOf('.')+1);
},
/**
* Returns the children files of the given directory absolute path
* @param {string} fn absolute path of the file in the file system
* @return {string[]} array of filenames of directory's children
*/
getChdir: function(fn) {
var files = getChildren(fn);
var fo = [];
files.forEach( file => { //adds only the files to the array
var stats = fs.statSync(folderpath+file);
if (stats.isFile(folderpath+file)) {
fo.push(file);
}
});
return fo;
},
/**
* Checks to see if the file exists
*
* Synchronous function
* @param {string} file path of the file
*/
fileExists: function(file) {
return fs.existsSync(file);
},
/**
* Writes the image in data to file
*
* Image is from Google URL
* @param {string} fn name of the file to write
* @param {string} data the image data to be written to file
*/
writeImageFile: function(fn, data) {
//console.log(data.substr(0,20));
var fd = fs.openSync('./backend/covers/' + fn, 'w+');
fs.writeFileSync(fd, data.read(), function(err) {
console.error('cover download failed: ' + err);
});
},
/**
* Deletes the file given by fn, and then calls the callback function
* @param {string} fn the path to the file to delete
* @param {function} callback the function to perform when the delete is completed
*/
deleteFile: function(fn, callback) {
fs.unlinkSync(fn);
callback();
}
};