Spaces:
Runtime error

File size: 2,081 Bytes
ad79323
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import path from "node:path";

import {
  allowedCaseFileExtensions,
  allowedCaseFileMimeTypes,
  MAX_CASE_FILE_SIZE_BYTES,
} from "@/lib/constants";

export function getFileExtension(fileName: string) {
  return path.extname(fileName).toLowerCase();
}

export function isAllowedCaseFileType(fileName: string, mimeType?: string | null) {
  const extension = getFileExtension(fileName);

  return (
    allowedCaseFileExtensions.includes(
      extension as (typeof allowedCaseFileExtensions)[number],
    ) ||
    (!!mimeType &&
      allowedCaseFileMimeTypes.includes(
        mimeType as (typeof allowedCaseFileMimeTypes)[number],
      ))
  );
}

export function isPreviewableMimeType(mimeType: string) {
  return (
    mimeType === "application/pdf" ||
    mimeType.startsWith("image/") ||
    mimeType.startsWith("text/")
  );
}

export function assertCaseFileWithinSizeLimit(sizeBytes: number) {
  return sizeBytes <= MAX_CASE_FILE_SIZE_BYTES;
}

export function getSafeMimeType(fileName: string, mimeType?: string | null) {
  if (mimeType && mimeType !== "application/octet-stream") {
    return mimeType;
  }

  switch (getFileExtension(fileName)) {
    case ".pdf":
      return "application/pdf";
    case ".jpg":
    case ".jpeg":
      return "image/jpeg";
    case ".png":
      return "image/png";
    case ".webp":
      return "image/webp";
    case ".gif":
      return "image/gif";
    case ".svg":
      return "image/svg+xml";
    case ".txt":
      return "text/plain";
    case ".csv":
      return "text/csv";
    case ".doc":
      return "application/msword";
    case ".docx":
      return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    case ".xls":
      return "application/vnd.ms-excel";
    case ".xlsx":
      return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    case ".ppt":
      return "application/vnd.ms-powerpoint";
    case ".pptx":
      return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
    default:
      return "application/octet-stream";
  }
}