refactor(media): centralize frontend media-type detection in lib/mediaType
Mirror the backend App\Enums\Media\Type on the frontend so "is this an image/video/PDF?" has a single definition instead of being hand-written (type === 'x' || mime_type startsWith ...) across a dozen files. - add lib/mediaType.ts: MediaType union, classify(), fromMimeType(), fromExtension(), isImage/isVideo/isDocument/isGif, acceptAttribute() - route every MediaItem detection and every File-upload validator through it - fix MediaDropzone rendering a typeless image item (AI/Unsplash/Giphy) as a broken <video> via the img-vs-video v-else - collapse getMediaIncompatibilityReason onto the shared getMediaValidationWarning core (canSchedule / variant selection unchanged) - tighten MediaItem/AssetMedia/SavedMedia/PickedMedia .type to MediaType - replace GalleryBrowser's hardcoded accept list with acceptAttribute()
This commit is contained in:
parent
49c77406a0
commit
a8bed34d17
12 changed files with 190 additions and 125 deletions
|
|
@ -2,6 +2,7 @@
|
|||
import { IconCloudUpload, IconPhoto, IconVideo, IconX, IconLoader2, IconPlus } from '@tabler/icons-vue';
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { fromMimeType, isImage, isVideo, MediaType } from '@/lib/mediaType';
|
||||
import type { MediaItem } from '@/types/media';
|
||||
|
||||
interface Props {
|
||||
|
|
@ -66,16 +67,15 @@ const canAddMore = computed(() => props.media.length < props.maxFiles);
|
|||
const isMultiple = computed(() => props.maxFiles > 1);
|
||||
|
||||
const isValidFileType = (file: File): boolean => {
|
||||
const isImage = file.type.startsWith('image/');
|
||||
const isVideo = file.type.startsWith('video/');
|
||||
const type = fromMimeType(file.type);
|
||||
|
||||
if (isImage && !props.acceptImages) {
|
||||
if (type === MediaType.Image && !props.acceptImages) {
|
||||
return false;
|
||||
}
|
||||
if (isVideo && !props.acceptVideos) {
|
||||
if (type === MediaType.Video && !props.acceptVideos) {
|
||||
return false;
|
||||
}
|
||||
return isImage || isVideo;
|
||||
return type === MediaType.Image || type === MediaType.Video;
|
||||
};
|
||||
|
||||
const handleDragOver = (e: DragEvent) => {
|
||||
|
|
@ -198,7 +198,7 @@ const handleRemove = (mediaId: string) => {
|
|||
class="relative group aspect-square rounded-lg overflow-hidden bg-muted"
|
||||
>
|
||||
<img
|
||||
v-if="item.type === 'image'"
|
||||
v-if="isImage(item)"
|
||||
:src="item.url"
|
||||
:alt="item.original_filename"
|
||||
class="w-full h-full object-cover"
|
||||
|
|
@ -212,7 +212,7 @@ const handleRemove = (mediaId: string) => {
|
|||
/>
|
||||
<!-- Video indicator -->
|
||||
<div
|
||||
v-if="item.type === 'video'"
|
||||
v-if="isVideo(item)"
|
||||
class="absolute bottom-1 left-1 bg-black/60 text-white text-[10px] px-1.5 py-0.5 rounded flex items-center gap-0.5"
|
||||
>
|
||||
<IconVideo class="h-3 w-3" />
|
||||
|
|
@ -247,7 +247,7 @@ const handleRemove = (mediaId: string) => {
|
|||
:class="[aspectRatio, variant === 'vertical' ? 'rounded-2xl' : 'rounded-lg']"
|
||||
>
|
||||
<img
|
||||
v-if="media[0].type === 'image'"
|
||||
v-if="isImage(media[0])"
|
||||
:src="media[0].url"
|
||||
:alt="media[0].original_filename"
|
||||
class="w-full h-full object-cover"
|
||||
|
|
@ -262,7 +262,7 @@ const handleRemove = (mediaId: string) => {
|
|||
/>
|
||||
<!-- Video indicator -->
|
||||
<div
|
||||
v-if="media[0].type === 'video'"
|
||||
v-if="isVideo(media[0])"
|
||||
class="absolute bottom-2 left-2 bg-black/60 text-white text-xs px-2 py-1 rounded-full flex items-center gap-1"
|
||||
>
|
||||
<IconVideo class="h-3.5 w-3.5" />
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import { Skeleton } from '@/components/ui/skeleton';
|
|||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip';
|
||||
import debounce from '@/debounce';
|
||||
import { acceptAttribute, isDocument, isVideo, type MediaType } from '@/lib/mediaType';
|
||||
import { destroy as assetsDestroy, search as assetsSearch, storeChunked as assetsStoreChunked, storeFromUrl } from '@/routes/app/assets';
|
||||
import { search as giphySearch, trending as giphyTrending } from '@/routes/app/assets/giphy';
|
||||
import { search as unsplashSearch, trending as unsplashTrending } from '@/routes/app/assets/unsplash';
|
||||
|
|
@ -24,7 +25,7 @@ interface AssetMedia {
|
|||
id: string;
|
||||
path: string;
|
||||
url: string;
|
||||
type: string;
|
||||
type: MediaType;
|
||||
mime_type: string;
|
||||
original_filename: string;
|
||||
size: number;
|
||||
|
|
@ -59,7 +60,7 @@ interface SavedMedia {
|
|||
id: string;
|
||||
path: string;
|
||||
url: string;
|
||||
type: string;
|
||||
type: MediaType;
|
||||
mime_type: string;
|
||||
}
|
||||
|
||||
|
|
@ -67,7 +68,7 @@ interface PickedMedia {
|
|||
id: string;
|
||||
path: string;
|
||||
url: string;
|
||||
type: string;
|
||||
type: MediaType;
|
||||
mime_type: string;
|
||||
original_filename?: string;
|
||||
size?: number;
|
||||
|
|
@ -84,6 +85,9 @@ const selected = defineModel<PickedMedia[]>('selected', { default: () => [] });
|
|||
|
||||
const isPicker = computed(() => props.mode === 'picker');
|
||||
|
||||
// The upload file picker accepts everything the backend's Media\Type allow-list does.
|
||||
const acceptedUploadTypes = acceptAttribute();
|
||||
|
||||
const lightbox = ref<InstanceType<typeof ImagePreviewDialog> | null>(null);
|
||||
|
||||
const handleAssetClick = (asset: AssetMedia) => {
|
||||
|
|
@ -92,14 +96,14 @@ const handleAssetClick = (asset: AssetMedia) => {
|
|||
return;
|
||||
}
|
||||
// PDFs can't render in the image lightbox — open them in a new tab.
|
||||
if (asset.type === 'document' || asset.mime_type === 'application/pdf') {
|
||||
if (isDocument(asset)) {
|
||||
window.open(asset.url, '_blank', 'noopener');
|
||||
return;
|
||||
}
|
||||
const previewable = uploads.value.filter((a) => a.type !== 'document' && a.mime_type !== 'application/pdf');
|
||||
const previewable = uploads.value.filter((a) => !isDocument(a));
|
||||
const items = previewable.map((a) => ({
|
||||
url: a.url,
|
||||
type: a.type === 'video' ? ('video' as const) : ('image' as const),
|
||||
type: isVideo(a) ? ('video' as const) : ('image' as const),
|
||||
}));
|
||||
const idx = previewable.findIndex((a) => a.id === asset.id);
|
||||
lightbox.value?.openCollection(items, idx);
|
||||
|
|
@ -626,7 +630,7 @@ onUnmounted(() => {
|
|||
type="file"
|
||||
class="hidden"
|
||||
multiple
|
||||
accept="image/jpeg,image/png,image/gif,image/webp,video/mp4,application/pdf"
|
||||
:accept="acceptedUploadTypes"
|
||||
@change="handleFileSelect"
|
||||
/>
|
||||
<div v-if="uploading" class="absolute inset-0 flex items-center justify-center rounded-2xl bg-card/85">
|
||||
|
|
@ -664,21 +668,21 @@ onUnmounted(() => {
|
|||
:key="asset.id"
|
||||
class="group relative overflow-hidden rounded-xl border-2 border-foreground bg-muted shadow-2xs transition-all hover:-translate-y-0.5 hover:shadow-md"
|
||||
:class="[
|
||||
(isPicker || asset.type !== 'video') ? 'cursor-pointer' : '',
|
||||
(isPicker || !isVideo(asset)) ? 'cursor-pointer' : '',
|
||||
isPicker && isSelected(asset.id) ? 'ring-2 ring-primary ring-offset-2 ring-offset-background' : '',
|
||||
]"
|
||||
@click="handleAssetClick(asset)"
|
||||
>
|
||||
<div class="aspect-square">
|
||||
<video
|
||||
v-if="asset.type === 'video'"
|
||||
v-if="isVideo(asset)"
|
||||
:src="asset.url"
|
||||
class="size-full object-cover"
|
||||
muted
|
||||
preload="metadata"
|
||||
/>
|
||||
<div
|
||||
v-else-if="asset.type === 'document' || asset.mime_type === 'application/pdf'"
|
||||
v-else-if="isDocument(asset)"
|
||||
class="flex size-full flex-col items-center justify-center gap-1.5 bg-rose-50 p-3 text-center"
|
||||
>
|
||||
<IconFileTypePdf class="size-9 text-rose-600" />
|
||||
|
|
|
|||
|
|
@ -5,12 +5,13 @@ import { computed, ref } from 'vue';
|
|||
import GalleryBrowser from '@/components/assets/GalleryBrowser.vue';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog';
|
||||
import { type MediaType } from '@/lib/mediaType';
|
||||
|
||||
interface PickedMedia {
|
||||
id: string;
|
||||
path: string;
|
||||
url: string;
|
||||
type: string;
|
||||
type: MediaType;
|
||||
mime_type: string;
|
||||
original_filename?: string;
|
||||
size?: number;
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import { Popover, PopoverAnchor, PopoverContent } from '@/components/ui/popover'
|
|||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip';
|
||||
import { formatBytes } from '@/composables/useMedia';
|
||||
import { getPlatformLabel, getPlatformLogo } from '@/composables/usePlatformLogo';
|
||||
import { isDocument, isVideo } from '@/lib/mediaType';
|
||||
import type { MediaItem } from '@/types/media';
|
||||
|
||||
interface Signature {
|
||||
|
|
@ -92,12 +93,6 @@ const openPreview = (item: MediaItem) => {
|
|||
);
|
||||
};
|
||||
|
||||
const isVideo = (item: MediaItem): boolean =>
|
||||
item.type === 'video' || Boolean(item.mime_type?.startsWith('video/'));
|
||||
|
||||
const isDocument = (item: MediaItem): boolean =>
|
||||
item.type === 'document' || item.mime_type === 'application/pdf';
|
||||
|
||||
const formatDuration = (seconds: number): string => {
|
||||
const total = Math.round(seconds);
|
||||
const m = Math.floor(total / 60);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import LabelBadge from '@/components/labels/LabelBadge.vue';
|
|||
import { Badge } from '@/components/ui/badge';
|
||||
import { usePageErrors } from '@/composables/usePageErrors';
|
||||
import { getPlatformLogo } from '@/composables/usePlatformLogo';
|
||||
import { isVideo } from '@/lib/mediaType';
|
||||
import type { PinterestBoard } from '@/types';
|
||||
import type { Channel } from '@/types/channel';
|
||||
import type { MediaItem } from '@/types/media';
|
||||
|
|
@ -97,7 +98,7 @@ const getBoards = (pp: PostPlatform): PinterestBoard[] =>
|
|||
pp.social_account_id ? props.pinterestBoards?.[pp.social_account_id] ?? [] : [];
|
||||
|
||||
const videoDurationSec = computed(() => {
|
||||
const video = props.media?.find((m) => m.type === 'video' || m.mime_type?.startsWith('video/'));
|
||||
const video = props.media?.find((m) => isVideo(m));
|
||||
const duration = video?.meta?.duration;
|
||||
return typeof duration === 'number' ? Math.ceil(duration) : null;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import { ref } from 'vue';
|
||||
|
||||
import { fromMimeType, MediaType } from '@/lib/mediaType';
|
||||
|
||||
export interface DragAndDropOptions {
|
||||
acceptImages: boolean;
|
||||
acceptVideos: boolean;
|
||||
|
|
@ -11,16 +13,15 @@ export function useDragAndDrop(options: DragAndDropOptions) {
|
|||
const isDragging = ref(false);
|
||||
|
||||
const isValidFileType = (file: File): boolean => {
|
||||
const isImage = file.type.startsWith('image/');
|
||||
const isVideo = file.type.startsWith('video/');
|
||||
const type = fromMimeType(file.type);
|
||||
|
||||
if (isImage && !options.acceptImages) {
|
||||
if (type === MediaType.Image && !options.acceptImages) {
|
||||
return false;
|
||||
}
|
||||
if (isVideo && !options.acceptVideos) {
|
||||
if (type === MediaType.Video && !options.acceptVideos) {
|
||||
return false;
|
||||
}
|
||||
return isImage || isVideo;
|
||||
return type === MediaType.Image || type === MediaType.Video;
|
||||
};
|
||||
|
||||
const handleDragOver = (e: DragEvent) => {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,15 @@
|
|||
import { getMediaRulesForContentType } from '@/composables/useMediaRules';
|
||||
import { isDocument, isGif, isImage, isVideo } from '@/lib/mediaType';
|
||||
import type { MediaItem } from '@/types/media';
|
||||
|
||||
export type { MediaItem } from '@/types/media';
|
||||
|
||||
// Media-type detection lives in '@/lib/mediaType' — these aliases keep the
|
||||
// names the rest of the app already imports from here pointing at it.
|
||||
export const isVideoMedia = isVideo;
|
||||
export const isDocumentMedia = isDocument;
|
||||
export const isImageMedia = isImage;
|
||||
|
||||
export interface MediaValidationWarning {
|
||||
key: string; // short key, e.g. 'gif_not_allowed'
|
||||
params: Record<string, string>;
|
||||
|
|
@ -37,10 +44,10 @@ export const getMediaValidationWarning = (
|
|||
if (! contentType) return { key: 'no_variant', params: {} };
|
||||
|
||||
const rules = getMediaRulesForContentType(contentType);
|
||||
const videos = media.filter((m) => m.type === 'video' || m.mime_type?.startsWith('video/'));
|
||||
const documents = media.filter((m) => m.type === 'document' || m.mime_type === 'application/pdf');
|
||||
const images = media.filter((m) => m.type === 'image' || (m.mime_type?.startsWith('image/') ?? false));
|
||||
const gifs = media.filter((m) => m.mime_type === 'image/gif');
|
||||
const videos = media.filter(isVideo);
|
||||
const documents = media.filter(isDocument);
|
||||
const images = media.filter(isImage);
|
||||
const gifs = media.filter(isGif);
|
||||
const total = media.length;
|
||||
|
||||
if (rules.requiresMedia && total === 0) {
|
||||
|
|
@ -72,14 +79,12 @@ export const getMediaValidationWarning = (
|
|||
}
|
||||
|
||||
for (const m of media) {
|
||||
const isVideo = m.type === 'video' || m.mime_type?.startsWith('video/');
|
||||
const isDocument = m.type === 'document' || m.mime_type === 'application/pdf';
|
||||
const size = m.size ?? 0;
|
||||
const width = m.meta?.width ?? 0;
|
||||
const height = m.meta?.height ?? 0;
|
||||
const duration = m.meta?.duration ?? 0;
|
||||
|
||||
if (isDocument) {
|
||||
if (isDocument(m)) {
|
||||
if (rules.maxDocumentBytes && size > rules.maxDocumentBytes) {
|
||||
return {
|
||||
key: 'document_too_large',
|
||||
|
|
@ -89,7 +94,7 @@ export const getMediaValidationWarning = (
|
|||
continue;
|
||||
}
|
||||
|
||||
if (isVideo) {
|
||||
if (isVideo(m)) {
|
||||
if (rules.maxVideoBytes && size > rules.maxVideoBytes) {
|
||||
return {
|
||||
key: 'video_too_large',
|
||||
|
|
@ -139,27 +144,27 @@ export const getMediaItemIssue = (item: MediaItem, contentType: string): string
|
|||
if (! contentType) return null;
|
||||
|
||||
const rules = getMediaRulesForContentType(contentType);
|
||||
const isVideo = item.type === 'video' || Boolean(item.mime_type?.startsWith('video/'));
|
||||
const isDocument = item.type === 'document' || item.mime_type === 'application/pdf';
|
||||
const isGif = item.mime_type === 'image/gif';
|
||||
const itemIsVideo = isVideo(item);
|
||||
const itemIsDocument = isDocument(item);
|
||||
const itemIsGif = isGif(item);
|
||||
|
||||
if (isDocument) {
|
||||
if (itemIsDocument) {
|
||||
if (! rules.acceptDocuments) return 'no_document_allowed';
|
||||
const docSize = item.size ?? 0;
|
||||
if (rules.maxDocumentBytes && docSize > rules.maxDocumentBytes) return 'document_too_large';
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isVideo && ! rules.acceptVideos) return 'no_video_allowed';
|
||||
if (! isVideo && ! rules.acceptImages) return 'no_image_allowed';
|
||||
if (isGif && ! rules.acceptsGif) return 'gif_not_allowed';
|
||||
if (itemIsVideo && ! rules.acceptVideos) return 'no_video_allowed';
|
||||
if (! itemIsVideo && ! rules.acceptImages) return 'no_image_allowed';
|
||||
if (itemIsGif && ! rules.acceptsGif) return 'gif_not_allowed';
|
||||
|
||||
const size = item.size ?? 0;
|
||||
if (isVideo && rules.maxVideoBytes && size > rules.maxVideoBytes) return 'video_too_large';
|
||||
if (! isVideo && rules.maxImageBytes && size > rules.maxImageBytes) return 'image_too_large';
|
||||
if (itemIsVideo && rules.maxVideoBytes && size > rules.maxVideoBytes) return 'video_too_large';
|
||||
if (! itemIsVideo && rules.maxImageBytes && size > rules.maxImageBytes) return 'image_too_large';
|
||||
|
||||
const duration = item.meta?.duration ?? 0;
|
||||
if (isVideo && rules.maxVideoDurationSec && duration > rules.maxVideoDurationSec) {
|
||||
if (itemIsVideo && rules.maxVideoDurationSec && duration > rules.maxVideoDurationSec) {
|
||||
return 'video_too_long';
|
||||
}
|
||||
|
||||
|
|
@ -173,20 +178,3 @@ export const getMediaItemIssue = (item: MediaItem, contentType: string): string
|
|||
|
||||
return null;
|
||||
};
|
||||
|
||||
export const isVideoMedia = (item: MediaItem | null | undefined): boolean => {
|
||||
if (! item) return false;
|
||||
return item.type === 'video' || Boolean(item.mime_type?.startsWith('video/'));
|
||||
};
|
||||
|
||||
export const isDocumentMedia = (item: MediaItem | null | undefined): boolean => {
|
||||
if (! item) return false;
|
||||
return item.type === 'document' || item.mime_type === 'application/pdf';
|
||||
};
|
||||
|
||||
export const isImageMedia = (item: MediaItem | null | undefined): boolean => {
|
||||
if (! item) return false;
|
||||
if (isVideoMedia(item)) return false;
|
||||
if (isDocumentMedia(item)) return false;
|
||||
return true;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import { computed, type Ref, type ComputedRef } from 'vue';
|
||||
|
||||
import { fromMimeType, MediaType } from '@/lib/mediaType';
|
||||
|
||||
export interface MediaRules {
|
||||
maxFiles: number;
|
||||
minFiles?: number;
|
||||
|
|
@ -173,20 +175,18 @@ export function useMediaRules(contentType: Ref<string> | ComputedRef<string>) {
|
|||
|
||||
const isValidFileType = computed(() => {
|
||||
return (file: File): boolean => {
|
||||
const isImage = file.type.startsWith('image/');
|
||||
const isVideo = file.type.startsWith('video/');
|
||||
const isDocument = file.type === 'application/pdf';
|
||||
const type = fromMimeType(file.type);
|
||||
|
||||
if (isImage && !rules.value.acceptImages) {
|
||||
if (type === MediaType.Image && !rules.value.acceptImages) {
|
||||
return false;
|
||||
}
|
||||
if (isVideo && !rules.value.acceptVideos) {
|
||||
if (type === MediaType.Video && !rules.value.acceptVideos) {
|
||||
return false;
|
||||
}
|
||||
if (isDocument) {
|
||||
if (type === MediaType.Document) {
|
||||
return Boolean(rules.value.acceptDocuments);
|
||||
}
|
||||
return isImage || isVideo;
|
||||
return type === MediaType.Image || type === MediaType.Video;
|
||||
};
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { trans } from 'laravel-vue-i18n';
|
||||
import { computed, type ComputedRef, type Ref } from 'vue';
|
||||
|
||||
import { getMediaItemIssue } from '@/composables/useMedia';
|
||||
import { getMediaItemIssue, getMediaValidationWarning } from '@/composables/useMedia';
|
||||
import { getMediaRulesForContentType } from '@/composables/useMediaRules';
|
||||
import { getPlatformLabel } from '@/composables/usePlatformLogo';
|
||||
import { ContentType } from '@/types/content-type';
|
||||
|
|
@ -86,57 +86,47 @@ export const getPlatformMetaIssue = (platform: string, meta: Record<string, any>
|
|||
return result.tooltipKey ? trans(result.tooltipKey) : trans('posts.edit.compliance_incomplete');
|
||||
};
|
||||
|
||||
// The editor's compliance copy keyed by the shared media-warning core's key.
|
||||
// Detection + rule evaluation live once in getMediaValidationWarning; only the
|
||||
// wording (and a couple of params) differ between the settings dialogs and this
|
||||
// publish gate.
|
||||
const COMPLIANCE_KEY_BY_WARNING: Record<string, string> = {
|
||||
no_variant: 'no_content_type',
|
||||
requires_media: 'requires_media',
|
||||
max_files_exceeded: 'too_many_files',
|
||||
min_files_required: 'too_few_files',
|
||||
no_video_allowed: 'no_videos',
|
||||
no_image_allowed: 'no_images',
|
||||
no_document_allowed: 'no_documents',
|
||||
no_mixed_media: 'no_mixed_media',
|
||||
document_not_alone: 'document_not_alone',
|
||||
gif_not_allowed: 'no_gifs',
|
||||
image_too_large: 'image_too_large',
|
||||
video_too_large: 'video_too_large',
|
||||
document_too_large: 'document_too_large',
|
||||
video_too_long: 'video_too_long',
|
||||
aspect_ratio_too_narrow: 'aspect_ratio_invalid',
|
||||
aspect_ratio_too_wide: 'aspect_ratio_invalid',
|
||||
};
|
||||
|
||||
export const getMediaIncompatibilityReason = (
|
||||
contentType: string,
|
||||
mediaItems: MediaItem[],
|
||||
): string | null => {
|
||||
const rules = getMediaRulesForContentType(contentType);
|
||||
const videos = mediaItems.filter((m) => m.type === 'video' || m.mime_type?.startsWith('video/'));
|
||||
const documents = mediaItems.filter((m) => m.type === 'document' || m.mime_type === 'application/pdf');
|
||||
const images = mediaItems.filter((m) => m.type === 'image' || m.mime_type?.startsWith('image/'));
|
||||
const gifs = mediaItems.filter((m) => m.mime_type === 'image/gif');
|
||||
const total = mediaItems.length;
|
||||
const warning = getMediaValidationWarning(contentType, mediaItems);
|
||||
if (!warning) return null;
|
||||
|
||||
if (rules.requiresMedia && total === 0) return trans('posts.edit.compliance.requires_media');
|
||||
if (!rules.acceptVideos && videos.length > 0) return trans('posts.edit.compliance.no_videos');
|
||||
if (!rules.acceptImages && images.length > 0) return trans('posts.edit.compliance.no_images');
|
||||
if (!rules.acceptDocuments && documents.length > 0) return trans('posts.edit.compliance.no_documents');
|
||||
if (rules.acceptDocuments && documents.length > 0 && total > 1) return trans('posts.edit.compliance.document_not_alone');
|
||||
if (rules.forbidsMixedMedia && videos.length > 0 && images.length > 0) return trans('posts.edit.compliance.no_mixed_media');
|
||||
if (!rules.acceptsGif && gifs.length > 0) return trans('posts.edit.compliance.no_gifs');
|
||||
if (total > rules.maxFiles) return trans('posts.edit.compliance.too_many_files', { max: String(rules.maxFiles) });
|
||||
if (rules.minFiles && total < rules.minFiles) return trans('posts.edit.compliance.too_few_files', { min: String(rules.minFiles) });
|
||||
const complianceKey = COMPLIANCE_KEY_BY_WARNING[warning.key];
|
||||
if (!complianceKey) return trans('posts.edit.compliance_incomplete');
|
||||
|
||||
for (const m of mediaItems) {
|
||||
const isVideo = m.type === 'video' || m.mime_type?.startsWith('video/');
|
||||
const isDocument = m.type === 'document' || m.mime_type === 'application/pdf';
|
||||
const size = m.size ?? 0;
|
||||
const duration = m.meta?.duration ?? 0;
|
||||
const width = m.meta?.width ?? 0;
|
||||
const height = m.meta?.height ?? 0;
|
||||
|
||||
if (isDocument) {
|
||||
if (rules.maxDocumentBytes && size > 0 && size > rules.maxDocumentBytes) return trans('posts.edit.compliance.document_too_large');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isVideo) {
|
||||
if (rules.maxVideoBytes && size > 0 && size > rules.maxVideoBytes) return trans('posts.edit.compliance.video_too_large');
|
||||
if (rules.maxVideoDurationSec && duration > 0 && duration > rules.maxVideoDurationSec) {
|
||||
return trans('posts.edit.compliance.video_too_long', { seconds: String(rules.maxVideoDurationSec) });
|
||||
}
|
||||
} else if (rules.maxImageBytes && size > 0 && size > rules.maxImageBytes) {
|
||||
return trans('posts.edit.compliance.image_too_large');
|
||||
}
|
||||
|
||||
if (width > 0 && height > 0 && (rules.aspectRatioMin || rules.aspectRatioMax)) {
|
||||
const ratio = width / height;
|
||||
if (rules.aspectRatioMin && ratio < rules.aspectRatioMin) return trans('posts.edit.compliance.aspect_ratio_invalid');
|
||||
if (rules.aspectRatioMax && ratio > rules.aspectRatioMax) return trans('posts.edit.compliance.aspect_ratio_invalid');
|
||||
}
|
||||
const params: Record<string, string> = {};
|
||||
if (warning.key === 'max_files_exceeded') params.max = warning.params.max;
|
||||
if (warning.key === 'min_files_required') params.min = warning.params.min;
|
||||
if (warning.key === 'video_too_long') {
|
||||
params.seconds = String(getMediaRulesForContentType(contentType).maxVideoDurationSec ?? '');
|
||||
}
|
||||
|
||||
return null;
|
||||
return trans(`posts.edit.compliance.${complianceKey}`, params);
|
||||
};
|
||||
|
||||
export const firstCompatibleVariant = (
|
||||
|
|
|
|||
88
resources/js/lib/mediaType.ts
Normal file
88
resources/js/lib/mediaType.ts
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
/**
|
||||
* Single source of truth for media-type detection on the frontend — the mirror
|
||||
* of the backend `App\Enums\Media\Type`. Every "is this an image / video / PDF?"
|
||||
* check goes through here so the answer can't drift between components.
|
||||
*/
|
||||
|
||||
export const MediaType = {
|
||||
Image: 'image',
|
||||
Video: 'video',
|
||||
Document: 'document',
|
||||
} as const;
|
||||
|
||||
export type MediaType = (typeof MediaType)[keyof typeof MediaType];
|
||||
|
||||
/** MIME allow-list we accept on upload — mirrors Type::allowedMimeTypes(). */
|
||||
export const ALLOWED_MIME_TYPES: Record<MediaType, readonly string[]> = {
|
||||
[MediaType.Image]: ['image/jpeg', 'image/png', 'image/gif', 'image/webp'],
|
||||
[MediaType.Video]: ['video/mp4', 'video/quicktime'],
|
||||
[MediaType.Document]: ['application/pdf'],
|
||||
};
|
||||
|
||||
// Broader than the upload allow-list so already-stored files in legacy formats
|
||||
// still resolve — mirrors Type::fromExtension() on the backend.
|
||||
const IMAGE_EXTENSIONS = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg', 'heic', 'heif'];
|
||||
const VIDEO_EXTENSIONS = ['mp4', 'mov', 'avi', 'wmv', 'webm', 'mkv', 'm4v'];
|
||||
|
||||
const GIF_MIME = 'image/gif';
|
||||
const PDF_MIME = 'application/pdf';
|
||||
|
||||
/** The `accept` attribute value for a file input that takes any media we allow. */
|
||||
export const acceptAttribute = (): string => Object.values(ALLOWED_MIME_TYPES).flat().join(',');
|
||||
|
||||
/** The structural shape every classifiable media item satisfies. */
|
||||
interface ClassifiableMedia {
|
||||
type?: string | null;
|
||||
mime_type?: string | null;
|
||||
original_filename?: string | null;
|
||||
path?: string | null;
|
||||
}
|
||||
|
||||
/** Resolve a MediaType from a raw MIME string (e.g. a browser `File.type`). */
|
||||
export const fromMimeType = (mime: string | null | undefined): MediaType | null => {
|
||||
const value = mime ?? '';
|
||||
|
||||
if (value.startsWith('image/')) return MediaType.Image;
|
||||
if (value.startsWith('video/')) return MediaType.Video;
|
||||
if (value === PDF_MIME) return MediaType.Document;
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/** Resolve a MediaType from a filename or path extension. */
|
||||
export const fromExtension = (nameOrPath: string | null | undefined): MediaType | null => {
|
||||
if (! nameOrPath) return null;
|
||||
|
||||
const ext = nameOrPath.split('.').pop()?.toLowerCase() ?? '';
|
||||
|
||||
if (IMAGE_EXTENSIONS.includes(ext)) return MediaType.Image;
|
||||
if (VIDEO_EXTENSIONS.includes(ext)) return MediaType.Video;
|
||||
if (ext === 'pdf') return MediaType.Document;
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Classify a media item. Trusts the server-assigned `type` first, then the MIME,
|
||||
* then falls back to the filename extension so already-stored items still
|
||||
* resolve. Returns null only when nothing identifies the item.
|
||||
*/
|
||||
export const classify = (item: ClassifiableMedia | null | undefined): MediaType | null => {
|
||||
if (! item) return null;
|
||||
|
||||
const explicit = item.type;
|
||||
if (explicit === MediaType.Image || explicit === MediaType.Video || explicit === MediaType.Document) {
|
||||
return explicit;
|
||||
}
|
||||
|
||||
return fromMimeType(item.mime_type) ?? fromExtension(item.original_filename ?? item.path);
|
||||
};
|
||||
|
||||
export const isImage = (item: ClassifiableMedia | null | undefined): boolean => classify(item) === MediaType.Image;
|
||||
|
||||
export const isVideo = (item: ClassifiableMedia | null | undefined): boolean => classify(item) === MediaType.Video;
|
||||
|
||||
export const isDocument = (item: ClassifiableMedia | null | undefined): boolean => classify(item) === MediaType.Document;
|
||||
|
||||
/** Whether the item is an animated GIF — several platforms treat it specially. */
|
||||
export const isGif = (item: ClassifiableMedia | null | undefined): boolean => (item?.mime_type ?? '') === GIF_MIME;
|
||||
|
|
@ -17,6 +17,7 @@ import { getPlatformLabel, getPlatformLogo } from '@/composables/usePlatformLogo
|
|||
import { getPlatformStatusConfig, getPostStatusConfig } from '@/composables/usePostStatus';
|
||||
import dayjs from '@/dayjs';
|
||||
import AppLayout from '@/layouts/AppLayout.vue';
|
||||
import { isDocument as isDocumentItem, isVideo as isVideoItem } from '@/lib/mediaType';
|
||||
import { index as postsIndex } from '@/routes/app/posts';
|
||||
import type { MediaItem } from '@/types/media';
|
||||
import { PostPlatformStatus, PostStatus } from '@/types/post';
|
||||
|
|
@ -87,12 +88,6 @@ const formatDateTime = (date: string | null): string =>
|
|||
|
||||
const lightbox = ref<InstanceType<typeof ImagePreviewDialog> | null>(null);
|
||||
|
||||
const isVideoItem = (item: MediaItem): boolean =>
|
||||
item.type === 'video' || (item.mime_type?.startsWith('video/') ?? false);
|
||||
|
||||
const isDocumentItem = (item: MediaItem): boolean =>
|
||||
item.type === 'document' || item.mime_type === 'application/pdf';
|
||||
|
||||
const openLightbox = (i: number) => {
|
||||
const item = props.post.media[i];
|
||||
// PDFs can't render in the image lightbox — open them in a new tab.
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
import type { MediaType } from '@/lib/mediaType';
|
||||
|
||||
export type MediaSource = 'ai' | 'unsplash' | 'giphy';
|
||||
|
||||
export interface MediaItem {
|
||||
id: string;
|
||||
url: string;
|
||||
path?: string;
|
||||
type?: string;
|
||||
type?: MediaType;
|
||||
mime_type?: string;
|
||||
original_filename?: string;
|
||||
size?: number;
|
||||
|
|
|
|||
Loading…
Reference in a new issue