utilsController.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const path = require('path')
  2. const config = require('../config.js')
  3. const fs = require('fs')
  4. const gm = require('gm')
  5. const ffmpeg = require('fluent-ffmpeg')
  6. const utilsController = {}
  7. utilsController.getPrettyDate = function(date) {
  8. return date.getFullYear() + '-'
  9. + (date.getMonth() + 1) + '-'
  10. + date.getDate() + ' '
  11. + (date.getHours() < 10 ? '0' : '')
  12. + date.getHours() + ':'
  13. + (date.getMinutes() < 10 ? '0' : '')
  14. + date.getMinutes() + ':'
  15. + (date.getSeconds() < 10 ? '0' : '')
  16. + date.getSeconds()
  17. }
  18. utilsController.generateThumbs = function(file, basedomain) {
  19. if (config.uploads.generateThumbnails !== true) return
  20. let extensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png', '.webm', '.mp4']
  21. for (let ext of extensions) {
  22. if (path.extname(file.name).toLowerCase() === ext) {
  23. let thumbname = path.join(__dirname, '..', config.uploads.folder, 'thumbs', file.name.slice(0, -ext.length) + '.png')
  24. fs.access(thumbname, function(err) {
  25. if (err && err.code === 'ENOENT') {
  26. // File doesnt exist
  27. if (ext === '.webm' || ext === '.mp4') {
  28. ffmpeg(path.join(__dirname, '..', config.uploads.folder, file.name))
  29. .thumbnail({
  30. timestamps: [0],
  31. filename: '%b.png',
  32. folder: path.join(__dirname, '..', config.uploads.folder, 'thumbs'),
  33. size: '200x?'
  34. })
  35. .on('error', (error) => {
  36. console.log('Error - ', error.message)
  37. })
  38. } else {
  39. let size = {
  40. width: 200,
  41. height: 200
  42. }
  43. gm(path.join(__dirname, '..', config.uploads.folder, file.name))
  44. .resize(size.width, size.height + '>')
  45. .gravity('Center')
  46. .extent(size.width, size.height)
  47. .background('transparent')
  48. .write(thumbname, (error) => {
  49. if (error) console.log('Error - ', error)
  50. })
  51. }
  52. }
  53. })
  54. }
  55. }
  56. }
  57. module.exports = utilsController