Skip to content

Commit 044bac6

Browse files
committed
new MimeTypeUtil
1 parent 665ad6d commit 044bac6

File tree

9 files changed

+148
-100
lines changed

9 files changed

+148
-100
lines changed

.changeset/real-spoons-pretend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@squarecloud/blob": patch
3+
---
4+
5+
New `MimeTypeUtil` class for handling supported mime types

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ const objectsToDelete = [
8282
await blob.objects.delete(objectsToDelete)
8383
```
8484

85+
### Extras
86+
87+
#### Mime types handling
88+
89+
- _Check supported file types [here](https://docs.squarecloud.app/services/blob#supported-file-types)._
90+
91+
```ts
92+
import { MimeTypeUtil } from "@squarecloud/blob"
93+
94+
// Get a supported mime type from a file extension
95+
console.log(MimeTypeUtil.fromExtension("jpeg")) // "image/jpeg" | Supported
96+
console.log(MimeTypeUtil.fromExtension("json")) // "application/json" | Supported
97+
console.log(MimeTypeUtil.fromExtension("potato")) // "text/plain" | Unsupported, defaults to text/plain
98+
```
99+
85100
## Contributing
86101

87102
Feel free to contribute with suggestions or bug reports at our [GitHub repository](https://github.com/squarecloudofc/sdk-blob-js).

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ export class SquareCloudBlob {
1717

1818
export * from "./types/create";
1919
export * from "./types/list";
20-
export { MimeTypes } from "./utils/mimetype";
20+
export * from "./utils/mimetype";

src/managers/objects.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { SquareCloudBlob } from "..";
22
import { SquareCloudBlobError } from "../structures/error";
33
import type { CreateObjectResponse, CreateObjectType } from "../types/create";
44
import type { ListObjectsResponse } from "../types/list";
5-
import { getMimeTypeFromExtension } from "../utils/mimetype";
5+
import { MimeTypeUtil } from "../utils/mimetype";
66
import { parsePathLike } from "../utils/pathlike";
77
import { assertCreateObjectResponse } from "../validation/assertions/create";
88
import { assertListObjectsResponse } from "../validation/assertions/list";
@@ -41,7 +41,7 @@ export class ObjectsManager {
4141
const file = await parsePathLike(payload.file);
4242
const mimeType =
4343
typeof object.file === "string"
44-
? getMimeTypeFromExtension(object.file.split(".")[1])
44+
? MimeTypeUtil.fromExtension(object.file.split(".")[1])
4545
: object.mimeType;
4646

4747
const formData = new FormData();

src/utils/mimetype.ts

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/utils/mimetype/enum.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export enum MimeTypes {
2+
VIDEO_MP4 = "video/mp4",
3+
VIDEO_MPEG = "video/mpeg",
4+
VIDEO_WEBM = "video/webm",
5+
VIDEO_X_FLV = "video/x-flv",
6+
VIDEO_X_M4V = "video/x-m4v",
7+
IMAGE_JPEG = "image/jpeg",
8+
IMAGE_PNG = "image/png",
9+
IMAGE_APNG = "image/apng",
10+
IMAGE_TIFF = "image/tiff",
11+
IMAGE_GIF = "image/gif",
12+
IMAGE_WEBP = "image/webp",
13+
IMAGE_BMP = "image/bmp",
14+
IMAGE_SVG = "image/svg+xml",
15+
IMAGE_X_ICON = "image/x-icon",
16+
IMAGE_ICO = "image/ico",
17+
IMAGE_CUR = "image/cur",
18+
IMAGE_HEIC = "image/heic",
19+
IMAGE_HEIF = "image/heif",
20+
AUDIO_WAV = "audio/wav",
21+
AUDIO_OGG = "audio/ogg",
22+
AUDIO_OPUS = "audio/opus",
23+
AUDIO_MP4 = "audio/mp4",
24+
AUDIO_MPEG = "audio/mpeg",
25+
AUDIO_AAC = "audio/aac",
26+
TEXT_PLAIN = "text/plain",
27+
TEXT_HTML = "text/html",
28+
TEXT_CSS = "text/css",
29+
TEXT_CSV = "text/csv",
30+
TEXT_X_SQL = "text/x-sql",
31+
APPLICATION_XML = "application/xml",
32+
APPLICATION_SQL = "application/sql",
33+
APPLICATION_X_SQL = "application/x-sql",
34+
APPLICATION_X_SQLITE3 = "application/x-sqlite3",
35+
APPLICATION_X_PKCS12 = "application/x-pkcs12",
36+
APPLICATION_PDF = "application/pdf",
37+
APPLICATION_JSON = "application/json",
38+
APPLICATION_JAVASCRIPT = "application/javascript",
39+
}

src/utils/mimetype/index.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { type MimeType, mimeTypes, mimeTypesWithExtension } from "./mimetypes";
2+
3+
// biome-ignore lint/complexity/noStaticOnlyClass: organization
4+
export class MimeTypeUtil {
5+
/**
6+
* Supported mime types with their extensions
7+
*/
8+
static mimeTypesWithExtension = mimeTypesWithExtension;
9+
/**
10+
* All supported mime types
11+
*/
12+
static mimeTypes = mimeTypes;
13+
14+
/**
15+
* Returns the corresponding MIME type for a given file extension.
16+
*
17+
* @param extension - The file extension to search for.
18+
* @return The MIME type associated with the extension, or "text/plain" if not found.
19+
*
20+
* @example
21+
* ```js
22+
* MimeTypeUtil.fromExtension("jpeg") // "image/jpeg" | Supported
23+
* MimeTypeUtil.fromExtension("json") // "application/json" | Supported
24+
* MimeTypeUtil.fromExtension("potato") // "text/plain" | Unsupported, defaults to text/plain
25+
* ```
26+
*/
27+
static fromExtension(extension: string): MimeType {
28+
const entries = Object.entries(mimeTypesWithExtension);
29+
const mimeType = entries.find(([, extensions]) =>
30+
extensions.includes(extension),
31+
)?.[0];
32+
33+
return (mimeType as MimeType) || "text/plain";
34+
}
35+
}
36+
37+
export * from "./enum";
38+
export { MimeType } from "./mimetypes";

src/utils/mimetype/mimetypes.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export const mimeTypesWithExtension = {
2+
"video/mp4": ["mp4"],
3+
"video/mpeg": ["mpeg"],
4+
"video/webm": ["webm"],
5+
"video/x-flv": ["flv"],
6+
"video/x-m4v": ["m4v"],
7+
"image/jpeg": ["jpg", "jpeg"],
8+
"image/png": ["png"],
9+
"image/apng": ["apng"],
10+
"image/tiff": ["tiff"],
11+
"image/gif": ["gif"],
12+
"image/webp": ["webp"],
13+
"image/bmp": ["bmp"],
14+
"image/svg+xml": ["svg"],
15+
"image/x-icon": ["ico"],
16+
"image/ico": ["ico"],
17+
"image/cur": ["cur"],
18+
"image/heic": ["heic"],
19+
"image/heif": ["heif"],
20+
"audio/wav": ["wav"],
21+
"audio/ogg": ["ogg"],
22+
"audio/opus": ["opus"],
23+
"audio/mp4": ["mp4"],
24+
"audio/mpeg": ["mp3"],
25+
"audio/aac": ["aac"],
26+
"text/plain": ["txt"],
27+
"text/html": ["html"],
28+
"text/css": ["css"],
29+
"text/csv": ["csv"],
30+
"text/x-sql": ["sql"],
31+
"application/xml": ["xml"],
32+
"application/sql": ["sql"],
33+
"application/x-sql": ["sql"],
34+
"application/x-sqlite3": ["sqlite3"],
35+
"application/x-pkcs12": ["pfx"],
36+
"application/pdf": ["pdf"],
37+
"application/json": ["json"],
38+
"application/javascript": ["js"],
39+
};
40+
41+
export const mimeTypes = Object.keys(mimeTypesWithExtension) as [
42+
MimeType,
43+
...MimeType[],
44+
];
45+
46+
export type MimeType = keyof typeof mimeTypesWithExtension;

src/validation/schemas/create.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from "zod";
2-
import { mimeTypes } from "../../utils/mimetype";
2+
import { MimeTypeUtil } from "../../utils/mimetype";
33
import { nameLikeSchema } from "./common";
44

55
export const createObjectSchema = z
@@ -9,7 +9,7 @@ export const createObjectSchema = z
99
/** Use absolute path, Buffer or Blob */
1010
file: z.string().or(z.instanceof(Buffer)),
1111
/** A string representing the MIME type of the file. */
12-
mimeType: z.enum(mimeTypes).optional(),
12+
mimeType: z.enum(MimeTypeUtil.mimeTypes).optional(),
1313
/** A string representing the prefix for the file. */
1414
prefix: nameLikeSchema.optional(),
1515
/** A number indicating the expiration period of the file, ranging from 1 to 365 days. */

0 commit comments

Comments
 (0)