]>
Commit | Line | Data |
---|---|---|
90a8bd30 C |
1 | import { join } from 'path' |
2 | import { doRequestAndSaveToFile } from '@server/helpers/requests' | |
3 | import { VideoFileModel } from '@server/models/video/video-file' | |
4 | import { CONFIG } from '../../initializers/config' | |
5 | import { FILES_CACHE } from '../../initializers/constants' | |
6 | import { VideoModel } from '../../models/video/video' | |
7 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' | |
8 | ||
9 | class 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 | ||
52 | export { | |
53 | VideosTorrentCache | |
54 | } |