summaryrefslogtreecommitdiff
path: root/apps/web/src/services/storage/migrations/v1-to-v2.ts
blob: 05d302ead763359ac6c5c5c25480edbff11e51fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import {
	DEFAULT_BLUR_INTENSITY,
	DEFAULT_CANVAS_SIZE,
	DEFAULT_COLOR,
	DEFAULT_FPS,
} from "@/constants/project-constants";
import { IndexedDBAdapter } from "@/services/storage/indexeddb-adapter";
import { StorageMigration } from "./base";

type ProjectRecord = Record<string, unknown>;

export class V1toV2Migration extends StorageMigration {
	from = 1;
	to = 2;

	async run(): Promise<void> {
		const projectsAdapter = new IndexedDBAdapter<unknown>(
			"video-editor-projects",
			"projects",
			1,
		);
		const projects = await projectsAdapter.getAll();

		for (const project of projects) {
			if (!isRecord(project)) {
				continue;
			}

			const projectId = getProjectId({ project });
			if (!projectId) {
				continue;
			}

			if (isV2Project({ project })) {
				continue;
			}

			const migratedProject = migrateProject({ project, projectId });
			await projectsAdapter.set(projectId, migratedProject);
		}
	}
}

function migrateProject({
	project,
	projectId,
}: {
	project: ProjectRecord;
	projectId: string;
}): ProjectRecord {
	const createdAt = normalizeDateString({ value: project.createdAt });
	const updatedAt = normalizeDateString({ value: project.updatedAt });
	const metadataValue = project.metadata;

	const metadata = isRecord(metadataValue)
		? {
				id: getStringValue({ value: metadataValue.id, fallback: projectId }),
				name: getStringValue({ value: metadataValue.name, fallback: "" }),
				thumbnail: getStringValue({ value: metadataValue.thumbnail }),
				createdAt: normalizeDateString({ value: metadataValue.createdAt }),
				updatedAt: normalizeDateString({ value: metadataValue.updatedAt }),
			}
		: {
				id: projectId,
				name: getStringValue({ value: project.name, fallback: "" }),
				thumbnail: getStringValue({ value: project.thumbnail }),
				createdAt,
				updatedAt,
			};

	const scenesValue = project.scenes;
	const scenes = Array.isArray(scenesValue) ? scenesValue : [];
	const legacyBookmarks = Array.isArray(project.bookmarks)
		? project.bookmarks
		: null;
	const normalizedScenes = applyLegacyBookmarks({
		scenes,
		legacyBookmarks,
	});

	const settingsValue = project.settings;
	const settings = isRecord(settingsValue)
		? {
				fps: getNumberValue({
					value: settingsValue.fps,
					fallback: DEFAULT_FPS,
				}),
				canvasSize: getCanvasSizeValue({
					value: settingsValue.canvasSize,
					fallback: DEFAULT_CANVAS_SIZE,
				}),
				background: getBackgroundValue({
					value: settingsValue.background,
				}),
			}
		: {
				fps: getNumberValue({ value: project.fps, fallback: DEFAULT_FPS }),
				canvasSize: getCanvasSizeValue({
					value: project.canvasSize,
					fallback: DEFAULT_CANVAS_SIZE,
				}),
				background: getBackgroundValue({
					value: project.background,
					backgroundType: project.backgroundType,
					backgroundColor: project.backgroundColor,
					blurIntensity: project.blurIntensity,
				}),
			};

	const currentSceneId = getCurrentSceneId({
		value: project.currentSceneId,
		scenes: normalizedScenes,
	});

	return {
		...project,
		metadata,
		scenes: normalizedScenes,
		currentSceneId,
		settings,
		version: 2,
	};
}

function getProjectId({ project }: { project: ProjectRecord }): string | null {
	const idValue = project.id;
	if (typeof idValue === "string" && idValue.length > 0) {
		return idValue;
	}

	const metadataValue = project.metadata;
	if (!isRecord(metadataValue)) {
		return null;
	}

	const metadataId = metadataValue.id;
	if (typeof metadataId === "string" && metadataId.length > 0) {
		return metadataId;
	}

	return null;
}

function getCurrentSceneId({
	value,
	scenes,
}: {
	value: unknown;
	scenes: unknown[];
}): string {
	if (typeof value === "string" && value.length > 0) {
		return value;
	}

	const mainSceneId = findMainSceneId({ scenes });
	if (mainSceneId) {
		return mainSceneId;
	}

	return "";
}

function findMainSceneId({ scenes }: { scenes: unknown[] }): string | null {
	for (const scene of scenes) {
		if (!isRecord(scene)) {
			continue;
		}

		if (scene.isMain === true && typeof scene.id === "string") {
			return scene.id;
		}
	}

	for (const scene of scenes) {
		if (!isRecord(scene)) {
			continue;
		}

		if (typeof scene.id === "string") {
			return scene.id;
		}
	}

	return null;
}

function applyLegacyBookmarks({
	scenes,
	legacyBookmarks,
}: {
	scenes: unknown[];
	legacyBookmarks: unknown[] | null;
}): unknown[] {
	if (!legacyBookmarks || legacyBookmarks.length === 0) {
		return scenes;
	}

	const mainSceneId = findMainSceneId({ scenes });

	return scenes.map((scene) => {
		if (!isRecord(scene)) {
			return scene;
		}

		if (mainSceneId && scene.id !== mainSceneId) {
			return scene;
		}

		if (Array.isArray(scene.bookmarks) && scene.bookmarks.length > 0) {
			return scene;
		}

		return {
			...scene,
			bookmarks: legacyBookmarks,
		};
	});
}

function getBackgroundValue({
	value,
	backgroundType,
	backgroundColor,
	blurIntensity,
}: {
	value: unknown;
	backgroundType?: unknown;
	backgroundColor?: unknown;
	blurIntensity?: unknown;
}): {
	type: "color" | "blur";
	color?: string;
	blurIntensity?: number;
} {
	if (isRecord(value)) {
		const typeValue = value.type;
		if (typeValue === "blur") {
			return {
				type: "blur",
				blurIntensity: getNumberValue({
					value: value.blurIntensity,
					fallback: DEFAULT_BLUR_INTENSITY,
				}),
			};
		}

		return {
			type: "color",
			color: getStringValue({ value: value.color, fallback: DEFAULT_COLOR }),
		};
	}

	if (backgroundType === "blur") {
		return {
			type: "blur",
			blurIntensity: getNumberValue({
				value: blurIntensity,
				fallback: DEFAULT_BLUR_INTENSITY,
			}),
		};
	}

	return {
		type: "color",
		color: getStringValue({ value: backgroundColor, fallback: DEFAULT_COLOR }),
	};
}

function getCanvasSizeValue({
	value,
	fallback,
}: {
	value: unknown;
	fallback: { width: number; height: number };
}): { width: number; height: number } {
	if (isRecord(value)) {
		const width = getNumberValue({
			value: value.width,
			fallback: fallback.width,
		});
		const height = getNumberValue({
			value: value.height,
			fallback: fallback.height,
		});

		return { width, height };
	}

	return fallback;
}

function getNumberValue({
	value,
	fallback,
}: {
	value: unknown;
	fallback: number;
}): number {
	return typeof value === "number" ? value : fallback;
}

function getStringValue({
	value,
	fallback,
}: {
	value: unknown;
	fallback?: string;
}): string | undefined {
	if (typeof value === "string") {
		return value;
	}

	return fallback;
}

function normalizeDateString({ value }: { value: unknown }): string {
	if (value instanceof Date) {
		return value.toISOString();
	}

	if (typeof value === "string") {
		return value;
	}

	return new Date().toISOString();
}

function isV2Project({ project }: { project: ProjectRecord }): boolean {
	const versionValue = project.version;
	if (typeof versionValue === "number" && versionValue >= 2) {
		return true;
	}

	return isRecord(project.metadata) && isRecord(project.settings);
}

function isRecord(value: unknown): value is ProjectRecord {
	return typeof value === "object" && value !== null;
}