import { join } from 'path'
-import { FILES_CACHE, STATIC_PATHS } from '../../initializers/constants'
+import { FILES_CACHE } from '../../initializers/constants'
import { VideoModel } from '../../models/video/video'
import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
-import { CONFIG } from '../../initializers/config'
+import { doRequestAndSaveToFile } from '@server/helpers/requests'
class VideosPreviewCache extends AbstractVideoStaticFileCache <string> {
return this.instance || (this.instance = new this())
}
- async getFilePath (videoUUID: string) {
- const video = await VideoModel.loadByUUIDWithFile(videoUUID)
+ async getFilePathImpl (videoUUID: string) {
+ const video = await VideoModel.loadByUUID(videoUUID)
if (!video) return undefined
- if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
+ if (video.isOwned()) return { isOwned: true, path: video.getPreview().getPath() }
- return this.loadFromLRU(videoUUID)
+ return this.loadRemoteFile(videoUUID)
}
protected async loadRemoteFile (key: string) {
if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.')
- const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
- const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreviewName())
+ const preview = video.getPreview()
+ const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, preview.filename)
- return this.saveRemoteVideoFileAndReturnPath(video, remoteStaticPath, destPath)
+ const remoteUrl = preview.getFileUrl(video)
+ await doRequestAndSaveToFile({ uri: remoteUrl }, destPath)
+
+ return { isOwned: false, path: destPath }
}
}