diff --git a/server/commands/userProvisioner.ts b/server/commands/userProvisioner.ts index 0a280a2eec..e326f6aee5 100644 --- a/server/commands/userProvisioner.ts +++ b/server/commands/userProvisioner.ts @@ -173,6 +173,13 @@ export default async function userProvisioner( ); }); + if (avatarUrl && !existingUser.getFlag(UserFlag.AvatarUpdated)) { + await new UploadUserAvatarTask().schedule({ + userId: existingUser.id, + avatarUrl, + }); + } + if (isInvite) { const inviter = await existingUser.$get("invitedBy"); if (inviter) { diff --git a/server/queues/tasks/UploadTeamAvatarTask.ts b/server/queues/tasks/UploadTeamAvatarTask.ts index fbf99ea693..14096b3b31 100644 --- a/server/queues/tasks/UploadTeamAvatarTask.ts +++ b/server/queues/tasks/UploadTeamAvatarTask.ts @@ -40,7 +40,7 @@ export default class UploadTeamAvatarTask extends BaseTask { }); if (attachment) { - await team.update({ avatarUrl: attachment.url }); + await team.update({ avatarUrl: attachment.redirectUrl }); } } diff --git a/server/queues/tasks/UploadUserAvatarTask.ts b/server/queues/tasks/UploadUserAvatarTask.ts index 40e182e8ba..9d11bb016a 100644 --- a/server/queues/tasks/UploadUserAvatarTask.ts +++ b/server/queues/tasks/UploadUserAvatarTask.ts @@ -2,7 +2,7 @@ import { createHash } from "node:crypto"; import { AttachmentPreset } from "@shared/types"; import attachmentCreator from "@server/commands/attachmentCreator"; import { createContext } from "@server/context"; -import { User } from "@server/models"; +import { Attachment, User } from "@server/models"; import { BaseTask, TaskPriority } from "./base/BaseTask"; type Props = { @@ -24,11 +24,24 @@ export default class UploadUserAvatarTask extends BaseTask { const hash = createHash("sha256").update(props.avatarUrl).digest("hex"); - // If the user's avatar URL already contains this hash, skip the upload + // If the user's avatar URL already contains this hash, skip the upload. + // This handles old-style canonical S3 URLs that include the hash in the path. if (user.avatarUrl?.includes(hash)) { return; } + // For redirect-style avatar URLs, check if the underlying attachment + // already has this hash in its key to avoid re-uploading the same avatar. + const redirectMatch = user.avatarUrl?.match( + /attachments\.redirect\?id=([^&]+)/ + ); + if (redirectMatch) { + const existing = await Attachment.findByPk(redirectMatch[1]); + if (existing?.key.endsWith(`/${hash}`)) { + return; + } + } + const attachment = await attachmentCreator({ name: hash, url: props.avatarUrl, @@ -38,7 +51,7 @@ export default class UploadUserAvatarTask extends BaseTask { }); if (attachment) { - await user.update({ avatarUrl: attachment.url }); + await user.update({ avatarUrl: attachment.redirectUrl }); } }