import querystring from 'querystring'; import type com from '../../proto/index.js'; import { ConverterSet, ConverterType } from '../domain.js'; import { alignIfLegacy, decodeText, encodeText, URL_HASH_PREFIX, WIX_PROTOCOL, } from './converters-utils.js'; const WIX_VIDEO = 'video'; export const videoV2: ConverterSet = { types: ['wix.common.VideoV2'], [ConverterType.FROM_JSON]: { transform: (val: com.wix.common.VideoV2) => { if (!val) { return; } let fileName = ''; if (val?.filename) { fileName = `/${encodeText(val.filename)}`; } let posterData = ''; if (val.posters?.length) { const [poster1, poster2] = val.posters; const poster = poster2 || poster1; let posterId: string = poster.id || ''; if (!posterId && poster.url) { const idx = poster.url.lastIndexOf('/'); if (idx !== -1) { posterId = poster.url.substring(idx + 1); } } if (posterId) { posterData = `${URL_HASH_PREFIX}posterUri=${posterId}&posterWidth=${ poster!.width }&posterHeight=${poster!.height}`; } } return val.id ? `wix:video://v1/${val.id}${fileName}${posterData}` : val.url; }, }, [ConverterType.TO_JSON]: { transform: (val: string): com.wix.common.VideoV2 | undefined => { if (!val) { return; } const alignedVideo = alignIfLegacy(val, WIX_VIDEO); const { protocol, hash, pathname } = new URL(alignedVideo); const { posterHeight: height, posterWidth: width, posterUri, } = querystring.parse(hash.replace(URL_HASH_PREFIX, '')); const [id, fileName] = pathname .replace(`${WIX_VIDEO}://v1/`, '') .split('/'); if (protocol === WIX_PROTOCOL) { let res: com.wix.common.VideoV2 = { id }; if (fileName) { res = { ...res, filename: decodeText(fileName) }; } if (!posterUri) { return res; } return { ...res, posters: [ { id: posterUri.toString(), height: Number(height), width: Number(width), }, ], }; } return { url: val }; }, }, };