@elqnt/react 1.0.2 → 1.0.4

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,10 +1,149 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});"use server";
1
+ "use client";
2
+ "use strict";
3
+ "use server";
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
2
21
 
3
-
4
-
5
- var _chunkW4AIK44Njs = require('../../chunk-W4AIK44N.js');
6
-
7
-
8
-
9
- exports.uploadFile = _chunkW4AIK44Njs.uploadFile; exports.uploadToS3 = _chunkW4AIK44Njs.uploadToS3;
22
+ // components/upload/upload-actions.ts
23
+ var upload_actions_exports = {};
24
+ __export(upload_actions_exports, {
25
+ uploadFile: () => uploadFile,
26
+ uploadToS3: () => uploadToS3
27
+ });
28
+ module.exports = __toCommonJS(upload_actions_exports);
29
+ var import_storage_blob = require("@azure/storage-blob");
30
+ var import_client_s3 = require("@aws-sdk/client-s3");
31
+ async function uploadFile(formData) {
32
+ try {
33
+ const file = formData.get("file");
34
+ if (!file) {
35
+ throw new Error("No file provided");
36
+ }
37
+ const STORAGE_CONNECTION_STRING = process.env.AZURE_STORAGE_CONNECTION_STRING;
38
+ const STORAGE_CONTAINER_NAME = process.env.AZURE_STORAGE_CONTAINER_NAME;
39
+ if (!STORAGE_CONNECTION_STRING || !STORAGE_CONTAINER_NAME) {
40
+ throw new Error(
41
+ "STORAGE_CONNECTION_STRING or STORAGE_CONTAINER_NAME not set"
42
+ );
43
+ }
44
+ const fileExtension = file.name.split(".").pop()?.toLowerCase();
45
+ const fileName = `${Date.now()}-${Math.random().toString(36).substring(2)}.${fileExtension}`;
46
+ const contentType = getContentType(fileExtension);
47
+ const buffer = Buffer.from(await file.arrayBuffer());
48
+ const blobServiceClient = import_storage_blob.BlobServiceClient.fromConnectionString(
49
+ STORAGE_CONNECTION_STRING
50
+ );
51
+ const containerClient = blobServiceClient.getContainerClient(
52
+ STORAGE_CONTAINER_NAME
53
+ );
54
+ await containerClient.createIfNotExists();
55
+ const blockBlobClient = containerClient.getBlockBlobClient(fileName);
56
+ await blockBlobClient.upload(buffer, buffer.length, {
57
+ blobHTTPHeaders: {
58
+ blobContentType: contentType
59
+ }
60
+ });
61
+ const fileUrl = blockBlobClient.url;
62
+ return { success: true, fileUrl };
63
+ } catch (error) {
64
+ console.error("Upload error:", error);
65
+ return {
66
+ success: false,
67
+ error: error instanceof Error ? error.message : "Failed to upload file"
68
+ };
69
+ }
70
+ }
71
+ function getContentType(fileExtension) {
72
+ if (!fileExtension) return "application/octet-stream";
73
+ const mimeTypes = {
74
+ // Images
75
+ png: "image/png",
76
+ jpg: "image/jpeg",
77
+ jpeg: "image/jpeg",
78
+ gif: "image/gif",
79
+ webp: "image/webp",
80
+ svg: "image/svg+xml",
81
+ // Documents
82
+ pdf: "application/pdf",
83
+ doc: "application/msword",
84
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
85
+ // Others
86
+ txt: "text/plain",
87
+ csv: "text/csv",
88
+ json: "application/json"
89
+ };
90
+ return mimeTypes[fileExtension] || "application/octet-stream";
91
+ }
92
+ async function uploadToS3(formData) {
93
+ try {
94
+ const file = formData.get("file");
95
+ if (!file) {
96
+ throw new Error("No file provided");
97
+ }
98
+ const AWS_ACCESS_KEY_ID = process.env.LINODE_ACCESS_KEY;
99
+ const AWS_SECRET_ACCESS_KEY = process.env.LINODE_SECRET_KEY;
100
+ const S3_BUCKET_NAME = process.env.LINODE_BUCKET_NAME;
101
+ let S3_ENDPOINT = process.env.LINODE_OBJECT_STORAGE_ENDPOINT;
102
+ if (!AWS_ACCESS_KEY_ID || !AWS_SECRET_ACCESS_KEY || !S3_BUCKET_NAME || !S3_ENDPOINT) {
103
+ throw new Error("S3 credentials or configuration not set");
104
+ }
105
+ const fileExtension = file.name.split(".").pop()?.toLowerCase();
106
+ const fileName = `${Date.now()}-${Math.random().toString(36).substring(2)}.${fileExtension}`;
107
+ const contentType = getContentType(fileExtension);
108
+ const buffer = Buffer.from(await file.arrayBuffer());
109
+ const s3Client = new import_client_s3.S3Client({
110
+ endpoint: S3_ENDPOINT,
111
+ region: "us-east-1",
112
+ // Linode requires a region but it's not used with custom endpoint
113
+ credentials: {
114
+ accessKeyId: AWS_ACCESS_KEY_ID,
115
+ secretAccessKey: AWS_SECRET_ACCESS_KEY
116
+ },
117
+ forcePathStyle: true,
118
+ // Required for Linode Object Storage
119
+ maxAttempts: 3,
120
+ requestHandler: {
121
+ timeout: 1e4
122
+ // 10 seconds timeout
123
+ }
124
+ });
125
+ const command = new import_client_s3.PutObjectCommand({
126
+ Bucket: S3_BUCKET_NAME,
127
+ Key: fileName,
128
+ Body: buffer,
129
+ ContentType: contentType,
130
+ ACL: "public-read"
131
+ // Make the object publicly readable
132
+ });
133
+ await s3Client.send(command);
134
+ const fileUrl = `${S3_ENDPOINT}/${S3_BUCKET_NAME}/${fileName}`;
135
+ return { success: true, fileUrl };
136
+ } catch (error) {
137
+ console.error("Upload error:", JSON.stringify(error, null, 2));
138
+ return {
139
+ success: false,
140
+ error: error instanceof Error ? error.message : "Failed to upload file"
141
+ };
142
+ }
143
+ }
144
+ // Annotate the CommonJS export names for ESM import in node:
145
+ 0 && (module.exports = {
146
+ uploadFile,
147
+ uploadToS3
148
+ });
10
149
  //# sourceMappingURL=upload-actions.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["/home/runner/work/eloquent-packages/eloquent-packages/packages/react/dist/components/upload/upload-actions.js"],"names":[],"mappings":"AAAA,qFAAY;AACZ;AACE;AACA;AACF,0DAAgC;AAChC;AACE;AACA;AACF,mGAAC","file":"/home/runner/work/eloquent-packages/eloquent-packages/packages/react/dist/components/upload/upload-actions.js"}
1
+ {"version":3,"sources":["../../../components/upload/upload-actions.ts"],"sourcesContent":["\"use server\";\nimport { BlobServiceClient } from \"@azure/storage-blob\";\nimport { PutObjectCommand, S3Client } from \"@aws-sdk/client-s3\";\n\n\nexport async function uploadFile(formData: FormData) {\n try {\n const file = formData.get(\"file\") as File;\n if (!file) {\n throw new Error(\"No file provided\");\n }\n\n const STORAGE_CONNECTION_STRING =\n process.env.AZURE_STORAGE_CONNECTION_STRING;\n const STORAGE_CONTAINER_NAME = process.env.AZURE_STORAGE_CONTAINER_NAME;\n\n if (!STORAGE_CONNECTION_STRING || !STORAGE_CONTAINER_NAME) {\n throw new Error(\n \"STORAGE_CONNECTION_STRING or STORAGE_CONTAINER_NAME not set\"\n );\n }\n\n // Generate unique filename\n const fileExtension = file.name.split(\".\").pop()?.toLowerCase();\n const fileName = `${Date.now()}-${Math.random().toString(36).substring(2)}.${fileExtension}`;\n\n // Determine content type based on file extension\n const contentType = getContentType(fileExtension);\n\n // Convert file to buffer\n const buffer = Buffer.from(await file.arrayBuffer());\n\n // Upload to Azure Blob Storage\n const blobServiceClient = BlobServiceClient.fromConnectionString(\n STORAGE_CONNECTION_STRING\n );\n const containerClient = blobServiceClient.getContainerClient(\n STORAGE_CONTAINER_NAME\n );\n\n // Create container if it doesn't exist\n await containerClient.createIfNotExists();\n\n // Upload file with content type\n const blockBlobClient = containerClient.getBlockBlobClient(fileName);\n await blockBlobClient.upload(buffer, buffer.length, {\n blobHTTPHeaders: {\n blobContentType: contentType,\n },\n });\n\n // Get the URL\n const fileUrl = blockBlobClient.url;\n\n return { success: true, fileUrl };\n } catch (error) {\n console.error(\"Upload error:\", error);\n return {\n success: false,\n error: error instanceof Error ? error.message : \"Failed to upload file\",\n };\n }\n}\n\nfunction getContentType(fileExtension: string | undefined): string {\n if (!fileExtension) return \"application/octet-stream\";\n\n const mimeTypes: Record<string, string> = {\n // Images\n png: \"image/png\",\n jpg: \"image/jpeg\",\n jpeg: \"image/jpeg\",\n gif: \"image/gif\",\n webp: \"image/webp\",\n svg: \"image/svg+xml\",\n // Documents\n pdf: \"application/pdf\",\n doc: \"application/msword\",\n docx: \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\",\n // Others\n txt: \"text/plain\",\n csv: \"text/csv\",\n json: \"application/json\",\n };\n\n return mimeTypes[fileExtension] || \"application/octet-stream\";\n}\n\nexport async function uploadToS3(formData: FormData) {\n try {\n const file = formData.get(\"file\") as File;\n if (!file) {\n throw new Error(\"No file provided\");\n }\n\n const AWS_ACCESS_KEY_ID = process.env.LINODE_ACCESS_KEY;\n const AWS_SECRET_ACCESS_KEY = process.env.LINODE_SECRET_KEY;\n const S3_BUCKET_NAME = process.env.LINODE_BUCKET_NAME;\n let S3_ENDPOINT = process.env.LINODE_OBJECT_STORAGE_ENDPOINT;\n\n if (\n !AWS_ACCESS_KEY_ID ||\n !AWS_SECRET_ACCESS_KEY ||\n !S3_BUCKET_NAME ||\n !S3_ENDPOINT\n ) {\n throw new Error(\"S3 credentials or configuration not set\");\n }\n\n // Generate unique filename\n const fileExtension = file.name.split(\".\").pop()?.toLowerCase();\n const fileName = `${Date.now()}-${Math.random().toString(36).substring(2)}.${fileExtension}`;\n\n // Determine content type based on file extension\n const contentType = getContentType(fileExtension);\n\n // Convert file to buffer\n const buffer = Buffer.from(await file.arrayBuffer());\n\n // Initialize S3 client\n const s3Client = new S3Client({\n endpoint: S3_ENDPOINT,\n region: \"us-east-1\", // Linode requires a region but it's not used with custom endpoint\n credentials: {\n accessKeyId: AWS_ACCESS_KEY_ID,\n secretAccessKey: AWS_SECRET_ACCESS_KEY,\n },\n forcePathStyle: true, // Required for Linode Object Storage\n maxAttempts: 3,\n requestHandler: {\n timeout: 10000, // 10 seconds timeout\n },\n });\n\n // Upload to S3\n const command = new PutObjectCommand({\n Bucket: S3_BUCKET_NAME,\n Key: fileName,\n Body: buffer,\n ContentType: contentType,\n ACL: \"public-read\", // Make the object publicly readable\n });\n\n await s3Client.send(command);\n\n // Construct the URL using the endpoint\n const fileUrl = `${S3_ENDPOINT}/${S3_BUCKET_NAME}/${fileName}`;\n\n return { success: true, fileUrl };\n } catch (error) {\n console.error(\"Upload error:\", JSON.stringify(error, null, 2));\n return {\n success: false,\n error: error instanceof Error ? error.message : \"Failed to upload file\",\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,0BAAkC;AAClC,uBAA2C;AAG3C,eAAsB,WAAW,UAAoB;AACnD,MAAI;AACF,UAAM,OAAO,SAAS,IAAI,MAAM;AAChC,QAAI,CAAC,MAAM;AACT,YAAM,IAAI,MAAM,kBAAkB;AAAA,IACpC;AAEA,UAAM,4BACJ,QAAQ,IAAI;AACd,UAAM,yBAAyB,QAAQ,IAAI;AAE3C,QAAI,CAAC,6BAA6B,CAAC,wBAAwB;AACzD,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAGA,UAAM,gBAAgB,KAAK,KAAK,MAAM,GAAG,EAAE,IAAI,GAAG,YAAY;AAC9D,UAAM,WAAW,GAAG,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,UAAU,CAAC,CAAC,IAAI,aAAa;AAG1F,UAAM,cAAc,eAAe,aAAa;AAGhD,UAAM,SAAS,OAAO,KAAK,MAAM,KAAK,YAAY,CAAC;AAGnD,UAAM,oBAAoB,sCAAkB;AAAA,MAC1C;AAAA,IACF;AACA,UAAM,kBAAkB,kBAAkB;AAAA,MACxC;AAA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GAAG,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,UAAU,CAAC,CAAC,IAAI,aAAa;AAG1F,UAAM,cAAc,eAAe,aAAa;AAGhD,UAAM,SAAS,OAAO,KAAK,MAAM,KAAK,YAAY,CAAC;AAGnD,UAAM,WAAW,IAAI,0BAAS;AAAA,MAC5B,UAAU;AAAA,MACV,QAAQ;AAAA;AAAA,MACR,aAAa;AAAA,QACX,aAAa;AAAA,QACb,iBAAiB;AAAA,MACnB;AAAA,MACA,gBAAgB;AAAA;AAAA,MAChB,aAAa;AAAA,MACb,gBAAgB;AAAA,QACd,SAAS;AAAA;AAAA,MACX;AAAA,IACF,CAAC;AAGD,UAAM,UAAU,IAAI,kCAAiB;AAAA,MACnC,QAAQ;AAAA,MACR,KAAK;AAAA,MACL,MAAM;AAAA,MACN,aAAa;AAAA,MACb,KAAK;AAAA;AAAA,IACP,CAAC;AAED,UAAM,SAAS,KAAK,OAAO;AAG3B,UAAM,UAAU,GAAG,WAAW,IAAI,cAAc,IAAI,QAAQ;AAE5D,WAAO,EAAE,SAAS,MAAM,QAAQ;AAAA,EAClC,SAAS,OAAO;AACd,YAAQ,MAAM,iBAAiB,KAAK,UAAU,OAAO,MAAM,CAAC,CAAC;AAC7D,WAAO;AAAA,MACL,SAAS;AAAA,MACT,OAAO,iBAAiB,QAAQ,MAAM,UAAU;AAAA,IAClD;AAAA,EACF;AACF;","names":[]}
@@ -1,8 +1,122 @@
1
+ "use client";
1
2
  "use server";
2
- import {
3
- uploadFile,
4
- uploadToS3
5
- } from "../../chunk-UXJQ7CBV.mjs";
3
+
4
+ // components/upload/upload-actions.ts
5
+ import { BlobServiceClient } from "@azure/storage-blob";
6
+ import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
7
+ async function uploadFile(formData) {
8
+ try {
9
+ const file = formData.get("file");
10
+ if (!file) {
11
+ throw new Error("No file provided");
12
+ }
13
+ const STORAGE_CONNECTION_STRING = process.env.AZURE_STORAGE_CONNECTION_STRING;
14
+ const STORAGE_CONTAINER_NAME = process.env.AZURE_STORAGE_CONTAINER_NAME;
15
+ if (!STORAGE_CONNECTION_STRING || !STORAGE_CONTAINER_NAME) {
16
+ throw new Error(
17
+ "STORAGE_CONNECTION_STRING or STORAGE_CONTAINER_NAME not set"
18
+ );
19
+ }
20
+ const fileExtension = file.name.split(".").pop()?.toLowerCase();
21
+ const fileName = `${Date.now()}-${Math.random().toString(36).substring(2)}.${fileExtension}`;
22
+ const contentType = getContentType(fileExtension);
23
+ const buffer = Buffer.from(await file.arrayBuffer());
24
+ const blobServiceClient = BlobServiceClient.fromConnectionString(
25
+ STORAGE_CONNECTION_STRING
26
+ );
27
+ const containerClient = blobServiceClient.getContainerClient(
28
+ STORAGE_CONTAINER_NAME
29
+ );
30
+ await containerClient.createIfNotExists();
31
+ const blockBlobClient = containerClient.getBlockBlobClient(fileName);
32
+ await blockBlobClient.upload(buffer, buffer.length, {
33
+ blobHTTPHeaders: {
34
+ blobContentType: contentType
35
+ }
36
+ });
37
+ const fileUrl = blockBlobClient.url;
38
+ return { success: true, fileUrl };
39
+ } catch (error) {
40
+ console.error("Upload error:", error);
41
+ return {
42
+ success: false,
43
+ error: error instanceof Error ? error.message : "Failed to upload file"
44
+ };
45
+ }
46
+ }
47
+ function getContentType(fileExtension) {
48
+ if (!fileExtension) return "application/octet-stream";
49
+ const mimeTypes = {
50
+ // Images
51
+ png: "image/png",
52
+ jpg: "image/jpeg",
53
+ jpeg: "image/jpeg",
54
+ gif: "image/gif",
55
+ webp: "image/webp",
56
+ svg: "image/svg+xml",
57
+ // Documents
58
+ pdf: "application/pdf",
59
+ doc: "application/msword",
60
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
61
+ // Others
62
+ txt: "text/plain",
63
+ csv: "text/csv",
64
+ json: "application/json"
65
+ };
66
+ return mimeTypes[fileExtension] || "application/octet-stream";
67
+ }
68
+ async function uploadToS3(formData) {
69
+ try {
70
+ const file = formData.get("file");
71
+ if (!file) {
72
+ throw new Error("No file provided");
73
+ }
74
+ const AWS_ACCESS_KEY_ID = process.env.LINODE_ACCESS_KEY;
75
+ const AWS_SECRET_ACCESS_KEY = process.env.LINODE_SECRET_KEY;
76
+ const S3_BUCKET_NAME = process.env.LINODE_BUCKET_NAME;
77
+ let S3_ENDPOINT = process.env.LINODE_OBJECT_STORAGE_ENDPOINT;
78
+ if (!AWS_ACCESS_KEY_ID || !AWS_SECRET_ACCESS_KEY || !S3_BUCKET_NAME || !S3_ENDPOINT) {
79
+ throw new Error("S3 credentials or configuration not set");
80
+ }
81
+ const fileExtension = file.name.split(".").pop()?.toLowerCase();
82
+ const fileName = `${Date.now()}-${Math.random().toString(36).substring(2)}.${fileExtension}`;
83
+ const contentType = getContentType(fileExtension);
84
+ const buffer = Buffer.from(await file.arrayBuffer());
85
+ const s3Client = new S3Client({
86
+ endpoint: S3_ENDPOINT,
87
+ region: "us-east-1",
88
+ // Linode requires a region but it's not used with custom endpoint
89
+ credentials: {
90
+ accessKeyId: AWS_ACCESS_KEY_ID,
91
+ secretAccessKey: AWS_SECRET_ACCESS_KEY
92
+ },
93
+ forcePathStyle: true,
94
+ // Required for Linode Object Storage
95
+ maxAttempts: 3,
96
+ requestHandler: {
97
+ timeout: 1e4
98
+ // 10 seconds timeout
99
+ }
100
+ });
101
+ const command = new PutObjectCommand({
102
+ Bucket: S3_BUCKET_NAME,
103
+ Key: fileName,
104
+ Body: buffer,
105
+ ContentType: contentType,
106
+ ACL: "public-read"
107
+ // Make the object publicly readable
108
+ });
109
+ await s3Client.send(command);
110
+ const fileUrl = `${S3_ENDPOINT}/${S3_BUCKET_NAME}/${fileName}`;
111
+ return { success: true, fileUrl };
112
+ } catch (error) {
113
+ console.error("Upload error:", JSON.stringify(error, null, 2));
114
+ return {
115
+ success: false,
116
+ error: error instanceof Error ? error.message : "Failed to upload file"
117
+ };
118
+ }
119
+ }
6
120
  export {
7
121
  uploadFile,
8
122
  uploadToS3
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
1
+ {"version":3,"sources":["../../../components/upload/upload-actions.ts"],"sourcesContent":["\"use server\";\nimport { BlobServiceClient } from \"@azure/storage-blob\";\nimport { PutObjectCommand, S3Client } from \"@aws-sdk/client-s3\";\n\n\nexport async function uploadFile(formData: FormData) {\n try {\n const file = formData.get(\"file\") as File;\n if (!file) {\n throw new Error(\"No file provided\");\n }\n\n const STORAGE_CONNECTION_STRING =\n process.env.AZURE_STORAGE_CONNECTION_STRING;\n const STORAGE_CONTAINER_NAME = process.env.AZURE_STORAGE_CONTAINER_NAME;\n\n if (!STORAGE_CONNECTION_STRING || !STORAGE_CONTAINER_NAME) {\n throw new Error(\n \"STORAGE_CONNECTION_STRING or STORAGE_CONTAINER_NAME not set\"\n );\n }\n\n // Generate unique filename\n const fileExtension = file.name.split(\".\").pop()?.toLowerCase();\n const fileName = `${Date.now()}-${Math.random().toString(36).substring(2)}.${fileExtension}`;\n\n // Determine content type based on file extension\n const contentType = getContentType(fileExtension);\n\n // Convert file to buffer\n const buffer = Buffer.from(await file.arrayBuffer());\n\n // Upload to Azure Blob Storage\n const blobServiceClient = BlobServiceClient.fromConnectionString(\n STORAGE_CONNECTION_STRING\n );\n const containerClient = blobServiceClient.getContainerClient(\n STORAGE_CONTAINER_NAME\n );\n\n // Create container if it doesn't exist\n await containerClient.createIfNotExists();\n\n // Upload file with content type\n const blockBlobClient = containerClient.getBlockBlobClient(fileName);\n await blockBlobClient.upload(buffer, buffer.length, {\n blobHTTPHeaders: {\n blobContentType: contentType,\n },\n });\n\n // Get the URL\n const fileUrl = blockBlobClient.url;\n\n return { success: true, fileUrl };\n } catch (error) {\n console.error(\"Upload error:\", error);\n return {\n success: false,\n error: error instanceof Error ? error.message : \"Failed to upload file\",\n };\n }\n}\n\nfunction getContentType(fileExtension: string | undefined): string {\n if (!fileExtension) return \"application/octet-stream\";\n\n const mimeTypes: Record<string, string> = {\n // Images\n png: \"image/png\",\n jpg: \"image/jpeg\",\n jpeg: \"image/jpeg\",\n gif: \"image/gif\",\n webp: \"image/webp\",\n svg: \"image/svg+xml\",\n // Documents\n pdf: \"application/pdf\",\n doc: \"application/msword\",\n docx: \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\",\n // Others\n txt: \"text/plain\",\n csv: \"text/csv\",\n json: \"application/json\",\n };\n\n return mimeTypes[fileExtension] || \"application/octet-stream\";\n}\n\nexport async function uploadToS3(formData: FormData) {\n try {\n const file = formData.get(\"file\") as File;\n if (!file) {\n throw new Error(\"No file provided\");\n }\n\n const AWS_ACCESS_KEY_ID = process.env.LINODE_ACCESS_KEY;\n const AWS_SECRET_ACCESS_KEY = process.env.LINODE_SECRET_KEY;\n const S3_BUCKET_NAME = process.env.LINODE_BUCKET_NAME;\n let S3_ENDPOINT = process.env.LINODE_OBJECT_STORAGE_ENDPOINT;\n\n if (\n !AWS_ACCESS_KEY_ID ||\n !AWS_SECRET_ACCESS_KEY ||\n !S3_BUCKET_NAME ||\n !S3_ENDPOINT\n ) {\n throw new Error(\"S3 credentials or configuration not set\");\n }\n\n // Generate unique filename\n const fileExtension = file.name.split(\".\").pop()?.toLowerCase();\n const fileName = `${Date.now()}-${Math.random().toString(36).substring(2)}.${fileExtension}`;\n\n // Determine content type based on file extension\n const contentType = getContentType(fileExtension);\n\n // Convert file to buffer\n const buffer = Buffer.from(await file.arrayBuffer());\n\n // Initialize S3 client\n const s3Client = new S3Client({\n endpoint: S3_ENDPOINT,\n region: \"us-east-1\", // Linode requires a region but it's not used with custom endpoint\n credentials: {\n accessKeyId: AWS_ACCESS_KEY_ID,\n secretAccessKey: AWS_SECRET_ACCESS_KEY,\n },\n forcePathStyle: true, // Required for Linode Object Storage\n maxAttempts: 3,\n requestHandler: {\n timeout: 10000, // 10 seconds timeout\n },\n });\n\n // Upload to S3\n const command = new PutObjectCommand({\n Bucket: S3_BUCKET_NAME,\n Key: fileName,\n Body: buffer,\n ContentType: contentType,\n ACL: \"public-read\", // Make the object publicly readable\n });\n\n await s3Client.send(command);\n\n // Construct the URL using the endpoint\n const fileUrl = `${S3_ENDPOINT}/${S3_BUCKET_NAME}/${fileName}`;\n\n return { success: true, fileUrl };\n } catch (error) {\n console.error(\"Upload error:\", JSON.stringify(error, null, 2));\n return {\n success: false,\n error: error instanceof Error ? error.message : \"Failed to upload file\",\n };\n }\n}\n"],"mappings":";;;;AACA,SAAS,yBAAyB;AAClC,SAAS,kBAAkB,gBAAgB;AAG3C,eAAsB,WAAW,UAAoB;AACnD,MAAI;AACF,UAAM,OAAO,SAAS,IAAI,MAAM;AAChC,QAAI,CAAC,MAAM;AACT,YAAM,IAAI,MAAM,kBAAkB;AAAA,IACpC;AAEA,UAAM,4BACJ,QAAQ,IAAI;AACd,UAAM,yBAAyB,QAAQ,IAAI;AAE3C,QAAI,CAAC,6BAA6B,CAAC,wBAAwB;AACzD,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAGA,UAAM,gBAAgB,KAAK,KAAK,MAAM,GAAG,EAAE,IAAI,GAAG,YAAY;AAC9D,UAAM,WAAW,GAAG,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,UAAU,CAAC,CAAC,IAAI,aAAa;AAG1F,UAAM,cAAc,eAAe,aAAa;AAGhD,UAAM,SAAS,OAAO,KAAK,MAAM,KAAK,YAAY,CAAC;AAGnD,UAAM,oBAAoB,kBAAkB;AAAA,MAC1C;AAAA,IACF;AACA,UAAM,kBAAkB,kBAAkB;AAAA,MACxC;AAAA,IACF;AAGA,UAAM,gBAAgB,kBAAkB;AAGxC,UAAM,kBAAkB,gBAAgB,mBAAmB,QAAQ;AACnE,UAAM,gBAAgB,OAAO,QAAQ,OAAO,QAAQ;AAAA,MAClD,iBAAiB;AAAA,QACf,iBAAiB;AAAA,MACnB;AAAA,IACF,CAAC;AAGD,UAAM,UAAU,gBAAgB;AAEhC,WAAO,EAAE,SAAS,MAAM,QAAQ;A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