2
1

utils.ts 3.0 KB

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