Skip to content

🎥 Make videos programmatically with React

Implement writeStaticFile()

Jonny Burgerc6c86430b62d
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 9513dc1854..aa7b09090a 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -67,6 +67,7 @@ import {
SaveDefaultProps,
saveStudioSchema,
} from './StudioApis/SaveDefaultProps';
+import {WriteStaticFile} from './StudioApis/WriteStaticFile';
import './style.css';
import {Tailwind} from './Tailwind';
import {TenFrameTester} from './TenFrameTester';
@@ -1226,8 +1227,18 @@ export const Index: React.FC = () => {
component={SaveDefaultProps}
fps={30}
durationInFrames={100}
- height={1080}
- width={1080}
+ height={200}
+ width={200}
+ schema={saveStudioSchema}
+ defaultProps={{color: 'green'}}
+ />
+ <Composition
+ id="write-static-file"
+ component={WriteStaticFile}
+ fps={30}
+ durationInFrames={100}
+ height={200}
+ width={200}
schema={saveStudioSchema}
defaultProps={{color: 'green'}}
/>
diff --git a/packages/example/src/StudioApis/WriteStaticFile.tsx b/packages/example/src/StudioApis/WriteStaticFile.tsx
new file mode 100644
index 0000000000..08718d730d
--- /dev/null
+++ b/packages/example/src/StudioApis/WriteStaticFile.tsx
@@ -0,0 +1,61 @@
+import {writeStaticFile} from '@remotion/studio';
+import React, {useCallback} from 'react';
+import {AbsoluteFill} from 'remotion';
+import {z} from 'zod';
+
+export const saveStudioSchema = z.object({
+ color: z.string(),
+});
+
+export const WriteStaticFile: React.FC = () => {
+ const saveFileRegular = useCallback(async () => {
+ await writeStaticFile({
+ filePath: 'hitehre',
+ contents: 'hello world',
+ });
+
+ console.log('Saved!');
+ }, []);
+
+ const saveFileSubfolder = useCallback(async () => {
+ await writeStaticFile({
+ filePath: 'hitehre/yooo',
+ contents: 'hello world',
+ });
+
+ console.log('Saved!');
+ }, []);
+ const saveFileBackslash = useCallback(async () => {
+ await writeStaticFile({
+ filePath: 'hitehre\\backslash',
+ contents: 'hello world',
+ });
+
+ console.log('Saved!');
+ }, []);
+ const dontAllowOtherFolderThanPublic = useCallback(async () => {
+ await writeStaticFile({
+ filePath: '../otherfolder/hitehre',
+ contents: 'hello world',
+ });
+
+ console.log('Saved!');
+ }, []);
+
+ return (
+ <AbsoluteFill>
+ <button type="button" onClick={saveFileRegular}>
+ save a file
+ </button>
+ <button type="button" onClick={saveFileSubfolder}>
+ subfolder
+ </button>
+ <button type="button" onClick={saveFileBackslash}>
+ backslash
+ </button>
+ <button type="button" onClick={dontAllowOtherFolderThanPublic}>
+ other folder (should error)
+ </button>
+ </AbsoluteFill>
+ );
+};
diff --git a/packages/studio/src/api/write-static-file.ts b/packages/studio/src/api/write-static-file.ts
index 0f951035d4..162ffa041a 100644
--- a/packages/studio/src/api/write-static-file.ts
+++ b/packages/studio/src/api/write-static-file.ts
@@ -2,13 +2,17 @@ export const writeStaticFile = async ({
contents,
filePath,
}: {
- contents: Buffer | ArrayBuffer;
+ contents: string | ArrayBuffer;
filePath: string;
}): Promise<void> => {
const url = new URL('/api/add-asset', window.location.origin);
+ if (filePath.includes('\\')) {
+ return Promise.reject(new Error('File path cannot contain backslashes'));
+ }
+
url.search = new URLSearchParams({
- filePath: [filePath].filter(Boolean).join('/'),
+ filePath,
}).toString();
const response = await fetch(url, {