From e33aa5f0c78f6d1664433405c6d705a507a8aeac Mon Sep 17 00:00:00 2001 From: Stephen Zhou Date: Tue, 8 Oct 2024 15:46:37 +0800 Subject: [PATCH] feat: image proxy for avatar images (#524) Co-authored-by: hyoban --- apps/renderer/src/components/feed-certification.tsx | 3 ++- apps/renderer/src/components/user-button.tsx | 3 ++- apps/renderer/src/lib/img-proxy.ts | 12 ++++++++++-- .../entry-content/components/EntryReadHistory.tsx | 8 ++++++-- .../src/modules/profile/user-profile-modal.tsx | 5 +++-- .../src/modules/settings/tabs/invitations.tsx | 5 ++++- .../tabs/wallet/transaction-section/index.tsx | 3 ++- .../(external)/(with-layout)/profile/[id]/index.tsx | 3 ++- packages/shared/src/image.ts | 8 ++++++++ 9 files changed, 39 insertions(+), 11 deletions(-) diff --git a/apps/renderer/src/components/feed-certification.tsx b/apps/renderer/src/components/feed-certification.tsx index b62f94b37..dcfd324ba 100644 --- a/apps/renderer/src/components/feed-certification.tsx +++ b/apps/renderer/src/components/feed-certification.tsx @@ -3,6 +3,7 @@ import { useTranslation } from "react-i18next" import { useWhoami } from "~/atoms/user" import { Avatar, AvatarFallback, AvatarImage } from "~/components/ui/avatar" import { Tooltip, TooltipContent, TooltipPortal, TooltipTrigger } from "~/components/ui/tooltip" +import { replaceImgUrlIfNeed } from "~/lib/img-proxy" import { cn } from "~/lib/utils" import type { FeedOrListRespModel } from "~/models" import { usePresentUserProfileModal } from "~/modules/profile/hooks" @@ -60,7 +61,7 @@ export const FeedCertification = ({ presentUserProfile(feed.owner!.id) }} > - + {feed.owner.name?.slice(0, 2)} ) : ( diff --git a/apps/renderer/src/components/user-button.tsx b/apps/renderer/src/components/user-button.tsx index 9c361f721..5defacfa9 100644 --- a/apps/renderer/src/components/user-button.tsx +++ b/apps/renderer/src/components/user-button.tsx @@ -10,6 +10,7 @@ import { useAuthQuery } from "~/hooks/common" import { apiClient } from "~/lib/api-fetch" import { defineQuery } from "~/lib/defineQuery" import { nextFrame } from "~/lib/dom" +import { replaceImgUrlIfNeed } from "~/lib/img-proxy" import { cn } from "~/lib/utils" import { useAchievementModal } from "~/modules/achievement/hooks" import { LoginModalContent } from "~/modules/auth/LoginModalContent" @@ -203,7 +204,7 @@ export function UserAvatar({ > {renderUserData?.name?.slice(0, 2)} diff --git a/apps/renderer/src/lib/img-proxy.ts b/apps/renderer/src/lib/img-proxy.ts index 8345002bd..45c470013 100644 --- a/apps/renderer/src/lib/img-proxy.ts +++ b/apps/renderer/src/lib/img-proxy.ts @@ -1,4 +1,4 @@ -import { imageRefererMatches } from "@follow/shared/image" +import { imageRefererMatches, webpCloudPublicServicesMatches } from "@follow/shared/image" import { getAbValue, isAbEnabled } from "~/hooks/biz/useAb" @@ -19,7 +19,15 @@ export const getImageProxyUrl = ({ } } -export const replaceImgUrlIfNeed = (url: string) => { +export const replaceImgUrlIfNeed = (url?: string) => { + if (!url) return url + + for (const rule of webpCloudPublicServicesMatches) { + if (rule.url.test(url)) { + return url.replace(rule.url, rule.target) + } + } + for (const rule of imageRefererMatches) { if (rule.url.test(url)) { return getImageProxyUrl({ url, width: 0, height: 0 }) diff --git a/apps/renderer/src/modules/entry-content/components/EntryReadHistory.tsx b/apps/renderer/src/modules/entry-content/components/EntryReadHistory.tsx index d83672df5..92de0a1f7 100644 --- a/apps/renderer/src/modules/entry-content/components/EntryReadHistory.tsx +++ b/apps/renderer/src/modules/entry-content/components/EntryReadHistory.tsx @@ -15,6 +15,7 @@ import { ScrollArea } from "~/components/ui/scroll-area" import { Tooltip, TooltipContent, TooltipPortal, TooltipTrigger } from "~/components/ui/tooltip" import { EllipsisHorizontalTextWithTooltip } from "~/components/ui/typography" import { useAuthQuery } from "~/hooks/common" +import { replaceImgUrlIfNeed } from "~/lib/img-proxy" import { useAppLayoutGridContainerWidth } from "~/providers/app-grid-layout-container-provider" import { Queries } from "~/queries" import { useEntryReadHistory } from "~/store/entry" @@ -121,7 +122,7 @@ const EntryUserRow: Component<{ userId: string }> = memo(({ userId }) => { className="relative flex min-w-0 max-w-[50ch] shrink-0 cursor-button items-center gap-2 truncate rounded-md p-1 px-2 hover:bg-muted" > - + {user.name?.slice(0, 2)} @@ -161,7 +162,10 @@ const EntryUser: Component<{ }} > - + {user.name?.slice(0, 2)} diff --git a/apps/renderer/src/modules/profile/user-profile-modal.tsx b/apps/renderer/src/modules/profile/user-profile-modal.tsx index 01133b8b8..d8a300325 100644 --- a/apps/renderer/src/modules/profile/user-profile-modal.tsx +++ b/apps/renderer/src/modules/profile/user-profile-modal.tsx @@ -20,6 +20,7 @@ import { useAuthQuery, useI18n } from "~/hooks/common" import { apiClient } from "~/lib/api-fetch" import { defineQuery } from "~/lib/defineQuery" import { nextFrame, stopPropagation } from "~/lib/dom" +import { replaceImgUrlIfNeed } from "~/lib/img-proxy" import { getStorageNS } from "~/lib/ns" import { cn } from "~/lib/utils" import type { SubscriptionModel } from "~/models" @@ -246,7 +247,7 @@ export const UserProfileModalContent: FC<{ @@ -291,7 +292,7 @@ export const UserProfileModalContent: FC<{ size="large" icon={ - + {userInfo.name?.slice(0, 2)} } diff --git a/apps/renderer/src/modules/settings/tabs/invitations.tsx b/apps/renderer/src/modules/settings/tabs/invitations.tsx index a3e1ebf76..b6fabb14b 100644 --- a/apps/renderer/src/modules/settings/tabs/invitations.tsx +++ b/apps/renderer/src/modules/settings/tabs/invitations.tsx @@ -23,6 +23,7 @@ import { INVITATION_PRICE } from "~/constants" import { useAuthQuery } from "~/hooks/common" import { apiClient } from "~/lib/api-fetch" import { toastFetchError } from "~/lib/error-parser" +import { replaceImgUrlIfNeed } from "~/lib/img-proxy" import { usePresentUserProfileModal } from "~/modules/profile/hooks" import { Queries } from "~/queries" @@ -122,7 +123,9 @@ export const SettingInvitations = () => { }} > - + {row.users?.name?.slice(0, 2)} diff --git a/apps/renderer/src/modules/settings/tabs/wallet/transaction-section/index.tsx b/apps/renderer/src/modules/settings/tabs/wallet/transaction-section/index.tsx index d4d866a24..bdb85c762 100644 --- a/apps/renderer/src/modules/settings/tabs/wallet/transaction-section/index.tsx +++ b/apps/renderer/src/modules/settings/tabs/wallet/transaction-section/index.tsx @@ -16,6 +16,7 @@ import { } from "~/components/ui/table" import { Tooltip, TooltipContent, TooltipPortal, TooltipTrigger } from "~/components/ui/tooltip" import { EllipsisHorizontalTextWithTooltip } from "~/components/ui/typography" +import { replaceImgUrlIfNeed } from "~/lib/img-proxy" import { cn } from "~/lib/utils" import { usePresentUserProfileModal } from "~/modules/profile/hooks" import { SettingSectionTitle } from "~/modules/settings/section" @@ -182,7 +183,7 @@ const UserRenderer = ({ ) : ( - + {name?.slice(0, 2)} )} diff --git a/apps/renderer/src/pages/(external)/(with-layout)/profile/[id]/index.tsx b/apps/renderer/src/pages/(external)/(with-layout)/profile/[id]/index.tsx index d60e8a788..919ba0a0c 100644 --- a/apps/renderer/src/pages/(external)/(with-layout)/profile/[id]/index.tsx +++ b/apps/renderer/src/pages/(external)/(with-layout)/profile/[id]/index.tsx @@ -12,6 +12,7 @@ import { usePresentFeedFormModal } from "~/hooks/biz/useFeedFormModal" import { useAuthQuery, useI18n, useTitle } from "~/hooks/common" import { apiClient } from "~/lib/api-fetch" import { defineQuery } from "~/lib/defineQuery" +import { replaceImgUrlIfNeed } from "~/lib/img-proxy" import { cn, isBizId } from "~/lib/utils" import { useUserSubscriptionsQuery } from "~/modules/profile/hooks" @@ -50,7 +51,7 @@ export function Component() { {user.data?.name?.slice(0, 2)} diff --git a/packages/shared/src/image.ts b/packages/shared/src/image.ts index 77b1d27c2..df690ed33 100644 --- a/packages/shared/src/image.ts +++ b/packages/shared/src/image.ts @@ -20,3 +20,11 @@ export const imageRefererMatches = [ referer: "https://sp1.piokok.com", }, ] + +export const webpCloudPublicServicesMatches = [ + // https://docs.webp.se/public-services/github-avatar/ + { + url: /^https:\/\/avatars\.githubusercontent\.com\/u\//, + target: "https://avatars-githubusercontent.webp.se/u/", + }, +]