gongxiangp2/lib/xiaoyi-delivery-material.ts

45 lines
2.0 KiB
TypeScript

export const XIAOYI_DELIVERY_MATERIAL_TYPES = {
detail: '自动上架-详情',
video: '自动上架-视频',
buyer_show: '自动上架-买家秀',
} as const
export type XiaoyiDeliveryKind = keyof typeof XIAOYI_DELIVERY_MATERIAL_TYPES
export const XIAOYI_DELIVERY_TYPES = Object.values(XIAOYI_DELIVERY_MATERIAL_TYPES)
export const XIAOYI_DETAIL_ASSET_GROUPS = ['main', 'sku', 'detail'] as const
export const XIAOYI_INTERNAL_ASSET_GROUPS = ['longImage', 'projectSource', 'psd'] as const
export const XIAOYI_EXTERNAL_ASSET_GROUPS = ['main', 'sku', 'detail', 'video', 'buyer_show'] as const
export const XIAOYI_DELIVERY_LOG_ACTION = 'XIAOYI_DELIVERY'
export type XiaoyiAssetSpec = { specId?: string; variantId?: string; variantName?: string }
export type XiaoyiDeliveryMetadata = { storeCategoryName?: string; assetSpecs: Record<string, XiaoyiAssetSpec> }
export function readXiaoyiDeliveryMetadata(changes?: string | null): XiaoyiDeliveryMetadata | null {
if (!changes) return null
try {
return JSON.parse(changes)?.deliveryMetadata?.newValue ?? null
} catch {
return null
}
}
export function xiaoyiSkuDimensions(assetType: string, fileUrl?: string | null) {
const sizes = assetType === 'sku' && fileUrl
? /(?:^|\/)xiaoyi-deliveries\/[^/]+\/[^/]+\/[^/]+\/sku\/([1-9]\d*)x([1-9]\d*)\//.exec(fileUrl.replace(/\\/g, '/').split(/[?#]/, 1)[0])
: null
if (sizes) {
const width = Number(sizes[1])
const height = Number(sizes[2])
if (Number.isSafeInteger(width) && Number.isSafeInteger(height)) {
let divisor = width
let remainder = height
while (remainder) [divisor, remainder] = [remainder, divisor % remainder]
return { width, height, resolution: `${width}x${height}`, aspectRatio: `${width / divisor}:${height / divisor}` }
}
}
return { width: null, height: null, resolution: null, aspectRatio: null }
}
export function isXiaoyiDeliveryMaterial(type: string): boolean {
return (XIAOYI_DELIVERY_TYPES as readonly string[]).includes(type)
}