diff options
author | Chocobozzz <me@florianbigard.com> | 2021-02-16 16:25:53 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2021-02-18 13:38:09 +0100 |
commit | 90a8bd305de4153ec21137a73ff482dcc2e3e19b (patch) | |
tree | 2e35b5504ec11bc51579c92a70c77ed3d5ace816 /server/lib/files-cache | |
parent | 684cdacbbd775b5f404dd7b373e02dd21baf5ff0 (diff) | |
download | PeerTube-90a8bd305de4153ec21137a73ff482dcc2e3e19b.tar.gz PeerTube-90a8bd305de4153ec21137a73ff482dcc2e3e19b.tar.zst PeerTube-90a8bd305de4153ec21137a73ff482dcc2e3e19b.zip |
Dissociate video file names and video uuid
Diffstat (limited to 'server/lib/files-cache')
-rw-r--r-- | server/lib/files-cache/abstract-video-static-file-cache.ts | 2 | ||||
-rw-r--r-- | server/lib/files-cache/videos-torrent-cache.ts | 54 |
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' | |||
2 | import { logger } from '../../helpers/logger' | 2 | import { logger } from '../../helpers/logger' |
3 | import * as memoizee from 'memoizee' | 3 | import * as memoizee from 'memoizee' |
4 | 4 | ||
5 | type GetFilePathResult = { isOwned: boolean, path: string } | undefined | 5 | type GetFilePathResult = { isOwned: boolean, path: string, downloadName?: string } | undefined |
6 | 6 | ||
7 | export abstract class AbstractVideoStaticFileCache <T> { | 7 | export 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 @@ | |||
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 | } | ||