New Gallery: create directory if doesn't exist!#120

This commit is contained in:
Paolo Arena
2019-12-28 14:30:30 +01:00
parent 523edf557b
commit 0c91e6ce0b
3 changed files with 101 additions and 52 deletions

View File

@@ -68,6 +68,16 @@ if (process.env.NODE_ENV === 'production') {
portapp: '0',
dir: '',
},
{
idapp: '7',
name: 'Notevole',
adminemail: 'pao.loarena77@gmail.com',
manageremail: '',
replyTo: '',
host: 'https://notevole.freeplanet.app',
portapp: '0',
dir: '/var/www/notevole.freeplanet.app',
},
];
} else if (process.env.NODE_ENV === 'test') {
MYAPPS = [
@@ -117,7 +127,16 @@ if (process.env.NODE_ENV === 'production') {
host: 'http://test.mandalaperlanima.eu',
portapp: '0',
dir: '',
}
},
{
idapp: '7',
name: 'Notevole',
adminemail: '',
manageremail: '',
host: 'https://notevole.freeplanet.app',
portapp: '0',
dir: '/var/www/notevole.freeplanet.app',
},
];
} else {
MYAPPS = [
@@ -168,6 +187,15 @@ if (process.env.NODE_ENV === 'production') {
portapp: '8083',
dir: '',
},
{
idapp: '7',
name: 'Notevole',
adminemail: 'paolo.arena77@gmail.com',
manageremail: '',
host: 'http://localhost',
portapp: '8085',
dir: '',
},
];
}

View File

@@ -462,62 +462,62 @@ router.post('/upload_from_other_server/:dir', authenticate, (req, res) => {
const dir = req.params.dir;
const idapp = req.user.idapp;
/*
const form = new formidable.IncomingForm();
/*
const form = new formidable.IncomingForm();
form.parse(req);
form.parse(req);
const client = new ftp(process.env.FTPSERVER_HOST, process.env.FTPSERVER_PORT, process.env.FTPSERVER_USER + idapp + '@associazioneshen.it', process.env.FTPSERVER_PWD + idapp, false, 134217728);
const client = new ftp(process.env.FTPSERVER_HOST, process.env.FTPSERVER_PORT, process.env.FTPSERVER_USER + idapp + '@associazioneshen.it', process.env.FTPSERVER_PWD + idapp, false, 134217728);
// SSL_OP_NO_TLSv1_2 = 134217728
// SSL_OP_NO_TLSv1_2 = 134217728
// console.log('client', client);
// console.log('client', client);
form.uploadDir = folder + '/' + dir;
try {
form.uploadDir = folder + '/' + dir;
try {
form.on('fileBegin', async function (name, file){
file.path = folder + '/' + file.name;
});
form.on('fileBegin', async function (name, file){
file.path = folder + '/' + file.name;
});
form.on('file', async function (name, file){
try {
// Create directory remote
form.on('file', async function (name, file){
try {
// Create directory remote
if (!!dir)
await client.createDir(dir);
if (!!dir)
await client.createDir(dir);
const miofile = (dir) ? dir + `/` + file.name : file.name;
console.log('Upload...');
const ret = await client.upload(file.path, miofile, 755);
console.log('Uploaded ' + file.name, 'status:', ret);
if (!ret)
const miofile = (dir) ? dir + `/` + file.name : file.name;
console.log('Upload...');
const ret = await client.upload(file.path, miofile, 755);
console.log('Uploaded ' + file.name, 'status:', ret);
if (!ret)
res.status(400).send();
else {
// Delete file from local directory
fs.unlinkSync(file.path);
res.end();
}
}catch (e) {
console.log('error', e);
res.status(400).send();
else {
// Delete file from local directory
fs.unlinkSync(file.path);
res.end();
}
}catch (e) {
console.log('error', e);
});
form.on('aborted', () => {
console.error('Request aborted by the user');
res.status(400).send();
}
});
});
form.on('aborted', () => {
console.error('Request aborted by the user');
res.status(400).send();
});
form.on('error', (err) => {
console.error('Error Uploading', err);
res.status(400).send();
});
form.on('error', (err) => {
console.error('Error Uploading', err);
res.status(400).send();
});
} catch (e) {
console.log('Error', e)
}
*/
} catch (e) {
console.log('Error', e)
}
*/
});
@@ -527,6 +527,8 @@ router.post('/upload/:dir', authenticate, (req, res) => {
const dir = req.params.dir;
const idapp = req.user.idapp;
// console.log('/upload dir:' + dir);
const form = new formidable.IncomingForm();
form.parse(req);
@@ -534,25 +536,31 @@ router.post('/upload/:dir', authenticate, (req, res) => {
form.uploadDir = folder + '/' + dir;
try {
form.on('fileBegin', async function (name, file){
form.on('fileBegin', async function (name, file) {
file.path = folder + '/' + file.name;
});
form.on('file', async function (name, file){
form.on('file', async function (name, file) {
try {
console.log('Uploaded ' + file.name);
// ++ Move in the folder application !
newname = tools.getdirByIdApp(idapp) + '/statics/upload/' + dir + '/' + file.name;
const mydir = tools.getdirByIdApp(idapp) + '/statics/upload/' + dir;
// Create Dir if doesn't exist:
tools.mkdirpath(mydir);
newname = mydir + '/' + file.name;
console.log('move from ', file.path, 'to :', newname);
// For local: ... resolve this... sending through the static folder...
// res.sendFile(path.resolve(file.name));
// Move in the folder application !
tools.move(file.path, newname, (err) => {
console.log('err', err);
console.log('err:', err);
res.end();
});
}catch (e) {
} catch (e) {
console.log('error', e);
res.status(400).send();
}
@@ -592,7 +600,7 @@ router.delete('/delfile', authenticate, (req, res) => {
res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
});
}catch (e) {
} catch (e) {
console.log('error', e);
res.status(400).send();
}

View File

@@ -571,6 +571,19 @@ module.exports = {
}
callback();
});
}
},
};
mkdirpath(dirPath) {
// if (!fs.accessSync(dirPath, fs.constants.R_OK | fs.constants.W_OK)) {
if (!fs.existsSync(dirPath)){
try {
fs.mkdirSync(dirPath, { recursive: true });
}
catch (e) {
mkdirpath(path.dirname(dirPath));
mkdirpath(dirPath);
}
}
},
};