Skip to content

🎥 Make videos programmatically with React

open in file explorer API

Jonny Burger16c7277bbe07
diff --git a/packages/cli/src/editor/components/RenderQueue/RenderQueueOutputName.tsx b/packages/cli/src/editor/components/RenderQueue/RenderQueueOutputName.tsx
index 4b151888fa..50dc87855b 100644
--- a/packages/cli/src/editor/components/RenderQueue/RenderQueueOutputName.tsx
+++ b/packages/cli/src/editor/components/RenderQueue/RenderQueueOutputName.tsx
@@ -1,5 +1,6 @@
import React, {useCallback, useMemo} from 'react';
import type {RenderJob} from '../../../preview-server/render-queue/job';
+import {notificationCenter} from '../Notifications/NotificationCenter';
import {openInFileExplorer} from './actions';
import {renderQueueItemSubtitleStyle} from './item-style';
@@ -21,7 +22,14 @@ export const RenderQueueOutputName: React.FC<{
job: RenderJob;
}> = ({job}) => {
const onClick = useCallback(() => {
- openInFileExplorer({directory: job.outputLocation});
+ openInFileExplorer({directory: job.outputLocation}).catch((err) => {
+ notificationCenter.current?.addNotification({
+ content: `Could not open file: ${err.message}`,
+ created: Date.now(),
+ duration: 2000,
+ id: String(Math.random()),
+ });
+ });
}, [job.outputLocation]);
const style = useMemo((): React.CSSProperties => {
diff --git a/packages/cli/src/editor/components/RenderQueue/actions.ts b/packages/cli/src/editor/components/RenderQueue/actions.ts
index b53b2389a7..c62c0c3092 100644
--- a/packages/cli/src/editor/components/RenderQueue/actions.ts
+++ b/packages/cli/src/editor/components/RenderQueue/actions.ts
@@ -5,13 +5,6 @@ import type {
OpenInFileExplorerRequest,
RenderJob,
} from '../../../preview-server/render-queue/job';
-import {notificationCenter} from '../Notifications/NotificationCenter';
-
-export const removeRenderJob = (job: RenderJob) => {
- return callApi('/api/remove-render', {
- jobId: job.id,
- });
-};
export const callApi = <Endpoint extends keyof ApiRoutes>(
endpoint: Endpoint,
@@ -88,32 +81,11 @@ export const openInFileExplorer = ({directory}: {directory: string}) => {
const body: OpenInFileExplorerRequest = {
directory,
};
- fetch(`/api/open-in-file-explorer`, {
- method: 'post',
- headers: {
- 'content-type': 'application/json',
- },
- body: JSON.stringify(body),
- })
- .then((res) => res.json())
- .then((data: {success: true} | {success: false; error: string}) => {
- if (data.success) {
- console.log('Opened file in explorer');
- } else {
- notificationCenter.current?.addNotification({
- content: `Could not open file: ${data.error}`,
- created: Date.now(),
- duration: 2000,
- id: String(Math.random()),
- });
- }
- })
- .catch((err) => {
- notificationCenter.current?.addNotification({
- content: `Could not open file: ${err.message}`,
- created: Date.now(),
- duration: 2000,
- id: String(Math.random()),
- });
- });
+ return callApi('/api/open-in-file-explorer', body);
+};
+
+export const removeRenderJob = (job: RenderJob) => {
+ return callApi('/api/remove-render', {
+ jobId: job.id,
+ });
};
diff --git a/packages/cli/src/preview-server/api-routes.ts b/packages/cli/src/preview-server/api-routes.ts
index 398a00ee7d..8ba7ecec05 100644
--- a/packages/cli/src/preview-server/api-routes.ts
+++ b/packages/cli/src/preview-server/api-routes.ts
@@ -1,5 +1,6 @@
import type {ApiHandler, ApiRoutes} from './api-types';
import {handleAddRender} from './routes/add-render';
+import {handleOpenInFileExplorer} from './routes/open-in-file-explorer';
import {handleRemoveRender} from './routes/remove-render';
import {subscribeToFileExistence} from './routes/subscribe-to-file-existence';
import {unsubscribeFromFileExistence} from './routes/unsubscribe-from-file-existence';
@@ -14,4 +15,5 @@ export const allApiRoutes: {
'/api/unsubscribe-from-file-existence': unsubscribeFromFileExistence,
'/api/subscribe-to-file-existence': subscribeToFileExistence,
'/api/remove-render': handleRemoveRender,
+ '/api/open-in-file-explorer': handleOpenInFileExplorer,
};
diff --git a/packages/cli/src/preview-server/api-types.ts b/packages/cli/src/preview-server/api-types.ts
index d99cc8f29d..77ff086151 100644
--- a/packages/cli/src/preview-server/api-types.ts
+++ b/packages/cli/src/preview-server/api-types.ts
@@ -1,6 +1,7 @@
import type {IncomingMessage, ServerResponse} from 'http';
import type {
AddRenderRequest,
+ OpenInFileExplorerRequest,
RemoveRenderRequest,
SubscribeToFileExistenceRequest,
SubscribeToFileExistenceResponse,
@@ -36,4 +37,5 @@ export type ApiRoutes = {
SubscribeToFileExistenceResponse
>;
'/api/remove-render': ReqAndRes<RemoveRenderRequest, undefined>;
+ '/api/open-in-file-explorer': ReqAndRes<OpenInFileExplorerRequest, void>;
};
diff --git a/packages/cli/src/preview-server/render-queue/open-in-file-explorer.ts b/packages/cli/src/preview-server/render-queue/open-in-file-explorer.ts
deleted file mode 100644
index 9b4f0732fc..0000000000
--- a/packages/cli/src/preview-server/render-queue/open-in-file-explorer.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-import type {IncomingMessage, ServerResponse} from 'http';
-import {parseRequestBody} from '../parse-body';
-import type {OpenInFileExplorerRequest} from './job';
-import {openDirectoryInFinder} from './open-directory-in-finder';
-
-export const handleOpenInFileExplorer = async (
- req: IncomingMessage,
- res: ServerResponse
-) => {
- if (req.method === 'OPTIONS') {
- res.statusCode = 200;
- res.end();
- return;
- }
-
- try {
- const body = (await parseRequestBody(req)) as OpenInFileExplorerRequest;
-
- // TODO: Disallow opening file that is not in Remotion CWD
- await openDirectoryInFinder(body.directory);
-
- res.setHeader('content-type', 'application/json');
- res.writeHead(200);
-
- res.end(
- JSON.stringify({
- success: true,
- })
- );
- } catch (err) {
- res.setHeader('content-type', 'application/json');
- res.writeHead(200);
-
- res.end(
- JSON.stringify({
- success: false,
- error: (err as Error).message,
- })
- );
- }
-};
diff --git a/packages/cli/src/preview-server/routes.ts b/packages/cli/src/preview-server/routes.ts
index 5782826a13..7bd89db0e9 100644
--- a/packages/cli/src/preview-server/routes.ts
+++ b/packages/cli/src/preview-server/routes.ts
@@ -19,7 +19,6 @@ import {handleRequest} from './handler';
import type {LiveEventsServer} from './live-events';
import {parseRequestBody} from './parse-body';
import {getProjectInfo} from './project-info';
-import {handleOpenInFileExplorer} from './render-queue/open-in-file-explorer';
import {getRenderQueue} from './render-queue/queue';
import {serveStatic} from './serve-static';
import {isUpdateAvailableWithTimeout} from './update-available';
@@ -234,10 +233,6 @@ export const handleRoutes = ({
return handleOpenInEditor(remotionRoot, request, response);
}
- if (url.pathname === '/api/open-in-file-explorer') {
- return handleOpenInFileExplorer(request, response);
- }
-
for (const [key, value] of Object.entries(allApiRoutes)) {
if (url.pathname === key) {
return handleRequest({
diff --git a/packages/cli/src/preview-server/routes/open-in-file-explorer.ts b/packages/cli/src/preview-server/routes/open-in-file-explorer.ts
new file mode 100644
index 0000000000..5f004f4352
--- /dev/null
+++ b/packages/cli/src/preview-server/routes/open-in-file-explorer.ts
@@ -0,0 +1,11 @@
+import type {ApiHandler} from '../api-types';
+import type {OpenInFileExplorerRequest} from '../render-queue/job';
+import {openDirectoryInFinder} from '../render-queue/open-directory-in-finder';
+
+export const handleOpenInFileExplorer: ApiHandler<
+ OpenInFileExplorerRequest,
+ void
+> = ({input: {directory}}) => {
+ // TODO: Disallow opening file that is not in Remotion CWD
+ return openDirectoryInFinder(directory);
+};