utils.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { remove } from 'fs-extra'
  2. import { Instance as ParseTorrent } from 'parse-torrent'
  3. import { join } from 'path'
  4. import { sha256 } from '@shared/extra-utils'
  5. import { ResultList } from '@shared/models'
  6. import { CONFIG } from '../initializers/config'
  7. import { execPromise, execPromise2, randomBytesPromise } from './core-utils'
  8. import { logger } from './logger'
  9. function deleteFileAndCatch (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, extension = '.mp4') {
  28. const id = typeof target === 'string'
  29. ? target
  30. : target.infoHash
  31. const hash = sha256(id)
  32. return join(CONFIG.STORAGE.TMP_DIR, `${hash}-import${extension}`)
  33. }
  34. function getSecureTorrentName (originalName: string) {
  35. return sha256(originalName) + '.torrent'
  36. }
  37. async function getServerCommit () {
  38. try {
  39. const tag = await execPromise2(
  40. '[ ! -d .git ] || git name-rev --name-only --tags --no-undefined HEAD 2>/dev/null || true',
  41. { stdio: [ 0, 1, 2 ] }
  42. )
  43. if (tag) return tag.replace(/^v/, '')
  44. } catch (err) {
  45. logger.debug('Cannot get version from git tags.', { err })
  46. }
  47. try {
  48. const version = await execPromise('[ ! -d .git ] || git rev-parse --short HEAD')
  49. if (version) return version.toString().trim()
  50. } catch (err) {
  51. logger.debug('Cannot get version from git HEAD.', { err })
  52. }
  53. return ''
  54. }
  55. /**
  56. * From a filename like "ede4cba5-742b-46fa-a388-9a6eb3a3aeb3.mp4", returns
  57. * only the "ede4cba5-742b-46fa-a388-9a6eb3a3aeb3" part. If the filename does
  58. * not contain a UUID, returns null.
  59. */
  60. function getUUIDFromFilename (filename: string) {
  61. const regex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
  62. const result = filename.match(regex)
  63. if (!result || Array.isArray(result) === false) return null
  64. return result[0]
  65. }
  66. // ---------------------------------------------------------------------------
  67. export {
  68. deleteFileAndCatch,
  69. generateRandomString,
  70. getFormattedObjects,
  71. getSecureTorrentName,
  72. getServerCommit,
  73. generateVideoImportTmpPath,
  74. getUUIDFromFilename
  75. }