forked from CapSoftware/Cap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportMedia.ts
More file actions
97 lines (88 loc) · 2.18 KB
/
Copy pathimportMedia.ts
File metadata and controls
97 lines (88 loc) · 2.18 KB
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
import { invoke } from "@tauri-apps/api/core";
import * as dialog from "@tauri-apps/plugin-dialog";
import { hideCurrentWindow } from "~/utils/hide-window";
import { commands } from "~/utils/tauri";
const videoExtensions = [
"mp4",
"mov",
"avi",
"mkv",
"webm",
"wmv",
"m4v",
"flv",
];
const imageExtensions = [
"png",
"jpg",
"jpeg",
"webp",
"gif",
"bmp",
"tif",
"tiff",
];
type ImportOptions = {
hideCurrentWindow?: boolean;
};
const selectedPath = (result: string | string[] | null) =>
typeof result === "string" ? result : null;
const maybeHideCurrentWindow = async (options?: ImportOptions) => {
if (options?.hideCurrentWindow) await hideCurrentWindow();
};
export const importVideoPath = async (
sourcePath: string,
options?: ImportOptions,
) => {
const projectPath = await commands.startVideoImport(sourcePath);
await commands.showWindow({ Editor: { project_path: projectPath } });
await maybeHideCurrentWindow(options);
return projectPath;
};
export const importImagePath = async (
sourcePath: string,
options?: ImportOptions,
) => {
const imagePath = await invoke<string>("start_image_import", { sourcePath });
await commands.showWindow({ ScreenshotEditor: { path: imagePath } });
await maybeHideCurrentWindow(options);
return imagePath;
};
export const importVideoFromPicker = async (options?: ImportOptions) => {
const result = await dialog.open({
filters: [
{
name: "Video Files",
extensions: videoExtensions,
},
],
multiple: false,
});
const path = selectedPath(result);
if (!path) return null;
return await importVideoPath(path, options);
};
export const importImageFromPicker = async (options?: ImportOptions) => {
const result = await dialog.open({
filters: [
{
name: "Image Files",
extensions: imageExtensions,
},
],
multiple: false,
});
const path = selectedPath(result);
if (!path) return null;
return await importImagePath(path, options);
};
export const showImportError = async (
mediaType: "video" | "image",
error: unknown,
) => {
const message = error instanceof Error ? error.message : String(error);
await dialog.message(`Failed to import ${mediaType}: ${message}`, {
title: "Import Error",
kind: "error",
});
};