Created
June 28, 2017 14:32
-
-
Save higordiego/854e9386c17da48099c3dc739033c1e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const multiparty = require('multiparty'); | |
const pass = require('../middleware/password') | |
const fs = require('fs') | |
const path = require('path') | |
function configPass (v) { | |
return pass.hash(v) | |
} | |
function cryptImage(fileName){ | |
const extension = fileName.substring(fileName.length - 4) | |
return configPass(fileName + new Date()) + extension; | |
} | |
function callbackRename(err){ | |
return err ? true : false | |
} | |
function write(atual, image, callback){ | |
return fs.rename(atual, image, callback) | |
} | |
function callback(error, fields, files){ | |
const path_public = path.join(__dirname, '../../', 'public/imagens'); | |
const path_atual = files.file[0].path; | |
const name = cryptImage(files.file[0].originalFilename) | |
const path_image = `${path_public}/${name}` | |
return write(path_atual, path_image, callbackRename) | |
} | |
function uploadImage(req){ | |
const form = new multiparty.Form(); | |
return form.parse(req, callback) | |
} | |
module.exports = { | |
uploadImage: uploadImage | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment