/** * WordPress dependencies */ import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import type { Maybe, MediaItem, SocialNetwork, SocialNetworkName, } from '@nelio-content/types'; /** * Internal dependencies */ import definitions from '../../includes/networks/requirements.json'; type MediaRules = { readonly mimeTypes: ReadonlyArray< string >; readonly minSizeKiB?: number; readonly maxSizeMiB?: number; readonly minDuration?: number; readonly maxDuration?: number; readonly maxSizeMiBByMime?: Readonly< Record< string, number > >; readonly minWidth?: number; readonly minHeight?: number; readonly maxHeight?: number; readonly maxDimensionSum?: number; }; type NetworkDefinition = Pick< SocialNetwork, | 'limit' | 'supports' | 'requires' | 'supportedImageTypes' | 'supportedVideoTypes' > & { readonly abilitySupported: boolean; readonly media?: Partial< Record< 'image' | 'video', MediaRules > >; }; // The file is also read by PHP. Keep constraints here rather than in either consumer. const networks = definitions as Record< SocialNetworkName, NetworkDefinition >; export function getNetworkDefinition( network: SocialNetworkName ): Pick< SocialNetwork, | 'limit' | 'supports' | 'requires' | 'supportedImageTypes' | 'supportedVideoTypes' > { const { limit, supports, requires, supportedImageTypes, supportedVideoTypes, media, } = networks[ network ]; return { limit, supports, requires, supportedImageTypes: media?.image?.mimeTypes ?? supportedImageTypes, supportedVideoTypes: media?.video?.mimeTypes ?? supportedVideoTypes, }; } export function getSharedMediaError( network: SocialNetworkName, type: 'image' | 'video', media: Maybe< MediaItem > ): Maybe< string > { if ( ! media ) { return undefined; } const rules = networks[ network ].media?.[ type ]; if ( ! rules ) { return _x( 'Unsupported media format for this social network.', 'text', 'nelio-content' ); } if ( ! rules.mimeTypes.some( ( mime ) => mime === media.mime || mime === media.mime.split( '/' )[ 0 ] ) ) { return _x( 'Unsupported media type for this social network.', 'text', 'nelio-content' ); } const sizeKiB = Math.round( media.filesizeInBytes / 1024 ); if ( rules.minSizeKiB !== undefined && sizeKiB < rules.minSizeKiB ) { return _x( 'The media file is smaller than this social network allows.', 'text', 'nelio-content' ); } const sizeMiB = Math.round( sizeKiB / 1024 ); const maxSize = rules.maxSizeMiBByMime?.[ media.mime ] ?? rules.maxSizeMiB; if ( maxSize !== undefined && sizeMiB > maxSize ) { return _x( 'The media file is larger than this social network allows.', 'text', 'nelio-content' ); } if ( type === 'video' && ( rules.minDuration !== undefined || rules.maxDuration !== undefined ) && media.duration === undefined ) { return _x( 'Video duration metadata is unavailable.', 'text', 'nelio-content' ); } if ( type === 'video' && rules.minDuration !== undefined && media.duration < rules.minDuration ) { return _x( 'The video is shorter than this social network allows.', 'text', 'nelio-content' ); } if ( type === 'video' && rules.maxDuration !== undefined && media.duration > rules.maxDuration ) { return _x( 'The video is longer than this social network allows.', 'text', 'nelio-content' ); } if ( ( rules.minWidth !== undefined && media.width === undefined ) || ( ( rules.minHeight !== undefined || rules.maxHeight !== undefined ) && media.height === undefined ) || ( rules.maxDimensionSum !== undefined && ( media.width === undefined || media.height === undefined ) ) ) { return _x( 'Image dimensions are unavailable.', 'text', 'nelio-content' ); } if ( ( rules.minWidth !== undefined && media.width < rules.minWidth ) || ( rules.minHeight !== undefined && media.height < rules.minHeight ) || ( rules.maxHeight !== undefined && media.height > rules.maxHeight ) || ( rules.maxDimensionSum !== undefined && media.width + media.height > rules.maxDimensionSum ) ) { return _x( 'Image dimensions do not meet this social network’s requirements.', 'text', 'nelio-content' ); } return undefined; }