videos-preview-cache.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { join } from 'path'
  2. import { FILES_CACHE, STATIC_PATHS } from '../../initializers/constants'
  3. import { VideoModel } from '../../models/video/video'
  4. import { AbstractVideoStaticFileCache } from './abstract-video-static-file-cache'
  5. import { CONFIG } from '../../initializers/config'
  6. import { fetchRemoteVideoStaticFile } from '../activitypub'
  7. class VideosPreviewCache extends AbstractVideoStaticFileCache <string> {
  8. private static instance: VideosPreviewCache
  9. private constructor () {
  10. super()
  11. }
  12. static get Instance () {
  13. return this.instance || (this.instance = new this())
  14. }
  15. async getFilePathImpl (videoUUID: string) {
  16. const video = await VideoModel.loadByUUIDWithFile(videoUUID)
  17. if (!video) return undefined
  18. if (video.isOwned()) return { isOwned: true, path: video.getPreview().getPath() }
  19. return this.loadRemoteFile(videoUUID)
  20. }
  21. protected async loadRemoteFile (key: string) {
  22. const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(key)
  23. if (!video) return undefined
  24. if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.')
  25. // FIXME: use URL
  26. const remoteStaticPath = join(STATIC_PATHS.PREVIEWS, video.getPreview().filename)
  27. const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, video.getPreview().filename)
  28. await fetchRemoteVideoStaticFile(video, remoteStaticPath, destPath)
  29. return { isOwned: false, path: destPath }
  30. }
  31. }
  32. export {
  33. VideosPreviewCache
  34. }