ニジカ投稿局 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-captions-simple-file-cache.ts 1.8 KiB

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