]>
Commit | Line | Data |
---|---|---|
3fd3ab2d | 1 | import { join } from 'path' |
74dc3bca | 2 | import { FILES_CACHE, STATIC_PATHS } from '../../initializers/constants' |
3fd3ab2d | 3 | import { VideoModel } from '../../models/video/video' |
40e87e9e | 4 | import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache' |
6dd9de95 | 5 | import { CONFIG } from '../../initializers/config' |
dc852737 | 6 | import { fetchRemoteVideoStaticFile } from '../activitypub' |
f981dae8 | 7 | |
40e87e9e | 8 | class VideosPreviewCache extends AbstractVideoStaticFileCache <string> { |
f981dae8 C |
9 | |
10 | private static instance: VideosPreviewCache | |
11 | ||
40e87e9e C |
12 | private constructor () { |
13 | super() | |
14 | } | |
f981dae8 C |
15 | |
16 | static get Instance () { | |
17 | return this.instance || (this.instance = new this()) | |
18 | } | |
19 | ||
e8bafea3 | 20 | async getFilePathImpl (videoUUID: string) { |
627621c1 | 21 | const video = await VideoModel.loadByUUIDWithFile(videoUUID) |
8fa5653a C |
22 | if (!video) return undefined |
23 | ||
3acc5084 | 24 | if (video.isOwned()) return { isOwned: true, path: join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreview().filename) } |
8fa5653a | 25 | |
e8bafea3 | 26 | return this.loadRemoteFile(videoUUID) |
f981dae8 C |
27 | } |
28 | ||
40e87e9e | 29 | protected async loadRemoteFile (key: string) { |
627621c1 | 30 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(key) |
f5028693 | 31 | if (!video) return undefined |
f981dae8 | 32 | |
40e87e9e | 33 | if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.') |
f981dae8 | 34 | |
e8bafea3 C |
35 | // FIXME: use URL |
36 | const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreview().filename) | |
37 | const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreview().filename) | |
f981dae8 | 38 | |
dc852737 | 39 | await fetchRemoteVideoStaticFile(video, remoteStaticPath, destPath) |
3acc5084 | 40 | |
dc852737 | 41 | return { isOwned: false, path: destPath } |
f981dae8 C |
42 | } |
43 | } | |
44 | ||
45 | export { | |
46 | VideosPreviewCache | |
47 | } |