-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.js
99 lines (89 loc) · 3.2 KB
/
init.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
#!/usr/bin/env node
/*
* Copyright (C) 2013-2016 Stéphane Péchard.
*
* This file is part of PhotoBackup.
*
* PhotoBackup is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PhotoBackup is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
(function () {
'use strict';
var bcrypt = require('bcrypt');
var fs = require('fs');
var ini = require('ini');
var inquirer = require('inquirer');
var sha512 = require('js-sha512');
var questions = [
{
type: 'input',
name: 'mediaroot',
message: 'The directory where to put the pictures (should be writable by the server you use):',
validate: validationFunc
},
{
type: 'input',
name: 'owner',
message: 'Owner of the directory:',
default: 'www-data',
validate: validationFunc
},
{
type: 'password',
name: 'password',
message: 'The server password:',
validate: validationFunc
}
];
// validate the answers given by the user
var validationFunc = function (input) {
return (input.length !== 0);
};
// create the config object for the ini file
function createConfig (configPath, section) {
var config = {};
try {
// config is the content of the ini file
config = ini.parse(fs.readFileSync(configPath, 'utf-8'));
if (config.hasOwnProperty(section)) {
// if the current section already exists, update it
delete config[section];
}
} catch (e) {
// config is an empty object if there is an error
config = {};
}
return config;
}
// function to export
var init = function (configPath, username, sectionName) {
inquirer.prompt(questions).then(function (answers) {
var config = createConfig(configPath, sectionName);
var passSHA = sha512(answers.password).toString('hex');
var passhash = bcrypt.hashSync(passSHA, bcrypt.genSaltSync());
// fill the section
config[sectionName] = {
'BindAddress': '127.0.0.1',
'MediaRoot': answers.mediaroot,
'Password': passSHA,
'PasswordBcrypt': passhash,
'Port': 8420
};
// write the whole file
fs.writeFileSync(configPath, ini.stringify(config));
var option = (username.length > 0) ? ' ' + username : '';
console.log("\nCreated, now launch PhotoBackup server with 'photobackup run" + option + "'");
});
};
module.exports = { init: init };
}());