Skip to content

🎥 Make videos programmatically with React

tweak asset names

Jonny Burger9899f2d41052
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 481784c501..e58e1b57cf 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
@@ -1,8 +1,7 @@
import {createWriteStream, mkdirSync} from 'fs';
import got from 'got';
import path from 'path';
-import crypto from 'crypto';
-import {TAsset} from 'remotion';
+import {random, TAsset} from 'remotion';
import stream from 'stream';
import {promisify} from 'util';
@@ -64,11 +63,17 @@ export const downloadAndMapAssetsToFileUrl = async ({
onDownload: (src: string) => void;
}): Promise<TAsset> => {
const {pathname, search} = new URL(localhostAsset.src);
- const hashedFileName = crypto.createHash('sha1')
- .update(`${pathname}${search}`)
- .digest('hex')
- .substring(0, 10);
- const newSrc = path.join(webpackBundle, hashedFileName);
+
+ 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, hashedFileName + fileExtension);
if (localhostAsset.isRemote) {
await downloadAsset(localhostAsset.src, newSrc, onDownload);
}
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 2ded0b94c8..101987c7a1 100644
--- a/packages/renderer/src/test/handle-weird-file-names.test.ts
+++ b/packages/renderer/src/test/handle-weird-file-names.test.ts
@@ -18,7 +18,54 @@ test('Should sanitize weird file names when downloading', async () => {
});
expect(newAsset.src).toBe(
process.platform === 'win32'
- ? '\\var\\tmp\\aWHv6qn3/X'
- : '/var/tmp/aWHv6qn3/X'
+ ? '\\var\\tmp\\06788239371962845'
+ : '/var/tmp/06788239371962845'
);
});
+
+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,
+ },
+ 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,
+ },
+ 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,
+ },
+ webpackBundle: '/var/tmp',
+ onDownload: () => undefined,
+ });
+ expect(asset1.src).toEqual(sameAgain.src);
+ expect(asset1.src).not.toEqual(differentAsset.src);
+});
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 f51e004a39..27731c15d2 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/BNuOOK3RlK'
+ '/var/folders/hl/p8pg9kw15dbg3l7dbpn0scc80000gn/T/react-motion-graphicstoSTC7/7847861938644201.mp4'
),
id: '0.8331499681195862',
frame: 2045,