utilsController.js 1.8 KB

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