]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Fix remote avatar without AP mediatype field
authorChocobozzz <me@florianbigard.com>
Wed, 12 Feb 2020 09:23:24 +0000 (10:23 +0100)
committerChocobozzz <me@florianbigard.com>
Wed, 12 Feb 2020 09:23:24 +0000 (10:23 +0100)
server/initializers/constants.ts
server/lib/activitypub/actor.ts

index cb1c6f2ae1b176fd36242a86f2b9442050def8e3..311d371a71f7021ce6e5a3a8c5bd406d2e41ee51 100644 (file)
@@ -422,7 +422,8 @@ const MIMETYPES = {
       'image/png': '.png',
       'image/jpg': '.jpg',
       'image/jpeg': '.jpg'
-    }
+    },
+    EXT_MIMETYPE: null as { [ id: string ]: string }
   },
   VIDEO_CAPTIONS: {
     MIMETYPE_EXT: {
@@ -438,6 +439,7 @@ const MIMETYPES = {
   }
 }
 MIMETYPES.AUDIO.EXT_MIMETYPE = invert(MIMETYPES.AUDIO.MIMETYPE_EXT)
+MIMETYPES.IMAGE.EXT_MIMETYPE = invert(MIMETYPES.IMAGE.MIMETYPE_EXT)
 
 // ---------------------------------------------------------------------------
 
index 3f6edc070cadb9a929c86e5d988a574c21eeba08..8c5c618fc2e8ff80125fb9e5492eac220f0c7d91 100644 (file)
@@ -35,6 +35,7 @@ import {
   MActorId,
   MChannel
 } from '../../typings/models'
+import { extname } from 'path'
 
 // Set account keys, this could be long so process after the account creation and do not block the client
 function setAsyncActorKeys <T extends MActor> (actor: T) {
@@ -215,19 +216,21 @@ async function fetchActorTotalItems (url: string) {
 }
 
 function getAvatarInfoIfExists (actorJSON: ActivityPubActor) {
-  if (
-    actorJSON.icon && actorJSON.icon.type === 'Image' && MIMETYPES.IMAGE.MIMETYPE_EXT[actorJSON.icon.mediaType] !== undefined &&
-    isActivityPubUrlValid(actorJSON.icon.url)
-  ) {
-    const extension = MIMETYPES.IMAGE.MIMETYPE_EXT[actorJSON.icon.mediaType]
-
-    return {
-      name: uuidv4() + extension,
-      fileUrl: actorJSON.icon.url
-    }
-  }
+  const mimetypes = MIMETYPES.IMAGE
+  const icon = actorJSON.icon
+
+  if (!icon || icon.type !== 'Image' || !isActivityPubUrlValid(icon.url)) return undefined
+
+  const extension = icon.mediaType
+    ? mimetypes.MIMETYPE_EXT[icon.mediaType]
+    : extname(icon.url)
 
-  return undefined
+  if (!extension) return undefined
+
+  return {
+    name: uuidv4() + extension,
+    fileUrl: icon.url
+  }
 }
 
 async function addFetchOutboxJob (actor: Pick<ActorModel, 'id' | 'outboxUrl'>) {