express-utils.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import * as express from 'express'
  2. import * as multer from 'multer'
  3. import { REMOTE_SCHEME } from '../initializers/constants'
  4. import { logger } from './logger'
  5. import { deleteFileAsync, generateRandomString } from './utils'
  6. import { extname } from 'path'
  7. import { isArray } from './custom-validators/misc'
  8. import { CONFIG } from '../initializers/config'
  9. function buildNSFWFilter (res?: express.Response, paramNSFW?: string) {
  10. if (paramNSFW === 'true') return true
  11. if (paramNSFW === 'false') return false
  12. if (paramNSFW === 'both') return undefined
  13. if (res && res.locals.oauth) {
  14. const user = res.locals.oauth.token.User
  15. // User does not want NSFW videos
  16. if (user.nsfwPolicy === 'do_not_list') return false
  17. // Both
  18. return undefined
  19. }
  20. if (CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list') return false
  21. // Display all
  22. return null
  23. }
  24. function cleanUpReqFiles (req: { files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[] }) {
  25. const files = req.files
  26. if (!files) return
  27. if (isArray(files)) {
  28. (files as Express.Multer.File[]).forEach(f => deleteFileAsync(f.path))
  29. return
  30. }
  31. for (const key of Object.keys(files)) {
  32. const file = files[ key ]
  33. if (isArray(file)) file.forEach(f => deleteFileAsync(f.path))
  34. else deleteFileAsync(file.path)
  35. }
  36. }
  37. function getHostWithPort (host: string) {
  38. const splitted = host.split(':')
  39. // The port was not specified
  40. if (splitted.length === 1) {
  41. if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
  42. return host + ':80'
  43. }
  44. return host
  45. }
  46. function badRequest (req: express.Request, res: express.Response) {
  47. return res.type('json').status(400).end()
  48. }
  49. function createReqFiles (
  50. fieldNames: string[],
  51. mimeTypes: { [ id: string ]: string },
  52. destinations: { [ fieldName: string ]: string }
  53. ) {
  54. const storage = multer.diskStorage({
  55. destination: (req, file, cb) => {
  56. cb(null, destinations[ file.fieldname ])
  57. },
  58. filename: async (req, file, cb) => {
  59. let extension: string
  60. const fileExtension = extname(file.originalname)
  61. const extensionFromMimetype = mimeTypes[ file.mimetype ]
  62. // Take the file extension if we don't understand the mime type
  63. // We have the OGG/OGV exception too because firefox sends a bad mime type when sending an OGG file
  64. if (fileExtension === '.ogg' || fileExtension === '.ogv' || !extensionFromMimetype) {
  65. extension = fileExtension
  66. } else {
  67. extension = extensionFromMimetype
  68. }
  69. let randomString = ''
  70. try {
  71. randomString = await generateRandomString(16)
  72. } catch (err) {
  73. logger.error('Cannot generate random string for file name.', { err })
  74. randomString = 'fake-random-string'
  75. }
  76. cb(null, randomString + extension)
  77. }
  78. })
  79. let fields: { name: string, maxCount: number }[] = []
  80. for (const fieldName of fieldNames) {
  81. fields.push({
  82. name: fieldName,
  83. maxCount: 1
  84. })
  85. }
  86. return multer({ storage }).fields(fields)
  87. }
  88. function isUserAbleToSearchRemoteURI (res: express.Response) {
  89. const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
  90. return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
  91. (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)
  92. }
  93. // ---------------------------------------------------------------------------
  94. export {
  95. buildNSFWFilter,
  96. getHostWithPort,
  97. isUserAbleToSearchRemoteURI,
  98. badRequest,
  99. createReqFiles,
  100. cleanUpReqFiles
  101. }