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
|
import { toast } from "sonner";
import { getMediaTypeFromFile } from "@/media/media-utils";
import { formatStorageBytes } from "@/services/storage/quota";
import { storageService } from "@/services/storage/service";
import type { MediaAsset } from "@/media/types";
import { readVideoFile } from "./mediabunny";
import type { VideoFileData } from "./mediabunny";
import { renderThumbnailDataUrl } from "./thumbnail";
export interface ProcessedMediaAsset extends Omit<MediaAsset, "id"> {}
const getUnsupportedVideoDescription = ({
codec,
}: {
codec: VideoFileData["codec"];
}): string => {
const codecLabel = codec ? codec.toUpperCase() : "this video codec";
return codec === "hevc"
? `${codecLabel} cannot be decoded in this browser, so this clip may not preview correctly. Convert it to H.264 MP4 or try importing it in Safari.`
: `${codecLabel} cannot be decoded in this browser, so this clip may not preview correctly. Convert it to H.264 MP4 and reimport it.`;
};
const getStorageLimitDescription = ({
fileSize,
availableBytes,
}: {
fileSize: number;
availableBytes: number | null;
}): string => {
const fileSizeLabel = formatStorageBytes({ bytes: fileSize });
if (availableBytes === null) {
return `File size is ${fileSizeLabel}.`;
}
return `File size is ${fileSizeLabel}, but only ${formatStorageBytes({
bytes: availableBytes,
})} is safely available in browser storage.`;
};
export async function generateImageThumbnail({
imageFile,
}: {
imageFile: File;
}): Promise<{ thumbnailUrl: string; width: number; height: number }> {
return new Promise((resolve, reject) => {
const image = new window.Image();
const objectUrl = URL.createObjectURL(imageFile);
image.addEventListener("load", () => {
try {
const thumbnailUrl = renderThumbnailDataUrl({
width: image.naturalWidth,
height: image.naturalHeight,
draw: ({ context, width, height }) => {
context.drawImage(image, 0, 0, width, height);
},
});
resolve({
thumbnailUrl,
width: image.naturalWidth,
height: image.naturalHeight,
});
} catch (error) {
reject(
error instanceof Error ? error : new Error("Could not render image"),
);
} finally {
URL.revokeObjectURL(objectUrl);
image.remove();
}
});
image.addEventListener("error", () => {
URL.revokeObjectURL(objectUrl);
image.remove();
reject(new Error("Could not load image"));
});
image.src = objectUrl;
});
}
export async function processMediaAssets({
files,
onProgress,
}: {
files: FileList | File[];
onProgress?: ({ progress }: { progress: number }) => void;
}): Promise<ProcessedMediaAsset[]> {
const fileArray = Array.from(files);
const processedAssets: ProcessedMediaAsset[] = [];
const total = fileArray.length;
let completed = 0;
for (const file of fileArray) {
const fileType = getMediaTypeFromFile({ file });
if (!fileType) {
toast.error(`Unsupported file type: ${file.name}`);
continue;
}
const storageCheck = await storageService.canStoreFile({
size: file.size,
});
if (!storageCheck.canStore) {
toast.error(`Not enough browser storage for ${file.name}`, {
description: getStorageLimitDescription({
fileSize: file.size,
availableBytes: storageCheck.availableBytes,
}),
});
continue;
}
const url = URL.createObjectURL(file);
let thumbnailUrl: string | undefined;
let duration: number | undefined;
let width: number | undefined;
let height: number | undefined;
let fps: number | undefined;
let hasAudio: boolean | undefined;
try {
if (fileType === "image") {
const result = await generateImageThumbnail({ imageFile: file });
thumbnailUrl = result.thumbnailUrl;
width = result.width;
height = result.height;
} else if (fileType === "video") {
try {
const videoData = await readVideoFile({ file });
duration = videoData.duration;
width = videoData.width;
height = videoData.height;
fps = Number.isFinite(videoData.fps)
? Math.round(videoData.fps)
: undefined;
hasAudio = videoData.hasAudio;
thumbnailUrl = videoData.thumbnailUrl ?? undefined;
if (!videoData.canDecode) {
toast.error(`Can't preview ${file.name}`, {
description: getUnsupportedVideoDescription({
codec: videoData.codec,
}),
});
}
} catch (error) {
const message =
error instanceof Error
? error.message
: "Could not process video";
toast.error(`Couldn't process ${file.name}`, {
description: message,
});
}
} else if (fileType === "audio") {
duration = await getMediaDuration({ file });
}
processedAssets.push({
name: file.name,
type: fileType,
file,
url,
thumbnailUrl,
duration,
width,
height,
fps,
hasAudio,
});
await new Promise((resolve) => setTimeout(resolve, 0));
completed += 1;
if (onProgress) {
const percent = Math.round((completed / total) * 100);
onProgress({ progress: percent });
}
} catch (error) {
console.error("Error processing file:", file.name, error);
toast.error(`Failed to process ${file.name}`);
URL.revokeObjectURL(url);
}
}
return processedAssets;
}
const getMediaDuration = ({ file }: { file: File }): Promise<number> => {
return new Promise((resolve, reject) => {
const element = document.createElement(
file.type.startsWith("video/") ? "video" : "audio",
) as HTMLVideoElement;
const objectUrl = URL.createObjectURL(file);
element.addEventListener("loadedmetadata", () => {
resolve(element.duration);
URL.revokeObjectURL(objectUrl);
element.remove();
});
element.addEventListener("error", () => {
reject(new Error("Could not load media"));
URL.revokeObjectURL(objectUrl);
element.remove();
});
element.src = objectUrl;
element.load();
});
};
|