]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
More robust actor image lazy load
authorChocobozzz <me@florianbigard.com>
Mon, 14 Jun 2021 14:14:45 +0000 (16:14 +0200)
committerChocobozzz <me@florianbigard.com>
Mon, 14 Jun 2021 14:14:45 +0000 (16:14 +0200)
server/controllers/lazy-static.ts
server/models/actor/actor-image.ts

index 27b1b7160054fecee2c3dc73cb81e24bf9a12855..9a7dacba049e86d3eb546dcc4bc91369ff591333 100644 (file)
@@ -48,7 +48,7 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function getActorImage (req: express.Request, res: express.Response) {
+async function getActorImage (req: express.Request, res: express.Response, next: express.NextFunction) {
   const filename = req.params.filename
 
   if (actorImagePathUnsafeCache.has(filename)) {
@@ -78,7 +78,23 @@ async function getActorImage (req: express.Request, res: express.Response) {
   const path = image.getPath()
 
   actorImagePathUnsafeCache.set(filename, path)
-  return res.sendFile(path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
+
+  return res.sendFile(path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER }, (err: any) => {
+    if (!err) return
+
+    // It seems this actor image is not on the disk anymore
+    if (err.status === HttpStatusCode.NOT_FOUND_404 && !image.isOwned()) {
+      logger.error('Cannot lazy serve actor image %s.', filename, { err })
+
+      actorImagePathUnsafeCache.del(filename)
+
+      image.onDisk = false
+      image.save()
+       .catch(err => logger.error('Cannot save new actor image disk state.', { err }))
+    }
+
+    return next(err)
+  })
 }
 
 async function getPreview (req: express.Request, res: express.Response) {
index a35f9edb0c76b0ef52ab712fef2db99e1f894921..98a7f6fbaa575d7bc7d6c68d334eb5c497897220 100644 (file)
@@ -98,4 +98,8 @@ export class ActorImageModel extends Model<Partial<AttributesOnly<ActorImageMode
     const imagePath = join(CONFIG.STORAGE.ACTOR_IMAGES, this.filename)
     return remove(imagePath)
   }
+
+  isOwned () {
+    return !this.fileUrl
+  }
 }