diff options
author | Chocobozzz <me@florianbigard.com> | 2019-03-19 14:23:17 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-03-19 14:30:43 +0100 |
commit | d74d29ad9e35929491cf37223398d2535ab23de0 (patch) | |
tree | 2812c9acbc05be0603eb671f8e6bd81086cf84d5 /server/lib/files-cache/videos-preview-cache.ts | |
parent | 9f79ade627f0044606a9fbbe16ca0154661d12b9 (diff) | |
download | PeerTube-d74d29ad9e35929491cf37223398d2535ab23de0.tar.gz PeerTube-d74d29ad9e35929491cf37223398d2535ab23de0.tar.zst PeerTube-d74d29ad9e35929491cf37223398d2535ab23de0.zip |
Limit user tokens cache
Diffstat (limited to 'server/lib/files-cache/videos-preview-cache.ts')
-rw-r--r-- | server/lib/files-cache/videos-preview-cache.ts | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/server/lib/files-cache/videos-preview-cache.ts b/server/lib/files-cache/videos-preview-cache.ts new file mode 100644 index 000000000..01cd3647e --- /dev/null +++ b/server/lib/files-cache/videos-preview-cache.ts | |||
@@ -0,0 +1,42 @@ | |||
1 | import { join } from 'path' | ||
2 | import { FILES_CACHE, CONFIG, STATIC_PATHS } from '../../initializers' | ||
3 | import { VideoModel } from '../../models/video/video' | ||
4 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' | ||
5 | |||
6 | class VideosPreviewCache extends AbstractVideoStaticFileCache <string> { | ||
7 | |||
8 | private static instance: VideosPreviewCache | ||
9 | |||
10 | private constructor () { | ||
11 | super() | ||
12 | } | ||
13 | |||
14 | static get Instance () { | ||
15 | return this.instance || (this.instance = new this()) | ||
16 | } | ||
17 | |||
18 | async getFilePath (videoUUID: string) { | ||
19 | const video = await VideoModel.loadByUUIDWithFile(videoUUID) | ||
20 | if (!video) return undefined | ||
21 | |||
22 | if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()) | ||
23 | |||
24 | return this.loadFromLRU(videoUUID) | ||
25 | } | ||
26 | |||
27 | protected async loadRemoteFile (key: string) { | ||
28 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(key) | ||
29 | if (!video) return undefined | ||
30 | |||
31 | if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.') | ||
32 | |||
33 | const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreviewName()) | ||
34 | const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreviewName()) | ||
35 | |||
36 | return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath) | ||
37 | } | ||
38 | } | ||
39 | |||
40 | export { | ||
41 | VideosPreviewCache | ||
42 | } | ||