-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved file upload from the controller to the middleware.
- Loading branch information
Showing
9 changed files
with
185 additions
and
133 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "airbnb-base" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ config/database.json | |
config/server.json | ||
config/analytics.js | ||
themes/ | ||
admin/ | ||
admin/ | ||
package-lock.json |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Image upload middleware for avatar and image management. | ||
*/ | ||
const Crypto = require('crypto'); | ||
const Path = require('path'); | ||
|
||
function filterExtension(file) { | ||
const extension_extract = /(?:\.([^.]+))?$/; | ||
const extension = extension_extract.exec(file.name); | ||
// console.log(extension[1]); | ||
|
||
const allowed_extensions = [ | ||
'png', | ||
'gif', | ||
'jpg', | ||
'jpeg', | ||
'bmp', | ||
]; | ||
|
||
if (allowed_extensions.indexOf(extension[1]) === -1) { | ||
return { | ||
extension: extension[1], | ||
output: false, | ||
}; | ||
} | ||
|
||
return { | ||
extension: extension[1], | ||
output: true, | ||
}; | ||
} | ||
|
||
function avatar(req, res, next) { | ||
if (req.files) { | ||
const filter = filterExtension(req.files.file); // Check the file extension. | ||
if (filter.output) { | ||
// Generate a random string to be used as a file name. | ||
const file_name = Crypto.randomBytes(12).toString('hex'); | ||
const directory = Path.join(__dirname, '..', '..', 'public', 'uploads', 'profile', `${file_name}.${filter.extension}`); | ||
req.files.file.mv(directory, (mv_err) => { | ||
if (mv_err) { | ||
res.json({ | ||
error: 1, | ||
}); | ||
} else { | ||
req.upload = `${file_name}.${filter.extension}`; | ||
next(); | ||
} | ||
}); | ||
} else { | ||
res.json({ | ||
error: 1, | ||
}); | ||
} | ||
} else { | ||
res.json({ | ||
error: 1, | ||
}); | ||
} | ||
} | ||
|
||
function library(req, res, next) { | ||
if (req.files) { | ||
const filter = filterExtension(req.files.file); // Check the file extension. | ||
if (filter.output) { | ||
const file_name = Crypto.randomBytes(12).toString('hex'); | ||
const directory = Path.join(__dirname, '..', '..', 'public', 'uploads', 'images', `${file_name}.${filter.extension}`); | ||
req.files.file.mv(directory, (mv_err) => { | ||
if (mv_err) { | ||
res.json({ | ||
error: 1, | ||
}); | ||
} else { | ||
req.upload = `${file_name}.${filter.extension}`; | ||
next(); | ||
} | ||
}); | ||
} else { | ||
res.json({ | ||
error: 1, | ||
}); | ||
} | ||
} else { | ||
res.json({ | ||
error: 1, | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { avatar, library }; |
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
Oops, something went wrong.