Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
1import generateUUID from "@hey/helpers/generateUUID";
2import type { NewAttachment } from "@hey/types/misc";
3import { toast } from "sonner";
4import compressImage from "./compressImage";
5
6const BYTES_IN_MB = 1_000_000;
7const IMAGE_UPLOAD_LIMIT = 50000000;
8const VIDEO_UPLOAD_LIMIT = 2000000000;
9const AUDIO_UPLOAD_LIMIT = 600000000;
10
11export const validateFileSize = (file: File): boolean => {
12 const isImage = file.type.includes("image");
13 const isVideo = file.type.includes("video");
14 const isAudio = file.type.includes("audio");
15
16 if (isImage && file.size > IMAGE_UPLOAD_LIMIT) {
17 toast.error(
18 `Image size should be less than ${IMAGE_UPLOAD_LIMIT / BYTES_IN_MB}MB`
19 );
20 return false;
21 }
22
23 if (isVideo && file.size > VIDEO_UPLOAD_LIMIT) {
24 toast.error(
25 `Video size should be less than ${VIDEO_UPLOAD_LIMIT / BYTES_IN_MB}MB`
26 );
27 return false;
28 }
29
30 if (isAudio && file.size > AUDIO_UPLOAD_LIMIT) {
31 toast.error(
32 `Audio size should be less than ${AUDIO_UPLOAD_LIMIT / BYTES_IN_MB}MB`
33 );
34 return false;
35 }
36
37 return true;
38};
39
40export const compressFiles = async (files: File[]): Promise<File[]> => {
41 return Promise.all(
42 files.map(async (file) => {
43 if (file.type.includes("image") && !file.type.includes("gif")) {
44 return await compressImage(file, {
45 maxSizeMB: 9,
46 maxWidthOrHeight: 6000
47 });
48 }
49 return file;
50 })
51 );
52};
53
54export const createPreviewAttachments = (files: File[]): NewAttachment[] => {
55 return files.map((file) => ({
56 file,
57 id: generateUUID(),
58 mimeType: file.type,
59 previewUri: URL.createObjectURL(file),
60 type: file.type.includes("image")
61 ? "Image"
62 : file.type.includes("video")
63 ? "Video"
64 : "Audio"
65 }));
66};