Skip to content

🎥 Make videos programmatically with React

fix tests

Jonny Burgercf570a6f94dd
diff --git a/packages/renderer/src/assets/download-and-map-assets-to-file.ts b/packages/renderer/src/assets/download-and-map-assets-to-file.ts
index 0f4699c8dd..b97ff4f1be 100644
--- a/packages/renderer/src/assets/download-and-map-assets-to-file.ts
+++ b/packages/renderer/src/assets/download-and-map-assets-to-file.ts
@@ -54,6 +54,36 @@ const downloadAsset = async (
notifyAssetIsDownloaded(src);
};
+export const getSanitizedFilenameForAssetUrl = ({
+ src,
+ isRemote,
+ webpackBundle,
+}: {
+ src: string;
+ isRemote: boolean;
+ webpackBundle: string;
+}) => {
+ const {pathname, search} = new URL(src);
+
+ if (!isRemote) {
+ return path.join(webpackBundle, sanitizeFilename(pathname));
+ }
+
+ const split = pathname.split('.');
+ const fileExtension =
+ split.length > 1 && split[split.length - 1]
+ ? `.${split[split.length - 1]}`
+ : '';
+ const hashedFileName = String(random(`${pathname}${search}`)).replace(
+ '0.',
+ ''
+ );
+ return path.join(
+ webpackBundle,
+ sanitizeFilename(hashedFileName + fileExtension)
+ );
+};
+
export const downloadAndMapAssetsToFileUrl = async ({
localhostAsset,
webpackBundle,
@@ -63,31 +93,18 @@ export const downloadAndMapAssetsToFileUrl = async ({
webpackBundle: string;
onDownload: (src: string) => void;
}): Promise<TAsset> => {
- const {pathname, search} = new URL(localhostAsset.src);
+ const newSrc = getSanitizedFilenameForAssetUrl({
+ src: localhostAsset.src,
+ isRemote: localhostAsset.isRemote,
+ webpackBundle,
+ });
if (localhostAsset.isRemote) {
- const split = pathname.split('.');
- const fileExtension =
- split.length > 1 && split[split.length - 1]
- ? `.${split[split.length - 1]}`
- : '';
- const hashedFileName = String(random(`${pathname}${search}`)).replace(
- '0.',
- ''
- );
- const newSrc = path.join(
- webpackBundle,
- sanitizeFilename(hashedFileName + fileExtension)
- );
await downloadAsset(localhostAsset.src, newSrc, onDownload);
- return {
- ...localhostAsset,
- src: newSrc,
- };
}
return {
...localhostAsset,
- src: path.join(webpackBundle, sanitizeFilename(pathname)),
+ src: newSrc,
};
};
diff --git a/packages/renderer/src/test/handle-weird-file-names.test.ts b/packages/renderer/src/test/handle-weird-file-names.test.ts
index 101987c7a1..246db5816e 100644
--- a/packages/renderer/src/test/handle-weird-file-names.test.ts
+++ b/packages/renderer/src/test/handle-weird-file-names.test.ts
@@ -1,71 +1,34 @@
-import {downloadAndMapAssetsToFileUrl} from '../assets/download-and-map-assets-to-file';
+import {getSanitizedFilenameForAssetUrl} from '../assets/download-and-map-assets-to-file';
test('Should sanitize weird file names when downloading', async () => {
- const newAsset = await downloadAndMapAssetsToFileUrl({
- localhostAsset: {
- src:
- 'http://localhost:3000/gtts-api.miniggiodev.fr/Ici+Japon+Corp.?lang=ja',
- frame: 0,
- id: 'japon',
- isRemote: false,
- mediaFrame: 1,
- type: 'video',
- volume: 1,
- playbackRate: 1,
- },
+ const newSrc = getSanitizedFilenameForAssetUrl({
+ src: 'http://gtts-api.miniggiodev.fr/Ici+Japon+Corp.?lang=ja',
+ isRemote: true,
webpackBundle: '/var/tmp',
- onDownload: () => undefined,
});
- expect(newAsset.src).toBe(
+ expect(newSrc).toBe(
process.platform === 'win32'
- ? '\\var\\tmp\\06788239371962845'
- : '/var/tmp/06788239371962845'
+ ? '\\var\\tmp\\7415404696948826'
+ : '/var/tmp/7415404696948826'
);
});
test('Should give different file names based on different url query parameters', async () => {
- const asset1 = await downloadAndMapAssetsToFileUrl({
- localhostAsset: {
- src: 'https://gtts-api.miniggiodev.fr/Ici+Japon+Corp.mp4?hi=1',
- frame: 0,
- id: 'japon',
- isRemote: false,
- mediaFrame: 1,
- type: 'video',
- volume: 1,
- playbackRate: 1,
- },
+ const asset1 = getSanitizedFilenameForAssetUrl({
+ src: 'https://gtts-api.miniggiodev.fr/Ici+Japon+Corp.mp4?hi=1',
+ isRemote: true,
webpackBundle: '/var/tmp',
- onDownload: () => undefined,
});
- const sameAgain = await downloadAndMapAssetsToFileUrl({
- localhostAsset: {
- src: 'https://gtts-api.miniggiodev.fr/Ici+Japon+Corp.mp4?hi=1',
- frame: 0,
- id: 'japon',
- isRemote: false,
- mediaFrame: 1,
- type: 'video',
- volume: 1,
- playbackRate: 1,
- },
+ const sameAgain = getSanitizedFilenameForAssetUrl({
+ src: 'https://gtts-api.miniggiodev.fr/Ici+Japon+Corp.mp4?hi=1',
+ isRemote: true,
webpackBundle: '/var/tmp',
- onDownload: () => undefined,
});
- const differentAsset = await downloadAndMapAssetsToFileUrl({
- localhostAsset: {
- src: 'https://gtts-api.miniggiodev.fr/Ici+Japon+Corp.mp4?hi=2',
- frame: 0,
- id: 'japon',
- isRemote: false,
- mediaFrame: 1,
- type: 'video',
- volume: 1,
- playbackRate: 1,
- },
+ const differentAsset = getSanitizedFilenameForAssetUrl({
+ src: 'https://gtts-api.miniggiodev.fr/Ici+Japon+Corp.mp4?hi=2',
+ isRemote: true,
webpackBundle: '/var/tmp',
- onDownload: () => undefined,
});
- expect(asset1.src).toEqual(sameAgain.src);
- expect(asset1.src).not.toEqual(differentAsset.src);
+ expect(asset1).toEqual(sameAgain);
+ expect(asset1).not.toEqual(differentAsset);
});
diff --git a/packages/renderer/src/test/map-localhost-to-file.test.ts b/packages/renderer/src/test/map-localhost-to-file.test.ts
index 27731c15d2..52f9bf2ee1 100644
--- a/packages/renderer/src/test/map-localhost-to-file.test.ts
+++ b/packages/renderer/src/test/map-localhost-to-file.test.ts
@@ -21,7 +21,7 @@ test('Should correctly map a localhost URL to an asset', async () => {
).toEqual({
type: 'video',
src: fileNameInOs(
- '/var/folders/hl/p8pg9kw15dbg3l7dbpn0scc80000gn/T/react-motion-graphicstoSTC7/7847861938644201.mp4'
+ '/var/folders/hl/p8pg9kw15dbg3l7dbpn0scc80000gn/T/react-motion-graphicstoSTC7/5f25ba62771d1f8195f858ec5ff8e8d6.mp4'
),
id: '0.8331499681195862',
frame: 2045,