From c6de3a85eebbdef3029769ea6225277419ffbe00 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 12 Feb 2020 10:23:24 +0100 Subject: [PATCH] Fix remote avatar without AP mediatype field --- server/initializers/constants.ts | 4 +++- server/lib/activitypub/actor.ts | 27 +++++++++++++++------------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index cb1c6f2ae..311d371a7 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -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) // --------------------------------------------------------------------------- diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts index 3f6edc070..8c5c618fc 100644 --- a/server/lib/activitypub/actor.ts +++ b/server/lib/activitypub/actor.ts @@ -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 (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) { -- 2.25.1