]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/actor.ts
Update translations
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / actor.ts
index 3f6edc070cadb9a929c86e5d988a574c21eeba08..9eabef4b0b175da39173af1825a62435256f7cc8 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) {
@@ -175,8 +176,8 @@ async function updateActorAvatarInstance (actor: MActorDefault, info: AvatarInfo
   if (!info.name) return actor
 
   if (actor.Avatar) {
-    // Don't update the avatar if the filename did not change
-    if (actor.Avatar.fileUrl === info.fileUrl) return actor
+    // Don't update the avatar if the file URL did not change
+    if (info.fileUrl && actor.Avatar.fileUrl === info.fileUrl) return actor
 
     try {
       await actor.Avatar.destroy({ transaction: t })
@@ -215,19 +216,27 @@ 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
+
+  let extension: string
+
+  if (icon.mediaType) {
+    extension = mimetypes.MIMETYPE_EXT[icon.mediaType]
+  } else {
+    const tmp = extname(icon.url)
+
+    if (mimetypes.EXT_MIMETYPE[tmp] !== undefined) extension = tmp
   }
 
-  return undefined
+  if (!extension) return undefined
+
+  return {
+    name: uuidv4() + extension,
+    fileUrl: icon.url
+  }
 }
 
 async function addFetchOutboxJob (actor: Pick<ActorModel, 'id' | 'outboxUrl'>) {