@susonwaiba/react-media-uploader 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,119 +1,153 @@
1
- import { type Media, MediaTypeEnum } from "@/types/media";
2
- import axios, { type AxiosProgressEvent } from "axios";
3
-
4
- export const imageMimeTypes = [
5
- "image/webp",
6
- "image/gif",
7
- "image/png",
8
- "image/jpeg",
9
- "image/jpg",
10
- ];
11
-
12
- export async function generateMediaType(file: File): Promise<MediaTypeEnum> {
13
- if (imageMimeTypes.includes(file.type)) {
14
- return MediaTypeEnum.IMAGE;
15
- }
16
- if (file.type === "application/pdf") {
17
- return MediaTypeEnum.PDF;
18
- }
19
- return MediaTypeEnum.OTHER;
20
- }
21
-
22
- export async function generateFileHash(
23
- file: File,
24
- algorithm = "sha-1",
25
- ): Promise<string> {
26
- const arrayBuffer = await file.arrayBuffer();
27
- const hashBuffer = await crypto.subtle.digest(algorithm, arrayBuffer);
28
- const hashArray = Array.from(new Uint8Array(hashBuffer));
29
- const hexHash = hashArray
30
- .map((b) => b.toString(16).padStart(2, "0"))
31
- .join("");
32
- return hexHash;
33
- }
34
-
35
- export const defaultHeaders = {
36
- "Content-Type": "application/json",
37
- };
38
-
39
- export interface GenerateUploadUrlProps {
40
- media: Partial<Media>;
41
- }
42
-
43
- export async function generateUploadUrl({ media }: GenerateUploadUrlProps) {
44
- const res = await axios.post("/api/media/generate-upload-url", media, {
45
- headers: defaultHeaders,
46
- });
47
- return res;
48
- }
49
-
50
- export interface UploadToStorageProps {
51
- sasUrl: string;
52
- file: File;
53
- onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
54
- abortController?: AbortController;
55
- }
56
-
57
- export async function uploadToStorage({
58
- sasUrl,
59
- file,
60
- onUploadProgress,
61
- abortController,
62
- }: UploadToStorageProps) {
63
- const res = await axios.put(sasUrl, file, {
64
- headers: {
65
- "x-ms-blob-type": "BlockBlob",
66
- "Content-Type": file.type,
67
- },
68
- onUploadProgress: onUploadProgress,
69
- signal: abortController ? abortController?.signal : undefined,
70
- });
71
- return res;
72
- }
73
-
74
- export interface MarkMediaAsTempProps {
75
- mediaIds: (string | number)[];
76
- }
77
-
78
- export async function markMediaAsTemp({ mediaIds }: MarkMediaAsTempProps) {
79
- const res = await axios.post(
80
- `/api/media/mark-media-as-temp`,
81
- { mediaIds },
82
- {
83
- headers: defaultHeaders,
84
- },
85
- );
86
- return res;
87
- }
88
-
89
- export interface MarkMediaAsActiveProps {
90
- mediaIds: (string | number)[];
91
- }
92
-
93
- export async function markMediaAsActive({ mediaIds }: MarkMediaAsActiveProps) {
94
- const res = await axios.post(
95
- `/api/media/mark-media-as-active`,
96
- { mediaIds },
97
- {
98
- headers: defaultHeaders,
99
- },
100
- );
101
- return res;
102
- }
103
-
104
- export interface MarkMediaAsCanceledProps {
105
- mediaIds: (string | number)[];
106
- }
107
-
108
- export async function markMediaAsCanceled({
109
- mediaIds,
110
- }: MarkMediaAsActiveProps) {
111
- const res = await axios.post(
112
- `/api/media/mark-media-as-canceled`,
113
- [mediaIds],
114
- {
115
- headers: defaultHeaders,
116
- },
117
- );
118
- return res;
119
- }
1
+ import { type Media, MediaTypeEnum } from "@/types/media";
2
+ import axios, { type AxiosProgressEvent } from "axios";
3
+
4
+ export const imageMimeTypes = [
5
+ "image/webp",
6
+ "image/gif",
7
+ "image/png",
8
+ "image/jpeg",
9
+ "image/jpg",
10
+ ];
11
+
12
+ export async function generateMediaType(file: File): Promise<MediaTypeEnum> {
13
+ if (imageMimeTypes.includes(file.type)) {
14
+ return MediaTypeEnum.IMAGE;
15
+ }
16
+ if (file.type === "application/pdf") {
17
+ return MediaTypeEnum.PDF;
18
+ }
19
+ return MediaTypeEnum.OTHER;
20
+ }
21
+
22
+ export async function generateFileHash(
23
+ file: File,
24
+ algorithm = "sha-1",
25
+ ): Promise<string> {
26
+ const arrayBuffer = await file.arrayBuffer();
27
+ const hashBuffer = await crypto.subtle.digest(algorithm, arrayBuffer);
28
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
29
+ const hexHash = hashArray
30
+ .map((b) => b.toString(16).padStart(2, "0"))
31
+ .join("");
32
+ return hexHash;
33
+ }
34
+
35
+ export const defaultHeaders = {
36
+ "Content-Type": "application/json",
37
+ };
38
+
39
+ export interface GenerateUploadUrlProps {
40
+ url?: string;
41
+ additionalHeaders?: Record<string, string>;
42
+ media: Partial<Media>;
43
+ }
44
+
45
+ export async function generateUploadUrl({
46
+ url,
47
+ additionalHeaders,
48
+ media,
49
+ }: GenerateUploadUrlProps) {
50
+ const res = await axios.post(url ?? "/api/media/generate-upload-url", media, {
51
+ headers: {
52
+ ...defaultHeaders,
53
+ ...additionalHeaders,
54
+ },
55
+ });
56
+ return res;
57
+ }
58
+
59
+ export interface UploadToStorageProps {
60
+ uploadUrl: string;
61
+ file: File;
62
+ onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
63
+ abortController?: AbortController;
64
+ }
65
+
66
+ export async function uploadToStorage({
67
+ uploadUrl,
68
+ file,
69
+ onUploadProgress,
70
+ abortController,
71
+ }: UploadToStorageProps) {
72
+ const res = await axios.put(uploadUrl, file, {
73
+ headers: {
74
+ "x-ms-blob-type": "BlockBlob",
75
+ "Content-Type": file.type,
76
+ },
77
+ onUploadProgress: onUploadProgress,
78
+ signal: abortController ? abortController?.signal : undefined,
79
+ });
80
+ return res;
81
+ }
82
+
83
+ export interface MarkMediaAsTempProps {
84
+ url?: string;
85
+ additionalHeaders?: Record<string, string>;
86
+ mediaIds: (string | number)[];
87
+ }
88
+
89
+ export async function markMediaAsTemp({
90
+ url,
91
+ additionalHeaders,
92
+ mediaIds,
93
+ }: MarkMediaAsTempProps) {
94
+ const res = await axios.post(
95
+ url ?? `/api/media/mark-media-as-temp`,
96
+ { mediaIds },
97
+ {
98
+ headers: {
99
+ ...defaultHeaders,
100
+ ...additionalHeaders,
101
+ },
102
+ },
103
+ );
104
+ return res;
105
+ }
106
+
107
+ export interface MarkMediaAsActiveProps {
108
+ url?: string;
109
+ additionalHeaders?: Record<string, string>;
110
+ mediaIds: (string | number)[];
111
+ }
112
+
113
+ export async function markMediaAsActive({
114
+ url,
115
+ additionalHeaders,
116
+ mediaIds,
117
+ }: MarkMediaAsActiveProps) {
118
+ const res = await axios.post(
119
+ url ?? `/api/media/mark-media-as-active`,
120
+ { mediaIds },
121
+ {
122
+ headers: {
123
+ ...defaultHeaders,
124
+ ...additionalHeaders,
125
+ },
126
+ },
127
+ );
128
+ return res;
129
+ }
130
+
131
+ export interface MarkMediaAsCanceledProps {
132
+ url?: string;
133
+ additionalHeaders?: Record<string, string>;
134
+ mediaIds: (string | number)[];
135
+ }
136
+
137
+ export async function markMediaAsCanceled({
138
+ url,
139
+ additionalHeaders,
140
+ mediaIds,
141
+ }: MarkMediaAsActiveProps) {
142
+ const res = await axios.post(
143
+ url ?? `/api/media/mark-media-as-canceled`,
144
+ [mediaIds],
145
+ {
146
+ headers: {
147
+ ...defaultHeaders,
148
+ ...additionalHeaders,
149
+ },
150
+ },
151
+ );
152
+ return res;
153
+ }
@@ -1,47 +1,47 @@
1
- export enum MediaTypeEnum {
2
- IMAGE = "IMAGE",
3
- PDF = "PDF",
4
- DOCS = "DOCS",
5
- OTHER = "OTHER",
6
- }
7
-
8
- export enum MediaStatusEnum {
9
- INIT = "INIT",
10
- TEMP = "TEMP",
11
- ACTIVE = "ACTIVE",
12
- INACTIVE = "INACTIVE",
13
- CANCELED = "CANCELED",
14
- DELETED = "DELETED",
15
- }
16
-
17
- export interface Media {
18
- id: string | number;
19
- type: MediaTypeEnum;
20
- status: MediaStatusEnum;
21
- title: string;
22
- description?: string;
23
- name: string;
24
- dir: string;
25
- path: string;
26
- provider: string;
27
- container?: string;
28
- mimeType?: string;
29
- size?: number;
30
- height?: number;
31
- width?: number;
32
- duration?: number;
33
- tags?: string[];
34
- checksum?: string;
35
- createdAt?: Date | string;
36
- updatedAt?: Date | string;
37
- deletedAt?: Date | string;
38
- }
39
-
40
- export interface MediaItem {
41
- localId: string;
42
- name: string;
43
- multiple?: boolean;
44
- file: File;
45
- tempPreviewUrl?: string;
46
- media: Partial<Media>;
47
- }
1
+ export enum MediaTypeEnum {
2
+ IMAGE = "IMAGE",
3
+ PDF = "PDF",
4
+ DOCS = "DOCS",
5
+ OTHER = "OTHER",
6
+ }
7
+
8
+ export enum MediaStatusEnum {
9
+ INIT = "INIT",
10
+ TEMP = "TEMP",
11
+ ACTIVE = "ACTIVE",
12
+ INACTIVE = "INACTIVE",
13
+ CANCELED = "CANCELED",
14
+ DELETED = "DELETED",
15
+ }
16
+
17
+ export interface Media {
18
+ id: string | number;
19
+ type: MediaTypeEnum;
20
+ status: MediaStatusEnum;
21
+ title: string;
22
+ description?: string;
23
+ name: string;
24
+ dir: string;
25
+ path: string;
26
+ provider: string;
27
+ container?: string;
28
+ mimeType?: string;
29
+ size?: number;
30
+ height?: number;
31
+ width?: number;
32
+ duration?: number;
33
+ tags?: string[];
34
+ checksum?: string;
35
+ createdAt?: Date | string;
36
+ updatedAt?: Date | string;
37
+ deletedAt?: Date | string;
38
+ }
39
+
40
+ export interface MediaItem {
41
+ localId: string;
42
+ name: string;
43
+ multiple?: boolean;
44
+ file: File;
45
+ tempPreviewUrl?: string;
46
+ media: Partial<Media>;
47
+ }
package/tsconfig.json CHANGED
@@ -1,19 +1,19 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "module": "ESNext",
5
- "jsx": "react-jsx",
6
- "declaration": true,
7
- "emitDeclarationOnly": false,
8
- "outDir": "dist",
9
- "rootDir": "src",
10
- "moduleResolution": "Bundler",
11
- "esModuleInterop": true,
12
- "skipLibCheck": true,
13
- "paths": {
14
- "@/*": ["./src/*"]
15
- }
16
- },
17
- "include": ["src"],
18
- "exclude": ["node_modules", "dist"]
19
- }
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "jsx": "react-jsx",
6
+ "declaration": true,
7
+ "emitDeclarationOnly": false,
8
+ "outDir": "dist",
9
+ "rootDir": "src",
10
+ "moduleResolution": "Bundler",
11
+ "esModuleInterop": true,
12
+ "skipLibCheck": true,
13
+ "paths": {
14
+ "@/*": ["./src/*"]
15
+ }
16
+ },
17
+ "include": ["src"],
18
+ "exclude": ["node_modules", "dist"]
19
+ }