import * as asyncLRU from 'async-lru'
-import { join } from 'path'
import { createWriteStream } from 'fs'
-
-import { database as db, CONFIG, CACHE } from '../../initializers'
+import { join } from 'path'
import { logger, unlinkPromise } from '../../helpers'
-import { VideoInstance } from '../../models'
-import { fetchRemotePreview } from '../../lib'
+import { CACHE, CONFIG } from '../../initializers'
+import { VideoModel } from '../../models/video/video'
+import { fetchRemoteVideoPreview } from '../activitypub'
class VideosPreviewCache {
})
}
- getPreviewPath (key: string) {
+ async getPreviewPath (key: string) {
+ const video = await VideoModel.loadByUUID(key)
+ if (!video) return undefined
+
+ if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
+
return new Promise<string>((res, rej) => {
this.lru.get(key, (err, value) => {
err ? rej(err) : res(value)
}
private async loadPreviews (key: string) {
- const video = await db.Video.loadByUUIDAndPopulateAccountAndPodAndTags(key)
+ const video = await VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(key)
if (!video) return undefined
- if (video.isOwned()) return join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName())
+ if (video.isOwned()) throw new Error('Cannot load preview of owned video.')
const res = await this.saveRemotePreviewAndReturnPath(video)
return res
}
- private saveRemotePreviewAndReturnPath (video: VideoInstance) {
- const req = fetchRemotePreview(video)
+ private saveRemotePreviewAndReturnPath (video: VideoModel) {
+ const req = fetchRemoteVideoPreview(video)
return new Promise<string>((res, rej) => {
const path = join(CACHE.DIRECTORIES.PREVIEWS, video.getPreviewName())