aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/files-cache
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/files-cache')
-rw-r--r--server/lib/files-cache/abstract-video-static-file-cache.ts2
-rw-r--r--server/lib/files-cache/videos-torrent-cache.ts54
2 files changed, 55 insertions, 1 deletions
diff --git a/server/lib/files-cache/abstract-video-static-file-cache.ts b/server/lib/files-cache/abstract-video-static-file-cache.ts
index c06355446..af66689a0 100644
--- a/server/lib/files-cache/abstract-video-static-file-cache.ts
+++ b/server/lib/files-cache/abstract-video-static-file-cache.ts
@@ -2,7 +2,7 @@ import { remove } from 'fs-extra'
2import { logger } from '../../helpers/logger' 2import { logger } from '../../helpers/logger'
3import * as memoizee from 'memoizee' 3import * as memoizee from 'memoizee'
4 4
5type GetFilePathResult = { isOwned: boolean, path: string } | undefined 5type GetFilePathResult = { isOwned: boolean, path: string, downloadName?: string } | undefined
6 6
7export abstract class AbstractVideoStaticFileCache <T> { 7export abstract class AbstractVideoStaticFileCache <T> {
8 8
diff --git a/server/lib/files-cache/videos-torrent-cache.ts b/server/lib/files-cache/videos-torrent-cache.ts
new file mode 100644
index 000000000..ca0e1770d
--- /dev/null
+++ b/server/lib/files-cache/videos-torrent-cache.ts
@@ -0,0 +1,54 @@
1import { join } from 'path'
2import { doRequestAndSaveToFile } from '@server/helpers/requests'
3import { VideoFileModel } from '@server/models/video/video-file'
4import { CONFIG } from '../../initializers/config'
5import { FILES_CACHE } from '../../initializers/constants'
6import { VideoModel } from '../../models/video/video'
7import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
8
9class VideosTorrentCache extends AbstractVideoStaticFileCache <string> {
10
11 private static instance: VideosTorrentCache
12
13 private constructor () {
14 super()
15 }
16
17 static get Instance () {
18 return this.instance || (this.instance = new this())
19 }
20
21 async getFilePathImpl (filename: string) {
22 const file = await VideoFileModel.loadWithVideoOrPlaylistByTorrentFilename(filename)
23 if (!file) return undefined
24
25 if (file.getVideo().isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.TORRENTS_DIR, file.torrentFilename) }
26
27 return this.loadRemoteFile(filename)
28 }
29
30 // Key is the torrent filename
31 protected async loadRemoteFile (key: string) {
32 const file = await VideoFileModel.loadWithVideoOrPlaylistByTorrentFilename(key)
33 if (!file) return undefined
34
35 if (file.getVideo().isOwned()) throw new Error('Cannot load remote file of owned video.')
36
37 // Used to fetch the path
38 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(file.getVideo().id)
39 if (!video) return undefined
40
41 const remoteUrl = file.getRemoteTorrentUrl(video)
42 const destPath = join(FILES_CACHE.TORRENTS.DIRECTORY, file.torrentFilename)
43
44 await doRequestAndSaveToFile({ uri: remoteUrl }, destPath)
45
46 const downloadName = `${video.name}-${file.resolution}p.torrent`
47
48 return { isOwned: false, path: destPath, downloadName }
49 }
50}
51
52export {
53 VideosTorrentCache
54}