Compare commits

..

No commits in common. 'f7699115409de4e3594210a017b79cf01de80416' and '9bfcbb3340b72ef737b10dd2dedbf8f1e793e6d0' have entirely different histories.

  1. 22
      migrations/20210331080204-addImageFileNameColumn.js
  2. 4
      models/user.js
  3. 5
      package.json
  4. 99
      routes/users.js
  5. 10
      services/user.js
  6. 34
      tools/imageResizer.js

@ -1,22 +0,0 @@
'use strict';
module.exports = {
up(queryInterface, Sequelize) {
return Promise.all([
queryInterface.addColumn(
'Users',
'imageFileName',
{
type: Sequelize.STRING,
allowNull: false,
},
)
]);
},
down(queryInterface, Sequelize) {
return Promise.all([
queryInterface.removeColumn('Users', 'imageFileName'),
]);
},
};

@ -38,10 +38,6 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false, allowNull: false,
defaultValue: 'Hform4.docx' defaultValue: 'Hform4.docx'
},
imageFileName:{
type: DataTypes.STRING,
allowNull: false,
} }
}, { }, {

@ -13,14 +13,11 @@
"express": "~4.16.1", "express": "~4.16.1",
"http-errors": "~1.6.3", "http-errors": "~1.6.3",
"jalali-moment": "^3.3.10", "jalali-moment": "^3.3.10",
"jimp": "^0.16.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"morgan": "~1.9.1", "morgan": "~1.9.1",
"multer": "^1.4.2",
"persianjs": "^0.4.0", "persianjs": "^0.4.0",
"pg": "^8.5.1", "pg": "^8.5.1",
"pizzip": "^3.0.6", "pizzip": "^3.0.6",
"sequelize": "^6.5.0", "sequelize": "^6.5.0"
"shortid": "^2.2.16"
} }
} }

@ -1,93 +1,48 @@
const express = require('express'); var express = require('express');
const multer = require('multer') var router = express.Router();
const shortid = require('shortid')
const router = express.Router();
const userServeice = require('../services/user') const userServeice = require('../services/user')
const fs = require('fs') const fs = require('fs')
const path = require('path'); var path = require('path');
//------------------------------//
// multer config
//------------------------------//
var imageStorage = multer.diskStorage({
destination: function (req, file, cb) {
//TODO: change address*
cb(null, __dirname + '/../public/images')
},
filename: function (req, file, cb) {
var fileInfo = path.parse(file.originalname)
let fileName = shortid.generate() + Date.now().toString().substring(6) + fileInfo.ext
cb(null, fileName)
}
})
var imageFilter = function (req, file, callback) {
if (!file.originalname.match(/\.(jpg|jpeg|png|PNG|JPEG|)$/i)) {
return callback(new Error('Only image files are allowed!'), false)
}
callback(null, true)
}
var uploadImageConfig = multer({
storage: imageStorage,
fileFilter: imageFilter
})
var uploadImage = uploadImageConfig.single('image')
router.post('/add', function (req, res) {
router.post('/add', uploadImage, function (req, res) {
let flag = false; let flag = false;
let emptyFieldsArray = [] let emptyFieldsArray =[]
const userInfo = { const userInfo = {
firstName: req.body.firstName ? req.body.firstName.trim() : false, firstName: req.body.firstName.trim(),
lastName: req.body.lastName ? req.body.lastName.trim() : false, lastName: req.body.lastName.trim(),
fatherName: req.body.fatherName ? req.body.fatherName.trim() : false, fatherName: req.body.fatherName.trim(),
codeMelli: req.body.codeMelli ? req.body.codeMelli.trim() : false, codeMelli: req.body.codeMelli.trim(),
workUnit: req.body.workUnit ? req.body.workUnit.trim() : false, workUnit: req.body.workUnit.trim(),
workUnitCode: req.body.workUnitCode ? req.body.workUnitCode.trim() : false, workUnitCode: req.body.workUnitCode.trim(),
address: req.body.address ? req.body.address.trim() : false, address: req.body.address.trim(),
obligationNumber: req.body.obligationNumber ? req.body.obligationNumber.trim() : false, obligationNumber: req.body.obligationNumber.trim(),
obligationDate: req.body.obligationDate ? req.body.obligationDate.trim() : false, obligationDate: req.body.obligationDate.trim(),
requestNumber: req.body.requestNumber ? req.body.requestNumber.trim() : false, requestNumber: req.body.requestNumber.trim(),
requestDate: req.body.requestDate ? req.body.requestDate.trim() : false, requestDate: req.body.requestDate.trim(),
writtenOrderNumber: req.body.writtenOrderNumber ? req.body.writtenOrderNumber.trim() : false, writtenOrderNumber: req.body.writtenOrderNumber.trim(),
writtenOrderDate: req.body.writtenOrderDate ? req.body.writtenOrderDate.trim() : false, writtenOrderDate: req.body.writtenOrderDate.trim(),
deadline: req.body.deadline ? req.body.deadline.trim() : false, deadline: req.body.deadline.trim(),
deadlineUnit: req.body.deadlineUnit ? req.body.deadlineUnit.trim() : 'روز', deadlineUnit: req.body.deadlineUnit.trim(),
healthCenter: req.body.healthCenter ? req.body.healthCenter.trim() : false, healthCenter: req.body.healthCenter.trim(),
healthCenterCity: req.body.healthCenterCity ? req.body.healthCenterCity.trim() : false, healthCenterCity: req.body.healthCenterCity.trim()
imageFileName: req.file ? req.file.filename.slice(0 , req.file.filename.indexOf('.')) : false
} }
for (const key in userInfo) { for (const key in userInfo) {
if ((!userInfo[key] || !userInfo[key].trim()) && key !== 'deadlineUnit') { if (!userInfo[key] || !userInfo[key].trim()) {
emptyFieldsArray.push(key) emptyFieldsArray.push(key)
flag = true flag = true
} }
} }
if (flag) { if(flag){
if (req.file) {
try {
fs.unlinkSync(__dirname + '/../public/images/' + req.file.filename)
console.log('picture is deleted .....')
console.log('----------------------------')
} catch (err) {
console.log(err);
console.log('picture not deleted .....')
}
}
res.status(412).send({ res.status(412).send({
success: false, success: false,
emptyFields: emptyFieldsArray, emptyFields : emptyFieldsArray,
error: `پر کردن همه فیلدها الزامی است` error: `پر کردن همه فیلدها الزامی است`
}) })
}else {
} else { userServeice.addUser(userInfo)
userServeice.addUser(userInfo , req.file.filename)
.then((result) => { .then((result) => {
res.status(200).send({ res.status(200).send({
success: true, success: true,

@ -2,7 +2,6 @@ const models = require('../models')
const jalali = require("jalali-moment"); const jalali = require("jalali-moment");
const persianjs = require('persianjs') const persianjs = require('persianjs')
const docxTemp = require('../tools/docxtemp') const docxTemp = require('../tools/docxtemp')
const imageResizer = require('../tools/imageResizer')
let methods = {} let methods = {}
@ -36,14 +35,9 @@ let privates = {
} }
methods.addUser = (userInfo,imgOriginalName) => { methods.addUser = (userInfo) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
models.User.create(userInfo).then((user) => {
imageResizer(imgOriginalName)
.then(()=>{
models.User.create(userInfo)
}).then((user) => {
resolve(user) resolve(user)

@ -1,34 +0,0 @@
const fs = require('fs');
const path = require('path');
const jimp = require('jimp')
module.exports = function (image){
return new Promise ((resolve, reject)=>{
let imageAddress = path.join(__dirname + "/../public/images/" + image)
let imageNameWithOutExtn = image.slice(0 , image.indexOf('.') )
jimp.read(imageAddress)
.then(lenna => {
return lenna
.resize(157, 142) // resize
.quality(90) // set JPEG quality
.write(`./public/images/resized/${imageNameWithOutExtn}.png`); // save
})
.then((res)=>{
try {
fs.unlinkSync(__dirname + '/../public/images/' + image)
console.log('original picture is deleted .....')
console.log('----------------------------')
} catch (err) {
console.log(err);
console.log('original picture not deleted .....')
}
resolve(res)
})
.catch(err => {
reject(err)
});
})
}
Loading…
Cancel
Save