]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/files-cache/videos-preview-cache.ts
Update translations
[github/Chocobozzz/PeerTube.git] / server / lib / files-cache / videos-preview-cache.ts
CommitLineData
3fd3ab2d 1import { join } from 'path'
a1587156 2import { FILES_CACHE } from '../../initializers/constants'
3fd3ab2d 3import { VideoModel } from '../../models/video/video'
40e87e9e 4import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
ca6d3622 5import { doRequestAndSaveToFile } from '@server/helpers/requests'
a8b1b404
C
6import { ThumbnailModel } from '@server/models/video/thumbnail'
7import { ThumbnailType } from '@shared/models'
8import { logger } from '@server/helpers/logger'
f981dae8 9
40e87e9e 10class VideosPreviewCache extends AbstractVideoStaticFileCache <string> {
f981dae8
C
11
12 private static instance: VideosPreviewCache
13
40e87e9e
C
14 private constructor () {
15 super()
16 }
f981dae8
C
17
18 static get Instance () {
19 return this.instance || (this.instance = new this())
20 }
21
a8b1b404 22 async getFilePathImpl (filename: string) {
a35a2279 23 const thumbnail = await ThumbnailModel.loadWithVideoByFilename(filename, ThumbnailType.PREVIEW)
a8b1b404 24 if (!thumbnail) return undefined
8fa5653a 25
a8b1b404 26 if (thumbnail.Video.isOwned()) return { isOwned: true, path: thumbnail.getPath() }
8fa5653a 27
a8b1b404 28 return this.loadRemoteFile(thumbnail.Video.uuid)
f981dae8
C
29 }
30
6302d599 31 // Key is the video UUID
40e87e9e 32 protected async loadRemoteFile (key: string) {
4fae2b1f 33 const video = await VideoModel.loadFull(key)
f5028693 34 if (!video) return undefined
f981dae8 35
40e87e9e 36 if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.')
f981dae8 37
ca6d3622
C
38 const preview = video.getPreview()
39 const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, preview.filename)
cb0eda56 40 const remoteUrl = preview.getOriginFileUrl(video)
3acc5084 41
cd25344f
C
42 try {
43 await doRequestAndSaveToFile(remoteUrl, destPath)
44
45 logger.debug('Fetched remote preview %s to %s.', remoteUrl, destPath)
46
47 return { isOwned: false, path: destPath }
48 } catch (err) {
49 logger.info('Cannot fetch remote preview file %s.', remoteUrl, { err })
a8b1b404 50
cd25344f
C
51 return undefined
52 }
f981dae8
C
53 }
54}
55
56export {
57 VideosPreviewCache
58}