utilsController.js 1.7 KB

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