Skip to content

🎥 Make videos programmatically with React

8.7 KB
Raw Blame
import path from 'node:path';
import {RenderInternals} from '@remotion/renderer';
import {StudioProtocolInternals} from '@remotion/studio-protocol';
import {
	isUrl,
	type InsertCompositionElementRequest,
	type InsertCompositionElementResponse,
	type InsertableCompositionElementPosition,
	type InsertableCompositionElement,
} from '@remotion/studio-shared';
import {writeFileAndNotifyFileWatchers} from '../../file-watcher';
import {insertJsxElementIntoComposition} from '../../helpers/resolve-composition-component';
import type {ApiHandler} from '../api-types';
import {formatLogFileLocation} from '../format-log-file-location';
import {broadcastSequenceNodePathMutation} from '../sequence-node-path-mutation';
import {
	printUndoHint,
	pushToUndoStack,
	suppressUndoStackInvalidation,
} from '../undo-stack';
import {
	getCodemodTimingPrefix,
	withSourceFileWriteQueue,
} from './source-file-write-queue';

const validateDimension = (name: string, value: number) => {
	if (!Number.isFinite(value) || value < 1) {
		throw new Error(`${name} must be a positive number`);
	}
};

const validatePosition = (
	position: InsertableCompositionElementPosition | null,
) => {
	if (position === null) {
		return;
	}

	if (!Number.isFinite(position.x) || !Number.isFinite(position.y)) {
		throw new Error('Position must be finite');
	}
};

const isInsideRemotionRoot = ({
	fileName,
	remotionRoot,
}: {
	fileName: string;
	remotionRoot: string;
}) => {
	const relativePath = path.relative(remotionRoot, fileName);
	return !relativePath.startsWith('..') && !path.isAbsolute(relativePath);
};

const hasParentDirectorySegment = (fileName: string) => {
	return fileName.split('/').includes('..');
};

const validateCompositionFile = ({
	compositionFile,
	remotionRoot,
}: {
	compositionFile: string;
	remotionRoot: string;
}) => {
	if (
		!compositionFile ||
		compositionFile.includes('\0') ||
		compositionFile.includes('\\') ||
		compositionFile.startsWith('/') ||
		hasParentDirectorySegment(compositionFile)
	) {
		throw new Error('Unsupported composition file');
	}

	const resolved = path.resolve(remotionRoot, compositionFile);
	if (!isInsideRemotionRoot({fileName: resolved, remotionRoot})) {
		throw new Error('Unsupported composition file');
	}
};

const validateElement = (
	element: InsertableCompositionElement,
	remotionRoot: string,
) => {
	validatePosition(element.position);

	if (element.type === 'solid') {
		validateDimension('width', element.width);
		validateDimension('height', element.height);
		return;
	}

	if (element.type === 'text') {
		if (typeof element.text !== 'string' || element.text.trim() === '') {
			throw new Error('Text must be a non-empty string');
		}

		validateDimension('fontSize', element.fontSize);
		if (typeof element.color !== 'string' || element.color.trim() === '') {
			throw new Error('Text color must be a non-empty string');
		}

		return;
	}

	if (element.type === 'svg') {
		if (typeof element.markup !== 'string' || element.markup.trim() === '') {
			throw new Error('SVG markup must be a non-empty string');
		}

		return;
	}

	if (element.type === 'asset') {
		if (element.srcType === 'remote') {
			if (!isUrl(element.src)) {
				throw new Error('Remote asset source must be a URL');
			}
		} else if (!element.src || element.src.includes('\\')) {
			throw new Error('Asset path must be a static file path');
		}

		if (element.dimensions) {
			validateDimension('width', element.dimensions.width);
			validateDimension('height', element.dimensions.height);
		}

		if (element.durationInFrames !== null) {
			validateDimension('durationInFrames', element.durationInFrames);
		}

		return;
	}

	if (element.type === 'component') {
		if (!StudioProtocolInternals.isComponentIdentifier(element.componentName)) {
			throw new Error('Unsupported component name');
		}

		if (!StudioProtocolInternals.isComponentIdentifier(element.importName)) {
			throw new Error('Unsupported component import name');
		}

		if (!StudioProtocolInternals.isComponentImportPath(element.importPath)) {
			throw new Error('Unsupported component import path');
		}

		if (!StudioProtocolInternals.areComponentProps(element.props)) {
			throw new Error('Unsupported component props');
		}

		return;
	}

	if (element.type === 'composition') {
		if (typeof element.compositionId !== 'string' || !element.compositionId) {
			throw new Error('Unsupported composition ID');
		}

		if (typeof element.compositionFile !== 'string') {
			throw new Error('Unsupported composition file');
		}

		validateCompositionFile({
			compositionFile: element.compositionFile,
			remotionRoot,
		});

		validateDimension('width', element.width);
		validateDimension('height', element.height);
		validateDimension('durationInFrames', element.durationInFrames);

		const parsedProps: unknown = JSON.parse(
			element.serializedResolvedPropsWithCustomSchema,
		);
		if (
			typeof parsedProps !== 'object' ||
			parsedProps === null ||
			Array.isArray(parsedProps)
		) {
			throw new Error('Resolved composition props must be an object');
		}

		return;
	}

	throw new Error('Unsupported element type');
};

const getElementLabel = (element: InsertableCompositionElement) => {
	if (element.type === 'text') {
		return '<Interactive.Div>';
	}

	if (element.type === 'solid') {
		return '<Solid>';
	}

	if (element.type === 'svg') {
		return '<Interactive.Svg>';
	}

	if (element.type === 'asset') {
		if (element.assetType === 'image') {
			return '<CanvasImage>';
		}

		if (element.assetType === 'video') {
			return '<Video>';
		}

		if (element.assetType === 'gif') {
			return '<Gif>';
		}

		if (element.assetType === 'animated-image') {
			return '<AnimatedImage>';
		}

		if (element.assetType === 'audio') {
			return '<Audio>';
		}
	}

	if (element.type === 'component') {
		return `<${element.componentName}>`;
	}

	if (element.type === 'composition') {
		return `composition "${element.compositionId}"`;
	}

	throw new Error('Unsupported element type');
};

export const insertCompositionElementHandler: ApiHandler<
	InsertCompositionElementRequest,
	InsertCompositionElementResponse
> = ({
	input: {compositionFile, compositionId, element, from},
	remotionRoot,
	logLevel,
}) =>
	withSourceFileWriteQueue(async () => {
		try {
			validateElement(element, remotionRoot);
			if (
				from !== null &&
				(!Number.isInteger(from) || !Number.isFinite(from) || from < 0)
			) {
				throw new Error('from must be a non-negative integer');
			}

			const elementLabel = getElementLabel(element);

			RenderInternals.Log.trace(
				{indent: false, logLevel},
				`[insert-composition-element] Received request for compositionFile="${compositionFile}" compositionId="${compositionId}" element="${element.type}"`,
			);

			const {
				fileName,
				source,
				oldContents,
				output,
				insertedNodePath,
				logLine,
				nodePathRemappings,
			} = await insertJsxElementIntoComposition({
				remotionRoot,
				compositionFile,
				compositionId,
				element,
				from,
				prettierConfigOverride: null,
				sourceFileOverrides: null,
			});
			const nodePathMutation = broadcastSequenceNodePathMutation(
				[
					{
						absolutePath: fileName,
						remappings: nodePathRemappings,
					},
				],
				insertedNodePath === null
					? null
					: {
							absolutePath: fileName,
							compositionId,
							nodePath: insertedNodePath,
						},
			);
			if (insertedNodePath === null) {
				RenderInternals.Log.warn(
					{indent: false, logLevel},
					'Could not determine the inserted JSX element node path. Skipping automatic selection.',
				);
			}

			pushToUndoStack({
				filePath: fileName,
				oldContents,
				newContents: output,
				logLevel,
				remotionRoot,
				logLine,
				description: {
					undoMessage: `↩️  Added ${elementLabel}`,
					redoMessage: `↪️  Added ${elementLabel}`,
				},
				entryType: 'insert-composition-element',
				suppressHmrOnFileRestore: false,
				nodePathRemappings,
			});
			suppressUndoStackInvalidation(fileName);
			writeFileAndNotifyFileWatchers({
				file: fileName,
				content: output,
				originatorClientId: undefined,
				metadata: {skipSequencePropsUpdate: true},
			});

			const locationLabel = formatLogFileLocation({
				remotionRoot,
				absolutePath: fileName,
				line: logLine,
			});
			RenderInternals.Log.info(
				{indent: false, logLevel},
				`${getCodemodTimingPrefix(logLevel)}${RenderInternals.chalk.blueBright(`${locationLabel}`)} Added ${elementLabel}`,
			);
			RenderInternals.Log.verbose(
				{indent: false, logLevel},
				`[insert-composition-element] Wrote ${source}`,
			);

			printUndoHint(logLevel);

			return {
				success: true,
				insertedNodePath:
					insertedNodePath === null
						? null
						: {absolutePath: fileName, nodePath: insertedNodePath},
				nodePathMutation,
			};
		} catch (err) {
			return {
				success: false,
				reason: (err as Error).message,
				stack: (err as Error).stack as string,
			};
		}
	});