- Dynamic Pages (MyPage)

- Uploading files to the Server FTP.
This commit is contained in:
Paolo Arena
2019-12-07 00:20:06 +01:00
parent b92655c2e5
commit 9d0e6136ac
6 changed files with 235 additions and 6 deletions

View File

@@ -17,6 +17,7 @@
"author": "Paolo Arena",
"license": "MIT",
"dependencies": {
"basic-ftp": "^4.5.1",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"cors": "^2.8.4",
@@ -25,6 +26,7 @@
"ejs": "^2.7.1",
"email-templates": "^5.1.0",
"express": "^4.17.1",
"formidable": "^1.2.1",
"i18n": "^0.8.3",
"jade": "^1.11.0",
"jsonwebtoken": "^7.1.9",

View File

@@ -0,0 +1,52 @@
'use strict';
const ftp = require('basic-ftp');
// const fs = require('fs');
class FTPClient {
constructor(host = 'localhost', port = 21, username = 'anonymous', password = 'guest', secure = false, secureOptions = '') {
this.client = new ftp.Client();
// this.client.ftp.verbose = true;
this.settings = {
host: host,
port: port,
user: username,
password: password,
secure: secure,
secureOptions: secureOptions
};
}
async upload(sourcePath, remotePath, permissions) {
let self = this;
try {
let access = await self.client.access(self.settings);
let upload = await self.client.uploadFrom(sourcePath, remotePath);
// let permissions = await self.changePermissions(permissions.toString(), remotePath);
self.client.close();
return true;
} catch (err) {
console.log('upload ERR: ', err);
self.client.close();
return false;
}
}
async createDir(miadir) {
let access = await this.client.access(this.settings);
await this.client.ensureDir(miadir);
}
close() {
this.client.close();
}
changePermissions(perms, filepath) {
let cmd = 'SITE CHMOD ' + perms + ' ' + filepath;
return this.client.send(cmd, false);
}
}
module.exports = FTPClient;

View File

@@ -1,4 +1,5 @@
{
"Email Verificata!": "Email Verificata!",
"partecipanti": "partecipanti"
"partecipanti": "partecipanti",
"L'Email è già stata Verificata.": "L'Email è già stata Verificata."
}

View File

@@ -0,0 +1,87 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const tools = require('../tools/general');
mongoose.Promise = global.Promise;
mongoose.level = "F";
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
});
const MyPageSchema = new Schema({
idapp: {
type: String,
},
author_username: {
type: String,
},
title: {
type: String,
},
path: {
type: String,
},
icon: {
type: String,
},
keywords: {
type: String,
},
description: {
type: String,
},
heightimg: {
type: Number,
},
imgback: {
type: String,
},
content: {
type: String,
},
active: {
type: Boolean,
},
inmenu: {
type: Boolean,
},
submenu: {
type: Boolean,
},
l_par: {
type: Number,
},
l_child: {
type: Number,
},
infooter: {
type: Boolean,
},
});
MyPageSchema.statics.getFieldsForSearch = function () {
return ['title', 'keywords', 'description', 'content']
};
MyPageSchema.statics.executeQueryTable = function (idapp, params) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, idapp, params);
};
MyPageSchema.statics.findAllIdApp = async function (idapp) {
const MyPage = this;
const myfind = { idapp };
return await MyPage.find(myfind, (err, arrrec) => {
return arrrec
});
};
const MyPage = mongoose.model('MyPage', MyPageSchema);
module.exports = { MyPage };

View File

@@ -1,5 +1,8 @@
const express = require('express');
const router = express.Router();
const router = express.Router(),
fs = require('fs'),
path = require('path');
const { authenticate, authenticate_noerror } = require('../middleware/authenticate');
@@ -8,6 +11,14 @@ const { ObjectID } = require('mongodb');
const mongoose = require('mongoose');
const cfgserver = mongoose.model('cfgserver');
const ftp = require('../ftp/FTPClient'),
formidable = require('formidable'),
folder = path.join(__dirname, 'files');
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder)
}
const _ = require('lodash');
const { User } = require('../models/user');
@@ -18,6 +29,7 @@ const { MyEvent } = require('../models/myevent');
const { Contribtype } = require('../models/contribtype');
const { Discipline } = require('../models/discipline');
const { Newstosent } = require('../models/newstosent');
const { MyPage } = require('../models/mypage');
const { TemplEmail } = require('../models/templemail');
const { OpzEmail } = require('../models/opzemail');
const { MailingList } = require('../models/mailinglist');
@@ -82,7 +94,7 @@ router.post(process.env.LINK_REQUEST_NEWPASSWORD, (req, res) => {
user.tokenforgot = jwt.sign(user._id.toHexString(), process.env.SIGNCODE).toString();
user.date_tokenforgot = new Date();
user.lasttimeonline = new Date();
user.save().then( async () => {
user.save().then(async () => {
await sendemail.sendEmail_RequestNewPassword(res.locale, user.email, user.idapp, user.tokenforgot);
res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
});
@@ -152,6 +164,8 @@ function getTableByTableName(tablename) {
mytable = Discipline;
else if (tablename === 'newstosent')
mytable = Newstosent;
else if (tablename === 'mypage')
mytable = MyPage;
else if (tablename === 'templemail')
mytable = TemplEmail;
else if (tablename === 'opzemail')
@@ -362,11 +376,12 @@ router.get('/loadsite/:userId/:idapp/:sall', authenticate_noerror, (req, res) =>
let newstosent = Promise.resolve([]);
let mailinglist = Promise.resolve([]);
if (sall){
let mypage = MyPage.findAllIdApp(idapp);
if (sall) {
newstosent = Newstosent.findAllIdApp(idapp);
}
return Promise.all([bookedevent, eventlist, operators, wheres, contribtype, settings, permissions, disciplines, newstosent, mailinglist])
return Promise.all([bookedevent, eventlist, operators, wheres, contribtype, settings, permissions, disciplines, newstosent, mailinglist, mypage])
.then((arrdata) => {
// console.table(arrdata);
res.send({
@@ -380,6 +395,7 @@ router.get('/loadsite/:userId/:idapp/:sall', authenticate_noerror, (req, res) =>
disciplines: arrdata[7],
newstosent: arrdata[8],
mailinglist: arrdata[9],
mypage: arrdata[10],
});
})
.catch((e) => {
@@ -437,5 +453,64 @@ router.get(process.env.LINK_CHECK_UPDATES, authenticate, (req, res) => {
});
router.post('/upload/:dir', authenticate, (req, res) => {
const dir = req.params.dir;
const idapp = req.user.idapp;
const form = new formidable.IncomingForm();
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);
// SSL_OP_NO_TLSv1_2 = 134217728
// console.log('client', client);
form.uploadDir = folder;
try {
form.on('fileBegin', async function (name, file){
file.path = folder + '/' + file.name;
});
form.on('file', async function (name, file){
try {
// Create directory remote
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)
res.status(400).send();
else
res.end();
}catch (e) {
console.log('error', e);
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();
});
} catch (e) {
console.log('Error', e)
}
});
module.exports = router;

View File

@@ -3,7 +3,9 @@ require('./config/config');
const _ = require('lodash');
const cors = require('cors');
var fs = require('fs');
const fs = require('fs');
//const throttle = require('express-throttle-bandwidth');
const port = process.env.PORT;
@@ -16,6 +18,7 @@ const sendemail = require('./sendemail');
const cron = require('node-cron');
i18n = require("i18n");
if ((process.env.NODE_ENV === 'production') || (process.env.NODE_ENV === 'test')) {
@@ -36,6 +39,7 @@ require('./models/booking');
require('./models/sendmsg');
require('./models/mailinglist');
require('./models/newstosent');
require('./models/mypage');
const mysql_func = require('./mysql/mysql_func');
@@ -152,6 +156,14 @@ if ((process.env.NODE_ENV === 'production') || (process.env.NODE_ENV === 'test')
httpServer.listen(port);
}
// app.use(throttle(1024 * 128)); // throttling bandwidth
// app.use((req, res, next) => {
// res.header('Access-Control-Allow-Origin', '*')
// res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
// next()
// });
if (process.env.PROD !== 1) {
// testmsgwebpush();
// sendemail.testemail('2', 'it');