Be more robust with missing thumbnails/previews
authorChocobozzz <me@florianbigard.com>
Thu, 6 Feb 2020 16:39:19 +0000 (17:39 +0100)
committerChocobozzz <me@florianbigard.com>
Thu, 6 Feb 2020 16:39:19 +0000 (17:39 +0100)
server/lib/activitypub/videos.ts
server/models/video/video.ts

index 7d8296e45e75b38267c02eec29616178feb62aea..7386c2e5388b6ae1e0750d4c41e7d6625eea1461 100644 (file)
@@ -340,9 +340,11 @@ async function updateVideoFromAP (options: {
 
       if (thumbnailModel) await videoUpdated.addAndSaveThumbnail(thumbnailModel, t)
 
-      const previewUrl = videoUpdated.getPreview().getFileUrl(videoUpdated)
-      const previewModel = createPlaceholderThumbnail(previewUrl, video, ThumbnailType.PREVIEW, PREVIEWS_SIZE)
-      await videoUpdated.addAndSaveThumbnail(previewModel, t)
+      if (videoUpdated.getPreview()) {
+        const previewUrl = videoUpdated.getPreview().getFileUrl(videoUpdated)
+        const previewModel = createPlaceholderThumbnail(previewUrl, video, ThumbnailType.PREVIEW, PREVIEWS_SIZE)
+        await videoUpdated.addAndSaveThumbnail(previewModel, t)
+      }
 
       {
         const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoUpdated, videoObject.url)
@@ -531,6 +533,10 @@ async function createVideo (videoObject: VideoTorrentObject, channel: MChannelAc
   const video = VideoModel.build(videoData) as MVideoThumbnail
 
   const promiseThumbnail = createVideoMiniatureFromUrl(getThumbnailFromIcons(videoObject).url, video, ThumbnailType.MINIATURE)
+    .catch(err => {
+      logger.error('Cannot create miniature from url.', { err })
+      return undefined
+    })
 
   let thumbnailModel: MThumbnail
   if (waitThumbnail === true) {
@@ -602,11 +608,15 @@ async function createVideo (videoObject: VideoTorrentObject, channel: MChannelAc
   })
 
   if (waitThumbnail === false) {
+    // Error is already caught above
+    // eslint-disable-next-line @typescript-eslint/no-floating-promises
     promiseThumbnail.then(thumbnailModel => {
+      if (!thumbnailModel) return
+
       thumbnailModel = videoCreated.id
 
       return thumbnailModel.save()
-    }).catch(err => logger.error('Cannot create miniature from url.', { err }))
+    })
   }
 
   return { autoBlacklisted, videoCreated }
index 5964526a997061c2503d361043d77935e10dc40b..bd4ca63ea643ea70e38ecb448942a7ceb1fff455 100644 (file)
@@ -1937,6 +1937,10 @@ export class VideoModel extends Model<VideoModel> {
     return this.uuid + '.jpg'
   }
 
+  hasPreview () {
+    return !!this.getPreview()
+  }
+
   getPreview () {
     if (Array.isArray(this.Thumbnails) === false) return undefined