Skip to content

🎥 Make videos programmatically with React

code working

Patric Salvisberg520de7aa0be1
diff --git a/packages/core/src/static-file.ts b/packages/core/src/static-file.ts
index 345a87dfa2..de1e226f0c 100644
--- a/packages/core/src/static-file.ts
+++ b/packages/core/src/static-file.ts
@@ -1,3 +1,74 @@
+export type HexCode =
+ | '%3A'
+ | '%2F'
+ | '%3F'
+ | '%23'
+ | '%5B'
+ | '%5D'
+ | '%40'
+ | '%21'
+ | '%24'
+ | '%26'
+ | '%27'
+ | '%28'
+ | '%29'
+ | '%2A'
+ | '%2B'
+ | '%2C'
+ | '%3B';
+
+export type HexInfo =
+ | {
+ containsHex: false;
+ }
+ | {
+ containsHex: true;
+ hexCode: HexCode;
+ };
+
+const didWarn: {[key: string]: boolean} = {};
+const warnOnce = (message: string) => {
+ if (didWarn[message]) {
+ return;
+ }
+
+ console.warn(message);
+ didWarn[message] = true;
+};
+
+export const includesHexOfUnsafeChar = (path: string): HexInfo => {
+ const decodedChars = {
+ '%3A': ':',
+ '%2F': '/',
+ '%3F': '?',
+ '%23': '#',
+ '%5B': '[',
+ '%5D': ']',
+ '%40': '@',
+ '%21': '!',
+ '%24': '$',
+ '%26': '&',
+ '%27': "'",
+ '%28': '(',
+ '%29': ')',
+ '%2A': '*',
+ '%2B': '+',
+ '%2C': ',',
+ '%3B': ';',
+ };
+
+ for (const key of Object.keys(
+ decodedChars
+ ) as (keyof typeof decodedChars)[]) {
+ if (path.includes(key)) {
+ console.log(`Path contains ${decodedChars[key]}`);
+ return {containsHex: true, hexCode: key as HexCode};
+ }
+ }
+
+ return {containsHex: false};
+};
+
const trimLeadingSlash = (path: string): string => {
if (path.startsWith('/')) {
return trimLeadingSlash(path.substr(1));
@@ -53,7 +124,15 @@ export const staticFile = (path: string) => {
);
}
- const preparsed = inner(path);
+ const encodedURI = encodeURIComponent(path);
+ const preparsed = inner(encodedURI);
+ const includesHex = includesHexOfUnsafeChar(preparsed);
+ if (includesHex.containsHex) {
+ warnOnce(
+ `WARNING: You seem to pass an already encoded path (path contains ${includesHex.hexCode}). The encoding gets automatically handled by staticFile() `
+ );
+ }
+
if (!preparsed.startsWith('/')) {
return `/${preparsed}`;
}
diff --git a/packages/core/src/test/includes-hex-of-unsafe-char.test.ts b/packages/core/src/test/includes-hex-of-unsafe-char.test.ts
new file mode 100644
index 0000000000..5fa1d13a4c
--- /dev/null
+++ b/packages/core/src/test/includes-hex-of-unsafe-char.test.ts
@@ -0,0 +1,14 @@
+import {expect, test} from 'vitest';
+import type {HexInfo} from '../static-file.js';
+import {includesHexOfUnsafeChar} from '../static-file.js';
+
+test('should find hex', () => {
+ const unsafeName = 'test%2Fexample#music';
+
+ const actual = includesHexOfUnsafeChar(unsafeName);
+ const expected: HexInfo = {
+ containsHex: true,
+ hexCode: '%2F',
+ };
+ expect(actual).toEqual(expected);
+});
diff --git a/packages/core/src/test/static-file-safe-uri.test.ts b/packages/core/src/test/static-file-safe-uri.test.ts
new file mode 100644
index 0000000000..115ced58ba
--- /dev/null
+++ b/packages/core/src/test/static-file-safe-uri.test.ts
@@ -0,0 +1,12 @@
+import {expect, test} from 'vitest';
+import {staticFile} from '../static-file.js';
+
+test('staticFile() should convert # into %23', () => {
+ const unsafeName = 'test/example#music';
+ expect(staticFile(unsafeName)).toBe('/test%2Fexample%23music');
+});
+
+test('staticFile() passing already encoded path should give warning', () => {
+ const unsafeName = '/test%2Fexample%23music';
+ expect(staticFile(unsafeName)).toBe('/test%2Fexample%23music');
+});
diff --git a/packages/core/src/use-media-in-timeline.ts b/packages/core/src/use-media-in-timeline.ts
index 5f3856baf6..11e4dd54f9 100644
--- a/packages/core/src/use-media-in-timeline.ts
+++ b/packages/core/src/use-media-in-timeline.ts
@@ -14,7 +14,6 @@ import type {VolumeProp} from './volume-prop.js';
import {evaluateVolume} from './volume-prop.js';
const didWarn: {[key: string]: boolean} = {};
-
const warnOnce = (message: string) => {
if (didWarn[message]) {
return;