From 35c5a0ebcf6b4d78847c2ab312f7f37f348ff9f4 Mon Sep 17 00:00:00 2001 From: Untone Date: Sat, 13 Jul 2024 15:25:25 +0300 Subject: [PATCH] getThumbUrl --- src/components/Article/FullArticle.tsx | 2 +- src/components/Inbox/DialogAvatar.tsx | 2 +- .../Topic/TopicBadge/TopicBadge.tsx | 2 +- src/components/Views/EditView/EditView.tsx | 2 +- .../Views/Profile/ProfileSettings.tsx | 2 +- src/components/_shared/Image/Image.tsx | 2 +- src/components/_shared/Lightbox/Lightbox.tsx | 2 +- src/components/_shared/PageLayout.tsx | 2 +- .../_shared/SolidSwiper/EditorSwiper.tsx | 2 +- .../_shared/SolidSwiper/ImageSwiper.tsx | 2 +- src/lib/{getImageUrl.ts => getThumbUrl.ts} | 18 ++++---- src/lib/groupby.ts | 42 ------------------- src/lib/validateUploads.ts | 6 +++ src/routes/author/[slug]/[...tab].tsx | 2 +- src/routes/topic/[slug]/[...tab].tsx | 2 +- 15 files changed, 28 insertions(+), 62 deletions(-) rename src/lib/{getImageUrl.ts => getThumbUrl.ts} (80%) delete mode 100644 src/lib/groupby.ts diff --git a/src/components/Article/FullArticle.tsx b/src/components/Article/FullArticle.tsx index bced205f..427cc0b0 100644 --- a/src/components/Article/FullArticle.tsx +++ b/src/components/Article/FullArticle.tsx @@ -12,7 +12,7 @@ import { DEFAULT_HEADER_OFFSET, useUI } from '~/context/ui' import type { Author, Maybe, Shout, Topic } from '~/graphql/schema/core.gen' import { processPrepositions } from '~/intl/prepositions' import { isCyrillic } from '~/intl/translate' -import { getImageUrl } from '~/lib/getImageUrl' +import { getImageUrl } from '~/lib/getThumbUrl' import { MediaItem } from '~/types/mediaitem' import { capitalize } from '~/utils/capitalize' import { AuthorBadge } from '../Author/AuthorBadge' diff --git a/src/components/Inbox/DialogAvatar.tsx b/src/components/Inbox/DialogAvatar.tsx index c3e255a2..e49ef397 100644 --- a/src/components/Inbox/DialogAvatar.tsx +++ b/src/components/Inbox/DialogAvatar.tsx @@ -1,7 +1,7 @@ import { clsx } from 'clsx' import { Show, createMemo } from 'solid-js' -import { getImageUrl } from '~/lib/getImageUrl' +import { getImageUrl } from '~/lib/getThumbUrl' import './DialogCard.module.scss' import styles from './DialogAvatar.module.scss' diff --git a/src/components/Topic/TopicBadge/TopicBadge.tsx b/src/components/Topic/TopicBadge/TopicBadge.tsx index 5a70e406..201cb0ae 100644 --- a/src/components/Topic/TopicBadge/TopicBadge.tsx +++ b/src/components/Topic/TopicBadge/TopicBadge.tsx @@ -6,7 +6,7 @@ import { useFollowing } from '~/context/following' import { useLocalize } from '~/context/localize' import { useSession } from '~/context/session' import { FollowingEntity, Topic } from '~/graphql/schema/core.gen' -import { getImageUrl } from '~/lib/getImageUrl' +import { getImageUrl } from '~/lib/getThumbUrl' import { mediaMatches } from '~/lib/mediaQuery' import { capitalize } from '~/utils/capitalize' import styles from './TopicBadge.module.scss' diff --git a/src/components/Views/EditView/EditView.tsx b/src/components/Views/EditView/EditView.tsx index 7858d51c..5a5e01eb 100644 --- a/src/components/Views/EditView/EditView.tsx +++ b/src/components/Views/EditView/EditView.tsx @@ -25,7 +25,7 @@ import { useLocalize } from '~/context/localize' import getMyShoutQuery from '~/graphql/query/core/article-my' import type { Shout, Topic } from '~/graphql/schema/core.gen' import { slugify } from '~/intl/translit' -import { getImageUrl } from '~/lib/getImageUrl' +import { getImageUrl } from '~/lib/getThumbUrl' import { isDesktop } from '~/lib/mediaQuery' import { LayoutType } from '~/types/common' import { MediaItem } from '~/types/mediaitem' diff --git a/src/components/Views/Profile/ProfileSettings.tsx b/src/components/Views/Profile/ProfileSettings.tsx index d986e420..f2b9d693 100644 --- a/src/components/Views/Profile/ProfileSettings.tsx +++ b/src/components/Views/Profile/ProfileSettings.tsx @@ -19,7 +19,7 @@ import { useProfile } from '~/context/profile' import { useSession } from '~/context/session' import { useSnackbar, useUI } from '~/context/ui' import { InputMaybe, ProfileInput } from '~/graphql/schema/core.gen' -import { getImageUrl } from '~/lib/getImageUrl' +import { getImageUrl } from '~/lib/getThumbUrl' import { handleImageUpload } from '~/lib/handleImageUpload' import { profileSocialLinks } from '~/lib/profileSocialLinks' import { clone } from '~/utils/clone' diff --git a/src/components/_shared/Image/Image.tsx b/src/components/_shared/Image/Image.tsx index 54275906..01966289 100644 --- a/src/components/_shared/Image/Image.tsx +++ b/src/components/_shared/Image/Image.tsx @@ -3,7 +3,7 @@ import type { JSX } from 'solid-js' import { Link } from '@solidjs/meta' import { splitProps } from 'solid-js' -import { getImageUrl } from '~/lib/getImageUrl' +import { getImageUrl } from '~/lib/getThumbUrl' type Props = JSX.ImgHTMLAttributes & { width: number diff --git a/src/components/_shared/Lightbox/Lightbox.tsx b/src/components/_shared/Lightbox/Lightbox.tsx index b543e257..b4c6b02c 100644 --- a/src/components/_shared/Lightbox/Lightbox.tsx +++ b/src/components/_shared/Lightbox/Lightbox.tsx @@ -1,7 +1,7 @@ import { clsx } from 'clsx' import { Show, createEffect, createMemo, createSignal, on, onCleanup } from 'solid-js' -import { getImageUrl } from '~/lib/getImageUrl' +import { getImageUrl } from '~/lib/getThumbUrl' import { useEscKeyDownHandler } from '~/lib/useEscKeyDownHandler' import { Icon } from '../Icon' diff --git a/src/components/_shared/PageLayout.tsx b/src/components/_shared/PageLayout.tsx index 63ed4836..ee1e01df 100644 --- a/src/components/_shared/PageLayout.tsx +++ b/src/components/_shared/PageLayout.tsx @@ -7,7 +7,7 @@ import { useLocalize } from '~/context/localize' import { Shout } from '~/graphql/schema/core.gen' import enKeywords from '~/intl/locales/en/keywords.json' import ruKeywords from '~/intl/locales/ru/keywords.json' -import { getImageUrl, getOpenGraphImageUrl } from '~/lib/getImageUrl' +import { getImageUrl, getOpenGraphImageUrl } from '~/lib/getThumbUrl' import { descFromBody } from '~/utils/meta' import { FooterView } from '../Discours/Footer' import { Header } from '../Nav/Header' diff --git a/src/components/_shared/SolidSwiper/EditorSwiper.tsx b/src/components/_shared/SolidSwiper/EditorSwiper.tsx index 26d2062d..c65af966 100644 --- a/src/components/_shared/SolidSwiper/EditorSwiper.tsx +++ b/src/components/_shared/SolidSwiper/EditorSwiper.tsx @@ -7,7 +7,7 @@ import { Manipulation, Navigation, Pagination } from 'swiper/modules' import { useLocalize } from '~/context/localize' import { useSnackbar } from '~/context/ui' import { composeMediaItems } from '~/lib/composeMediaItems' -import { getImageUrl } from '~/lib/getImageUrl' +import { getImageUrl } from '~/lib/getThumbUrl' import { handleImageUpload } from '~/lib/handleImageUpload' import { validateUploads } from '~/lib/validateUploads' import { DropArea } from '../DropArea' diff --git a/src/components/_shared/SolidSwiper/ImageSwiper.tsx b/src/components/_shared/SolidSwiper/ImageSwiper.tsx index 6e6318b3..fde4b448 100644 --- a/src/components/_shared/SolidSwiper/ImageSwiper.tsx +++ b/src/components/_shared/SolidSwiper/ImageSwiper.tsx @@ -4,7 +4,7 @@ import SwiperCore from 'swiper' import { HashNavigation, Manipulation, Navigation, Pagination } from 'swiper/modules' import { throttle } from 'throttle-debounce' -import { getImageUrl } from '~/lib/getImageUrl' +import { getImageUrl } from '~/lib/getThumbUrl' import { MediaItem } from '~/types/mediaitem' import { Icon } from '../Icon' import { Image } from '../Image' diff --git a/src/lib/getImageUrl.ts b/src/lib/getThumbUrl.ts similarity index 80% rename from src/lib/getImageUrl.ts rename to src/lib/getThumbUrl.ts index 2a350bd3..d9097722 100644 --- a/src/lib/getImageUrl.ts +++ b/src/lib/getThumbUrl.ts @@ -8,12 +8,6 @@ const URL_CONFIG = { productionFolder: 'production/' } -const AUDIO_EXTENSIONS = new Set(['wav', 'mp3', 'ogg', 'aif', 'flac']) - -const isAudioFile = (filename: string): boolean => { - const extension = filename.split('.').pop()?.toLowerCase() - return AUDIO_EXTENSIONS.has(extension ?? '') -} const getLastSegment = (url: string): string => url.toLowerCase().split('/').pop() || '' const buildSizePart = (width?: number, height?: number, includeSize = true): string => { @@ -31,13 +25,21 @@ export const getImageUrl = ( return src } const filename = getLastSegment(src) - const base = isAudioFile(filename) ? URL_CONFIG.cdnUrl : URL_CONFIG.thumborUrl const suffix = options.noSizeUrlPart ? '' : buildSizePart(options.width, options.height) - const subfolder = isAudioFile(filename) ? URL_CONFIG.audioSubfolder : URL_CONFIG.imageSubfolder + const base = URL_CONFIG.thumborUrl + const subfolder = URL_CONFIG.imageSubfolder return `${base}${suffix}${URL_CONFIG.productionFolder}${subfolder}/${filename}` } +export const getAudioUrl = (src: string) => { + const filename = getLastSegment(src) + const base = URL_CONFIG.cdnUrl + const subfolder = URL_CONFIG.audioSubfolder + const prodfolder = URL_CONFIG.productionFolder + return `${base}${prodfolder}${subfolder}/${filename}` +} + export const getOpenGraphImageUrl = ( src: string, options: { diff --git a/src/lib/groupby.ts b/src/lib/groupby.ts deleted file mode 100644 index b4dbbae1..00000000 --- a/src/lib/groupby.ts +++ /dev/null @@ -1,42 +0,0 @@ -import type { Author, Shout, Topic } from '~/graphql/schema/core.gen' - -export const groupByName = (arr: Author[]) => { - return arr.reduce( - (acc: { [key: string]: Author[] }, a: Author) => { - let c = - a?.name - ?.replaceAll(/[^\d A-Za-zА-я]/g, '') - .split(' ') - .pop() - ?.slice(0, 1) - .toUpperCase() || '' - if (c) { - if (/[^А-я]/.test(c)) c = 'A-Z' - else if (!acc[c]) acc[c] = [] - acc[c].push(a) - } - return acc - }, - { - 'A-Z': [] - } - ) -} - -export const groupByTitle = (arr: (Shout | Topic)[]) => { - return arr.reduce( - (acc: { [key: string]: (Topic | Shout)[] }, tt: Topic | Shout) => { - let c = (tt.title || '') - .replaceAll(/[^\d A-Za-zА-я]/g, '') - .slice(0, 1) - .toUpperCase() - if (/[^А-я]/.test(c)) c = 'A-Z' - else if (!acc[c]) acc[c] = [] - acc[c].push(tt) - return acc - }, - { - 'A-Z': [] - } - ) -} diff --git a/src/lib/validateUploads.ts b/src/lib/validateUploads.ts index 709d2bd3..1da62af5 100644 --- a/src/lib/validateUploads.ts +++ b/src/lib/validateUploads.ts @@ -2,6 +2,12 @@ import { UploadFile } from '@solid-primitives/upload' export const imageExtensions = new Set(['jpg', 'jpeg', 'png', 'gif', 'bmp']) export const docExtensions = new Set(['doc', 'docx', 'pdf', 'txt']) +export const audioExtensions = new Set(['wav', 'mp3', 'ogg', 'aif', 'flac']) + +export const isAudioFilename = (filename: string): boolean => { + const extension = filename.split('.').pop()?.toLowerCase() + return audioExtensions.has(extension || '') +} export const validateUploads = (fileType: string, files: UploadFile[]): boolean => { for (const file of files) { diff --git a/src/routes/author/[slug]/[...tab].tsx b/src/routes/author/[slug]/[...tab].tsx index e14a275a..2c037156 100644 --- a/src/routes/author/[slug]/[...tab].tsx +++ b/src/routes/author/[slug]/[...tab].tsx @@ -15,7 +15,7 @@ import { Shout, Topic } from '~/graphql/schema/core.gen' -import { getImageUrl } from '~/lib/getImageUrl' +import { getImageUrl } from '~/lib/getThumbUrl' import { SHOUTS_PER_PAGE } from '../../(main)' const fetchAuthorShouts = async (slug: string, offset?: number) => { diff --git a/src/routes/topic/[slug]/[...tab].tsx b/src/routes/topic/[slug]/[...tab].tsx index 15a2c2a7..c85d0f15 100644 --- a/src/routes/topic/[slug]/[...tab].tsx +++ b/src/routes/topic/[slug]/[...tab].tsx @@ -9,7 +9,7 @@ import { useLocalize } from '~/context/localize' import { useTopics } from '~/context/topics' import { loadShouts, loadTopics } from '~/graphql/api/public' import { Author, LoadShoutsOptions, Shout, Topic } from '~/graphql/schema/core.gen' -import { getImageUrl } from '~/lib/getImageUrl' +import { getImageUrl } from '~/lib/getThumbUrl' import { descFromBody } from '~/utils/meta' import { SHOUTS_PER_PAGE } from '../../(main)'