utils.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { ResultList } from '../../shared'
  2. import { execPromise, execPromise2, randomBytesPromise, sha256 } from './core-utils'
  3. import { logger } from './logger'
  4. import { join } from 'path'
  5. import { Instance as ParseTorrent } from 'parse-torrent'
  6. import { remove } from 'fs-extra'
  7. import { CONFIG } from '../initializers/config'
  8. import { isVideoFileExtnameValid } from './custom-validators/videos'
  9. function deleteFileAsync (path: string) {
  10. remove(path)
  11. .catch(err => logger.error('Cannot delete the file %s asynchronously.', path, { err }))
  12. }
  13. async function generateRandomString (size: number) {
  14. const raw = await randomBytesPromise(size)
  15. return raw.toString('hex')
  16. }
  17. interface FormattableToJSON<U, V> {
  18. toFormattedJSON (args?: U): V
  19. }
  20. function getFormattedObjects<U, V, T extends FormattableToJSON<U, V>> (objects: T[], objectsTotal: number, formattedArg?: U) {
  21. const formattedObjects = objects.map(o => o.toFormattedJSON(formattedArg))
  22. return {
  23. total: objectsTotal,
  24. data: formattedObjects
  25. } as ResultList<V>
  26. }
  27. function generateVideoImportTmpPath (target: string | ParseTorrent, extensionArg?: string) {
  28. const id = typeof target === 'string'
  29. ? target
  30. : target.infoHash
  31. let extension = '.mp4'
  32. if (extensionArg && isVideoFileExtnameValid(extensionArg)) {
  33. extension = extensionArg
  34. }
  35. const hash = sha256(id)
  36. return join(CONFIG.STORAGE.TMP_DIR, `${hash}-import${extension}`)
  37. }
  38. function getSecureTorrentName (originalName: string) {
  39. return sha256(originalName) + '.torrent'
  40. }
  41. async function getServerCommit () {
  42. try {
  43. const tag = await execPromise2(
  44. '[ ! -d .git ] || git name-rev --name-only --tags --no-undefined HEAD 2>/dev/null || true',
  45. { stdio: [ 0, 1, 2 ] }
  46. )
  47. if (tag) return tag.replace(/^v/, '')
  48. } catch (err) {
  49. logger.debug('Cannot get version from git tags.', { err })
  50. }
  51. try {
  52. const version = await execPromise('[ ! -d .git ] || git rev-parse --short HEAD')
  53. if (version) return version.toString().trim()
  54. } catch (err) {
  55. logger.debug('Cannot get version from git HEAD.', { err })
  56. }
  57. return ''
  58. }
  59. /**
  60. * From a filename like "ede4cba5-742b-46fa-a388-9a6eb3a3aeb3.mp4", returns
  61. * only the "ede4cba5-742b-46fa-a388-9a6eb3a3aeb3" part. If the filename does
  62. * not contain a UUID, returns null.
  63. */
  64. function getUUIDFromFilename (filename: string) {
  65. const regex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
  66. const result = filename.match(regex)
  67. if (!result || Array.isArray(result) === false) return null
  68. return result[0]
  69. }
  70. // ---------------------------------------------------------------------------
  71. export {
  72. deleteFileAsync,
  73. generateRandomString,
  74. getFormattedObjects,
  75. getSecureTorrentName,
  76. getServerCommit,
  77. generateVideoImportTmpPath,
  78. getUUIDFromFilename
  79. }