umwd-components 0.1.784 → 0.1.785

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.
@@ -55,73 +55,58 @@ async function uploadMinioMediaAction(prevState, formData) {
55
55
  severity: "error",
56
56
  };
57
57
  }
58
- // VALIDATE THE FILES
59
- const validatedFields = {
60
- success: true,
61
- };
62
- /* filesSchema.safeParse({
63
- files: validFiles.length === 1 ? validFiles[0] : validFiles,
64
- }); */
65
- if (!validatedFields.success) ;
66
58
  console.log(`Uploading ${validFiles.length} file(s):`);
67
- // Upload each file
68
- const uploadResults = [];
69
- const errors = [];
59
+ // Prepare files data for upload
60
+ const filesData = [];
70
61
  for (const file of validFiles) {
71
- try {
72
- // Convert file to ArrayBuffer first, then to Buffer, then to base64 string
73
- const arrayBuffer = await file.arrayBuffer();
74
- const fileContent = Buffer.from(arrayBuffer).toString("base64");
75
- console.log("Uploading file:", file.name, "Size:", Math.round(fileContent.length / 1024 / 1024), "MB");
76
- // UPLOAD FILE TO MINIO
77
- const fileUploadResponse = reference && refID && field
78
- ? await minioUploadService(bucketName, file.name, fileContent, reference, refID, field)
79
- : await minioUploadService(bucketName, file.name, fileContent);
80
- if (!fileUploadResponse) {
81
- errors.push(`Failed to upload ${file.name}`);
82
- continue;
83
- }
84
- if (fileUploadResponse.error) {
85
- errors.push(`Failed to upload ${file.name}: ${fileUploadResponse.error.message || 'Unknown error'}`);
86
- continue;
87
- }
88
- const flattenedData = flattenAttributes(fileUploadResponse);
89
- uploadResults.push({
90
- fileName: file.name,
91
- data: flattenedData
92
- });
93
- }
94
- catch (fileError) {
95
- console.error(`Error uploading ${file.name}:`, fileError);
96
- errors.push(`Failed to process ${file.name}`);
97
- }
62
+ const arrayBuffer = await file.arrayBuffer();
63
+ const fileContent = Buffer.from(arrayBuffer).toString("base64");
64
+ filesData.push({
65
+ fileName: file.name,
66
+ fileContent: fileContent,
67
+ });
68
+ console.log("Prepared file:", file.name, "Size:", Math.round(fileContent.length / 1024 / 1024), "MB");
69
+ }
70
+ // UPLOAD ALL FILES TO MINIO
71
+ const fileUploadResponse = await minioUploadService(bucketName, filesData, reference, refID, field);
72
+ if (!fileUploadResponse) {
73
+ return {
74
+ ...prevState,
75
+ strapiErrors: null,
76
+ zodErrors: null,
77
+ message: "Failed to upload files",
78
+ severity: "error",
79
+ };
98
80
  }
99
- // Check results
100
- if (uploadResults.length === 0) {
81
+ if (fileUploadResponse.error) {
101
82
  return {
102
83
  ...prevState,
103
- strapiErrors: errors.length > 0 ? { message: errors.join(', ') } : null,
84
+ strapiErrors: fileUploadResponse.error,
104
85
  zodErrors: null,
105
- message: "Failed to upload any files",
86
+ message: "Failed to upload files",
106
87
  severity: "error",
107
88
  };
108
89
  }
109
- if (errors.length > 0 && uploadResults.length > 0) {
90
+ // Handle partial failures
91
+ if (fileUploadResponse.errors && fileUploadResponse.errors.length > 0) {
92
+ const successCount = fileUploadResponse.uploadResults?.length || 0;
93
+ const errorCount = fileUploadResponse.errors.length;
110
94
  return {
111
95
  ...prevState,
112
- data: uploadResults,
96
+ data: fileUploadResponse.uploadResults,
113
97
  zodErrors: null,
114
98
  strapiErrors: null,
115
- message: `Uploaded ${uploadResults.length} file(s) successfully. ${errors.length} file(s) failed: ${errors.join(', ')}`,
116
- severity: "warning",
99
+ message: `Uploaded ${successCount} file(s) successfully. ${errorCount} file(s) failed.`,
100
+ severity: successCount > 0 ? "warning" : "error",
117
101
  };
118
102
  }
103
+ const flattenedData = flattenAttributes(fileUploadResponse);
119
104
  return {
120
105
  ...prevState,
121
- data: uploadResults,
106
+ data: flattenedData,
122
107
  zodErrors: null,
123
108
  strapiErrors: null,
124
- message: `Successfully uploaded ${uploadResults.length} file(s)`,
109
+ message: `Successfully uploaded ${validFiles.length} file(s)`,
125
110
  severity: "success",
126
111
  };
127
112
  }
@@ -8,7 +8,7 @@
8
8
  import { getAuthToken } from '../../../get-token.js';
9
9
  import { getStrapiURL } from '../../../../../lib/utils.js';
10
10
 
11
- async function minioUploadService(bucketName, fileName, fileContent, reference, refID, field) {
11
+ async function minioUploadService(bucketName, files, reference, refID, field) {
12
12
  const authToken = await getAuthToken();
13
13
  if (!authToken)
14
14
  throw new Error("No auth token found");
@@ -16,13 +16,15 @@ async function minioUploadService(bucketName, fileName, fileContent, reference,
16
16
  const url = new URL("/api/minio-plugin/uploadbyreference", baseUrl);
17
17
  try {
18
18
  const response = await fetch(url, {
19
- headers: { Authorization: `Bearer ${authToken}` },
19
+ headers: {
20
+ Authorization: `Bearer ${authToken}`,
21
+ "Content-Type": "application/json",
22
+ },
20
23
  method: "POST",
21
24
  body: JSON.stringify({
22
25
  data: {
23
26
  bucketName,
24
- fileName,
25
- fileContent,
27
+ files,
26
28
  reference,
27
29
  refID,
28
30
  field,
@@ -33,7 +35,7 @@ async function minioUploadService(bucketName, fileName, fileContent, reference,
33
35
  return dataResponse;
34
36
  }
35
37
  catch (error) {
36
- console.error("Error uploading image:", error);
38
+ console.error("Error uploading files:", error);
37
39
  throw error;
38
40
  }
39
41
  }