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
|
import type { EditorCore } from "@/core";
import { toast } from "sonner";
import type { MediaAsset } from "@/media/types";
import { storageService } from "@/services/storage/service";
import { generateUUID } from "@/utils/id";
import { videoCache } from "@/services/video-cache/service";
import { thumbnailCache } from "@/services/thumbnail-cache/service";
import { waveformCache } from "@/services/waveform-cache/service";
import { BatchCommand, RemoveMediaAssetCommand } from "@/commands";
export class MediaManager {
private assets: MediaAsset[] = [];
private isLoading = false;
private listeners = new Set<() => void>();
constructor(private editor: EditorCore) {}
async addMediaAsset({
projectId,
asset,
}: {
projectId: string;
asset: Omit<MediaAsset, "id">;
}): Promise<MediaAsset | null> {
const newAsset: MediaAsset = {
...asset,
id: generateUUID(),
};
this.assets = [...this.assets, newAsset];
this.notify();
try {
await storageService.saveMediaAsset({ projectId, mediaAsset: newAsset });
this.editor.project.ratchetFpsForImportedMedia({
importedAssets: [newAsset],
});
return newAsset;
} catch (error) {
console.error("Failed to save media asset:", error);
this.assets = this.assets.filter((asset) => asset.id !== newAsset.id);
this.notify();
if (storageService.isQuotaExceededError({ error })) {
toast.error("Not enough browser storage", {
description: error instanceof Error ? error.message : undefined,
});
}
return null;
}
}
async renameMediaAsset({
projectId,
id,
name,
}: {
projectId: string;
id: string;
name: string;
}): Promise<void> {
const asset = this.assets.find((asset) => asset.id === id);
if (!asset || asset.name === name) {
return;
}
const previousName = asset.name;
const updatedAsset: MediaAsset = { ...asset, name };
this.assets = this.assets.map((asset) =>
asset.id === id ? updatedAsset : asset,
);
this.notify();
try {
await storageService.saveMediaAsset({
projectId,
mediaAsset: updatedAsset,
});
} catch (error) {
console.error("Failed to rename media asset:", error);
this.assets = this.assets.map((asset) =>
asset.id === id ? { ...asset, name: previousName } : asset,
);
this.notify();
toast.error("Failed to rename media asset", {
description:
error instanceof Error ? error.message : "Please try again",
});
}
}
removeMediaAsset({ projectId, id }: { projectId: string; id: string }): void {
this.removeMediaAssets({ projectId, ids: [id] });
}
removeMediaAssets({
projectId,
ids,
}: {
projectId: string;
ids: string[];
}): void {
const uniqueIds = [...new Set(ids)];
if (uniqueIds.length === 0) {
return;
}
const command =
uniqueIds.length === 1
? new RemoveMediaAssetCommand({
projectId,
assetId: uniqueIds[0],
})
: new BatchCommand(
uniqueIds.map((id) =>
new RemoveMediaAssetCommand({
projectId,
assetId: id,
}),
),
);
this.editor.command.execute({ command });
}
async loadProjectMedia({ projectId }: { projectId: string }): Promise<void> {
this.isLoading = true;
this.notify();
try {
const mediaAssets = await storageService.loadAllMediaAssets({
projectId,
});
this.assets = mediaAssets;
this.notify();
} catch (error) {
console.error("Failed to load media assets:", error);
} finally {
this.isLoading = false;
this.notify();
}
}
async clearProjectMedia({ projectId }: { projectId: string }): Promise<void> {
thumbnailCache.clearAll();
waveformCache.clearAll();
this.assets.forEach((asset) => {
if (asset.url) {
URL.revokeObjectURL(asset.url);
}
if (asset.thumbnailUrl) {
URL.revokeObjectURL(asset.thumbnailUrl);
}
});
const mediaIds = this.assets.map((asset) => asset.id);
this.assets = [];
this.notify();
try {
await Promise.all(
mediaIds.map((id) =>
storageService.deleteMediaAsset({ projectId, id }),
),
);
} catch (error) {
console.error("Failed to clear media assets from storage:", error);
}
}
clearAllAssets(): void {
videoCache.clearAll();
thumbnailCache.clearAll();
waveformCache.clearAll();
this.assets.forEach((asset) => {
if (asset.url) {
URL.revokeObjectURL(asset.url);
}
if (asset.thumbnailUrl) {
URL.revokeObjectURL(asset.thumbnailUrl);
}
});
this.assets = [];
this.notify();
}
getAssets(): MediaAsset[] {
return this.assets;
}
setAssets({ assets }: { assets: MediaAsset[] }): void {
this.assets = assets;
this.notify();
}
isLoadingMedia(): boolean {
return this.isLoading;
}
subscribe(listener: () => void): () => void {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
}
private notify(): void {
this.listeners.forEach((fn) => {
fn();
});
}
}
|