ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

video-previews-simple-file-cache.ts 1.8 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { join } from 'path'
  2. import { FILES_CACHE } from '../../initializers/constants.js'
  3. import { VideoModel } from '../../models/video/video.js'
  4. import { AbstractSimpleFileCache } from './shared/abstract-simple-file-cache.js'
  5. import { doRequestAndSaveToFile } from '@server/helpers/requests.js'
  6. import { ThumbnailModel } from '@server/models/video/thumbnail.js'
  7. import { ThumbnailType } from '@peertube/peertube-models'
  8. import { logger } from '@server/helpers/logger.js'
  9. class VideoPreviewsSimpleFileCache extends AbstractSimpleFileCache <string> {
  10. private static instance: VideoPreviewsSimpleFileCache
  11. private constructor () {
  12. super()
  13. }
  14. static get Instance () {
  15. return this.instance || (this.instance = new this())
  16. }
  17. async getFilePathImpl (filename: string) {
  18. const thumbnail = await ThumbnailModel.loadWithVideoByFilename(filename, ThumbnailType.PREVIEW)
  19. if (!thumbnail) return undefined
  20. if (thumbnail.Video.isOwned()) return { isOwned: true, path: thumbnail.getPath() }
  21. return this.loadRemoteFile(thumbnail.Video.uuid)
  22. }
  23. // Key is the video UUID
  24. protected async loadRemoteFile (key: string) {
  25. const video = await VideoModel.loadFull(key)
  26. if (!video) return undefined
  27. if (video.isOwned()) throw new Error('Cannot load remote preview of owned video.')
  28. const preview = video.getPreview()
  29. const destPath = join(FILES_CACHE.PREVIEWS.DIRECTORY, preview.filename)
  30. const remoteUrl = preview.getOriginFileUrl(video)
  31. try {
  32. await doRequestAndSaveToFile(remoteUrl, destPath)
  33. logger.debug('Fetched remote preview %s to %s.', remoteUrl, destPath)
  34. return { isOwned: false, path: destPath }
  35. } catch (err) {
  36. logger.info('Cannot fetch remote preview file %s.', remoteUrl, { err })
  37. return undefined
  38. }
  39. }
  40. }
  41. export {
  42. VideoPreviewsSimpleFileCache
  43. }