]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/utils.ts
Add torrent tests
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
index f4cc5547d49ddacbb1b57f4d528c13a6b584fabf..eaad555553527b98549e69580203f0357063b0ee 100644 (file)
@@ -6,11 +6,11 @@ import { CONFIG } from '../initializers'
 import { UserModel } from '../models/account/user'
 import { ActorModel } from '../models/activitypub/actor'
 import { ApplicationModel } from '../models/application/application'
-import { pseudoRandomBytesPromise, unlinkPromise } from './core-utils'
+import { pseudoRandomBytesPromise, sha256, unlinkPromise } from './core-utils'
 import { logger } from './logger'
 import { isArray } from './custom-validators/misc'
-import * as crypto from "crypto"
-import { join } from "path"
+import { join } from 'path'
+import { Instance as ParseTorrent } from 'parse-torrent'
 
 const isCidr = require('is-cidr')
 
@@ -183,13 +183,18 @@ async function getServerActor () {
   return Promise.resolve(serverActor)
 }
 
-function generateVideoTmpPath (id: string) {
-  const hash = crypto.createHash('sha256').update(id).digest('hex')
+function generateVideoTmpPath (target: string | ParseTorrent) {
+  const id = typeof target === 'string' ? target : target.infoHash
+
+  const hash = sha256(id)
   return join(CONFIG.STORAGE.VIDEOS_DIR, hash + '-import.mp4')
 }
 
-type SortType = { sortModel: any, sortValue: string }
+function getSecureTorrentName (originalName: string) {
+  return sha256(originalName) + '.torrent'
+}
 
+type SortType = { sortModel: any, sortValue: string }
 
 // ---------------------------------------------------------------------------
 
@@ -199,6 +204,7 @@ export {
   generateRandomString,
   getFormattedObjects,
   isSignupAllowed,
+  getSecureTorrentName,
   isSignupAllowedForCurrentIP,
   computeResolutionsToTranscode,
   resetSequelizeInstance,