@settlemint/sdk-minio 2.1.4 → 2.1.5-pr3ad019ac

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.
package/dist/minio.d.ts CHANGED
@@ -1,19 +1,19 @@
1
- import { Client } from 'minio';
1
+ import { Client, ItemBucketMetadata } from 'minio';
2
+ export { Client, ItemBucketMetadata } from 'minio';
2
3
  import { z } from 'zod';
4
+ import { Buffer } from 'node:buffer';
3
5
 
4
6
  /**
5
- * Schema for validating server client options for the Portal client.
6
- * Extends the ClientOptionsSchema with additional server-specific fields.
7
+ * Schema for validating server client options for the MinIO client.
7
8
  */
8
- declare const ServerClientOptionsSchema: z.ZodObject<z.objectUtil.extendShape<{
9
+ declare const ServerClientOptionsSchema: z.ZodObject<{
9
10
  /** The URL of the MinIO instance to connect to */
10
11
  instance: z.ZodString;
11
- }, {
12
12
  /** The MinIO access key used to authenticate with the MinIO server */
13
13
  accessKey: z.ZodString;
14
14
  /** The MinIO secret key used to authenticate with the MinIO server */
15
15
  secretKey: z.ZodString;
16
- }>, "strip", z.ZodTypeAny, {
16
+ }, "strip", z.ZodTypeAny, {
17
17
  instance: string;
18
18
  accessKey: string;
19
19
  secretKey: string;
@@ -27,6 +27,328 @@ declare const ServerClientOptionsSchema: z.ZodObject<z.objectUtil.extendShape<{
27
27
  */
28
28
  type ServerClientOptions = z.infer<typeof ServerClientOptionsSchema>;
29
29
 
30
+ /**
31
+ * Base interface for all MinIO operations
32
+ *
33
+ * @template T The return type of the operation
34
+ */
35
+ interface MinioOperation<T> {
36
+ execute: (client: Client) => Promise<T>;
37
+ }
38
+ /**
39
+ * Creates an operation to list objects in a bucket
40
+ *
41
+ * @param bucket - The bucket name to list objects from
42
+ * @param prefix - Optional prefix to filter objects (like a folder path)
43
+ * @returns A MinioOperation that lists objects when executed
44
+ * @throws Will throw an error if the operation fails
45
+ *
46
+ * @example
47
+ * import { createListObjectsOperation, executeMinioOperation } from "@settlemint/sdk-minio";
48
+ *
49
+ * const listOperation = createListObjectsOperation("my-bucket", "folder/");
50
+ * const objects = await executeMinioOperation(client, listOperation);
51
+ */
52
+ declare function createListObjectsOperation(bucket: string, prefix?: string): MinioOperation<Array<{
53
+ name: string;
54
+ prefix?: string;
55
+ size: number;
56
+ etag: string;
57
+ lastModified: Date;
58
+ }>>;
59
+ /**
60
+ * Creates an operation to get an object's metadata
61
+ *
62
+ * @param bucket - The bucket name containing the object
63
+ * @param objectName - The object name/path
64
+ * @returns A MinioOperation that gets object stats when executed
65
+ * @throws Will throw an error if the operation fails
66
+ *
67
+ * @example
68
+ * import { createStatObjectOperation, executeMinioOperation } from "@settlemint/sdk-minio";
69
+ *
70
+ * const statOperation = createStatObjectOperation("my-bucket", "folder/file.txt");
71
+ * const stats = await executeMinioOperation(client, statOperation);
72
+ */
73
+ declare function createStatObjectOperation(bucket: string, objectName: string): MinioOperation<{
74
+ size: number;
75
+ etag: string;
76
+ metaData: Record<string, string>;
77
+ lastModified: Date;
78
+ }>;
79
+ /**
80
+ * Creates an operation to upload a buffer to MinIO
81
+ *
82
+ * @param bucket - The bucket name to upload to
83
+ * @param objectName - The object name/path to create
84
+ * @param buffer - The buffer containing the file data
85
+ * @param metadata - Optional metadata to attach to the object
86
+ * @returns A MinioOperation that uploads the buffer when executed
87
+ * @throws Will throw an error if the operation fails
88
+ *
89
+ * @example
90
+ * import { createUploadOperation, executeMinioOperation } from "@settlemint/sdk-minio";
91
+ *
92
+ * const buffer = Buffer.from("file content");
93
+ * const uploadOperation = createUploadOperation("my-bucket", "folder/file.txt", buffer, { "content-type": "text/plain" });
94
+ * const result = await executeMinioOperation(client, uploadOperation);
95
+ */
96
+ declare function createUploadOperation(bucket: string, objectName: string, buffer: Buffer, metadata?: ItemBucketMetadata): MinioOperation<{
97
+ etag: string;
98
+ }>;
99
+ /**
100
+ * Creates an operation to delete an object from MinIO
101
+ *
102
+ * @param bucket - The bucket name containing the object
103
+ * @param objectName - The object name/path to delete
104
+ * @returns A MinioOperation that deletes the object when executed
105
+ * @throws Will throw an error if the operation fails
106
+ *
107
+ * @example
108
+ * import { createDeleteOperation, executeMinioOperation } from "@settlemint/sdk-minio";
109
+ *
110
+ * const deleteOperation = createDeleteOperation("my-bucket", "folder/file.txt");
111
+ * await executeMinioOperation(client, deleteOperation);
112
+ */
113
+ declare function createDeleteOperation(bucket: string, objectName: string): MinioOperation<void>;
114
+ /**
115
+ * Creates an operation to generate a presigned URL for an object
116
+ *
117
+ * @param bucket - The bucket name containing the object
118
+ * @param objectName - The object name/path
119
+ * @param expirySeconds - How long the URL should be valid for in seconds
120
+ * @returns A MinioOperation that creates a presigned URL when executed
121
+ * @throws Will throw an error if the operation fails
122
+ *
123
+ * @example
124
+ * import { createPresignedUrlOperation, executeMinioOperation } from "@settlemint/sdk-minio";
125
+ *
126
+ * const urlOperation = createPresignedUrlOperation("my-bucket", "folder/file.txt", 3600);
127
+ * const url = await executeMinioOperation(client, urlOperation);
128
+ */
129
+ declare function createPresignedUrlOperation(bucket: string, objectName: string, expirySeconds: number): MinioOperation<string>;
130
+ /**
131
+ * Creates an operation to generate a presigned PUT URL for direct uploads
132
+ *
133
+ * @param bucket - The bucket name to upload to
134
+ * @param objectName - The object name/path to create
135
+ * @param expirySeconds - How long the URL should be valid for in seconds
136
+ * @returns A MinioOperation that creates a presigned PUT URL when executed
137
+ * @throws Will throw an error if the operation fails
138
+ *
139
+ * @example
140
+ * import { createPresignedPutOperation, executeMinioOperation } from "@settlemint/sdk-minio";
141
+ *
142
+ * const putUrlOperation = createPresignedPutOperation("my-bucket", "folder/file.txt", 3600);
143
+ * const url = await executeMinioOperation(client, putUrlOperation);
144
+ */
145
+ declare function createPresignedPutOperation(bucket: string, objectName: string, expirySeconds: number): MinioOperation<string>;
146
+ /**
147
+ * Creates a simplified upload function bound to a specific client
148
+ *
149
+ * @param client - The MinIO client to use for uploads
150
+ * @returns A function that uploads buffers to MinIO
151
+ * @throws Will throw an error if the operation fails
152
+ *
153
+ * @example
154
+ * import { createSimpleUploadOperation, getMinioClient } from "@settlemint/sdk-minio";
155
+ *
156
+ * const client = await getMinioClient();
157
+ * const uploadFn = createSimpleUploadOperation(client);
158
+ * const result = await uploadFn(buffer, "my-bucket", "folder/file.txt", { "content-type": "text/plain" });
159
+ */
160
+ declare function createSimpleUploadOperation(client: Client): (buffer: Buffer, bucket: string, objectName: string, metadata?: ItemBucketMetadata) => Promise<{
161
+ etag: string;
162
+ }>;
163
+
164
+ /**
165
+ * Helper type to extract the inferred type from a Zod schema.
166
+ *
167
+ * @template T - The Zod schema type
168
+ */
169
+ type Static<T extends z.ZodType> = z.infer<T>;
170
+ /**
171
+ * Schema for file metadata stored in MinIO.
172
+ * Defines the structure and validation rules for file information.
173
+ */
174
+ declare const FileMetadataSchema: z.ZodObject<{
175
+ id: z.ZodString;
176
+ name: z.ZodString;
177
+ contentType: z.ZodString;
178
+ size: z.ZodNumber;
179
+ uploadedAt: z.ZodString;
180
+ etag: z.ZodString;
181
+ url: z.ZodOptional<z.ZodString>;
182
+ }, "strip", z.ZodTypeAny, {
183
+ id: string;
184
+ name: string;
185
+ contentType: string;
186
+ size: number;
187
+ uploadedAt: string;
188
+ etag: string;
189
+ url?: string | undefined;
190
+ }, {
191
+ id: string;
192
+ name: string;
193
+ contentType: string;
194
+ size: number;
195
+ uploadedAt: string;
196
+ etag: string;
197
+ url?: string | undefined;
198
+ }>;
199
+ /**
200
+ * Type representing file metadata after validation.
201
+ */
202
+ type FileMetadata = Static<typeof FileMetadataSchema>;
203
+ /**
204
+ * Default bucket name to use for file storage when none is specified.
205
+ */
206
+ declare const DEFAULT_BUCKET = "uploads";
207
+
208
+ /**
209
+ * Gets a list of files with optional prefix filter
210
+ *
211
+ * @param client - The MinIO client to use
212
+ * @param prefix - Optional prefix to filter files (like a folder path)
213
+ * @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
214
+ * @returns Array of file metadata objects
215
+ * @throws Will throw an error if the operation fails or client initialization fails
216
+ *
217
+ * @example
218
+ * import { createServerMinioClient, getFilesList } from "@settlemint/sdk-minio";
219
+ *
220
+ * const { client } = createServerMinioClient({
221
+ * instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
222
+ * accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
223
+ * secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
224
+ * });
225
+ *
226
+ * const files = await getFilesList(client, "documents/");
227
+ */
228
+ declare function getFilesList(client: Client, prefix?: string, bucket?: string): Promise<FileMetadata[]>;
229
+ /**
230
+ * Gets a single file by its object name
231
+ *
232
+ * @param client - The MinIO client to use
233
+ * @param objectName - The object name/path
234
+ * @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
235
+ * @returns File metadata with presigned URL
236
+ * @throws Will throw an error if the file doesn't exist or client initialization fails
237
+ *
238
+ * @example
239
+ * import { createServerMinioClient, getFileByObjectName } from "@settlemint/sdk-minio";
240
+ *
241
+ * const { client } = createServerMinioClient({
242
+ * instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
243
+ * accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
244
+ * secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
245
+ * });
246
+ *
247
+ * const file = await getFileByObjectName(client, "documents/report.pdf");
248
+ */
249
+ declare function getFileByObjectName(client: Client, objectName: string, bucket?: string): Promise<FileMetadata>;
250
+ /**
251
+ * Deletes a file from storage
252
+ *
253
+ * @param client - The MinIO client to use
254
+ * @param fileId - The file identifier/path to delete
255
+ * @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
256
+ * @returns Success status
257
+ * @throws Will throw an error if deletion fails or client initialization fails
258
+ *
259
+ * @example
260
+ * import { createServerMinioClient, deleteFile } from "@settlemint/sdk-minio";
261
+ *
262
+ * const { client } = createServerMinioClient({
263
+ * instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
264
+ * accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
265
+ * secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
266
+ * });
267
+ *
268
+ * await deleteFile(client, "documents/report.pdf");
269
+ */
270
+ declare function deleteFile(client: Client, fileId: string, bucket?: string): Promise<boolean>;
271
+ /**
272
+ * Creates a presigned upload URL for direct browser uploads
273
+ *
274
+ * @param client - The MinIO client to use
275
+ * @param fileName - The file name to use
276
+ * @param path - Optional path/folder
277
+ * @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
278
+ * @param expirySeconds - How long the URL should be valid for
279
+ * @returns Presigned URL for PUT operation
280
+ * @throws Will throw an error if URL creation fails or client initialization fails
281
+ *
282
+ * @example
283
+ * import { createServerMinioClient, createPresignedUploadUrl } from "@settlemint/sdk-minio";
284
+ *
285
+ * const { client } = createServerMinioClient({
286
+ * instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
287
+ * accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
288
+ * secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
289
+ * });
290
+ *
291
+ * // Generate the presigned URL on the server
292
+ * const url = await createPresignedUploadUrl(client, "report.pdf", "documents/");
293
+ *
294
+ * // Send the URL to the client/browser via HTTP response
295
+ * return Response.json({ uploadUrl: url });
296
+ *
297
+ * // Then in the browser:
298
+ * const response = await fetch('/api/get-upload-url');
299
+ * const { uploadUrl } = await response.json();
300
+ * await fetch(uploadUrl, {
301
+ * method: 'PUT',
302
+ * headers: { 'Content-Type': 'application/pdf' },
303
+ * body: pdfFile
304
+ * });
305
+ */
306
+ declare function createPresignedUploadUrl(client: Client, fileName: string, path?: string, bucket?: string, expirySeconds?: number): Promise<string>;
307
+ /**
308
+ * Uploads a buffer directly to storage
309
+ *
310
+ * @param client - The MinIO client to use
311
+ * @param buffer - The buffer to upload
312
+ * @param objectName - The full object name/path
313
+ * @param contentType - The content type of the file
314
+ * @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
315
+ * @returns The uploaded file metadata
316
+ * @throws Will throw an error if upload fails or client initialization fails
317
+ *
318
+ * @example
319
+ * import { createServerMinioClient, uploadBuffer } from "@settlemint/sdk-minio";
320
+ *
321
+ * const { client } = createServerMinioClient({
322
+ * instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
323
+ * accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
324
+ * secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
325
+ * });
326
+ *
327
+ * const buffer = Buffer.from("Hello, world!");
328
+ * const uploadedFile = await uploadFile(client, buffer, "documents/hello.txt", "text/plain");
329
+ */
330
+ declare function uploadFile(client: Client, buffer: Buffer, objectName: string, contentType: string, bucket?: string): Promise<FileMetadata>;
331
+
332
+ /**
333
+ * Executes a MinIO operation using the provided client
334
+ *
335
+ * @param client - MinIO client to use
336
+ * @param operation - The operation to execute
337
+ * @returns The result of the operation execution
338
+ * @throws Will throw an error if the operation fails
339
+ *
340
+ * @example
341
+ * import { createServerMinioClient, createListObjectsOperation, executeMinioOperation } from "@settlemint/sdk-minio";
342
+ * const { client } = createServerMinioClient({
343
+ * instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
344
+ * accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
345
+ * secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
346
+ * });
347
+ * const listOperation = createListObjectsOperation("my-bucket", "prefix/");
348
+ * const result = await executeMinioOperation(client, listOperation);
349
+ */
350
+ declare function executeMinioOperation<T>(client: Client, operation: MinioOperation<T>): Promise<T>;
351
+
30
352
  /**
31
353
  * Creates a MinIO client for server-side use with authentication.
32
354
  *
@@ -48,4 +370,4 @@ declare function createServerMinioClient(options: ServerClientOptions): {
48
370
  client: Client;
49
371
  };
50
372
 
51
- export { createServerMinioClient };
373
+ export { DEFAULT_BUCKET, type FileMetadata, type MinioOperation, createDeleteOperation, createListObjectsOperation, createPresignedPutOperation, createPresignedUploadUrl, createPresignedUrlOperation, createServerMinioClient, createSimpleUploadOperation, createStatObjectOperation, createUploadOperation, deleteFile, executeMinioOperation, getFileByObjectName, getFilesList, uploadFile };
package/dist/minio.mjs CHANGED
@@ -1,26 +1,259 @@
1
1
  // src/minio.ts
2
- import { ensureServer } from "@settlemint/sdk-utils/runtime";
3
- import { validate } from "@settlemint/sdk-utils/validation";
2
+ import { ensureServer as ensureServer3 } from "@settlemint/sdk-utils/runtime";
3
+ import { validate as validate2 } from "@settlemint/sdk-utils/validation";
4
4
  import { Client } from "minio";
5
5
 
6
6
  // src/helpers/client-options.schema.ts
7
7
  import { UrlSchema } from "@settlemint/sdk-utils/validation";
8
8
  import { z } from "zod";
9
- var ClientOptionsSchema = z.object({
9
+ var ServerClientOptionsSchema = z.object({
10
10
  /** The URL of the MinIO instance to connect to */
11
- instance: UrlSchema
12
- });
13
- var ServerClientOptionsSchema = ClientOptionsSchema.extend({
11
+ instance: UrlSchema,
14
12
  /** The MinIO access key used to authenticate with the MinIO server */
15
13
  accessKey: z.string(),
16
14
  /** The MinIO secret key used to authenticate with the MinIO server */
17
15
  secretKey: z.string()
18
16
  });
19
17
 
18
+ // src/helpers/operations.ts
19
+ function createListObjectsOperation(bucket, prefix = "") {
20
+ return {
21
+ execute: async (client) => {
22
+ const objectsStream = client.listObjects(bucket, prefix, true);
23
+ const objects = [];
24
+ return new Promise((resolve, reject) => {
25
+ objectsStream.on("data", (obj) => {
26
+ if (obj.name && typeof obj.size === "number" && obj.etag && obj.lastModified) {
27
+ objects.push({
28
+ name: obj.name,
29
+ prefix: obj.prefix,
30
+ size: obj.size,
31
+ etag: obj.etag,
32
+ lastModified: obj.lastModified
33
+ });
34
+ }
35
+ });
36
+ objectsStream.on("error", (err) => {
37
+ reject(err);
38
+ });
39
+ objectsStream.on("end", () => {
40
+ resolve(objects);
41
+ });
42
+ });
43
+ }
44
+ };
45
+ }
46
+ function createStatObjectOperation(bucket, objectName) {
47
+ return {
48
+ execute: async (client) => {
49
+ return client.statObject(bucket, objectName);
50
+ }
51
+ };
52
+ }
53
+ function createUploadOperation(bucket, objectName, buffer, metadata) {
54
+ return {
55
+ execute: async (client) => {
56
+ return client.putObject(bucket, objectName, buffer, void 0, metadata);
57
+ }
58
+ };
59
+ }
60
+ function createDeleteOperation(bucket, objectName) {
61
+ return {
62
+ execute: async (client) => {
63
+ return client.removeObject(bucket, objectName);
64
+ }
65
+ };
66
+ }
67
+ function createPresignedUrlOperation(bucket, objectName, expirySeconds) {
68
+ return {
69
+ execute: async (client) => {
70
+ return client.presignedGetObject(bucket, objectName, expirySeconds);
71
+ }
72
+ };
73
+ }
74
+ function createPresignedPutOperation(bucket, objectName, expirySeconds) {
75
+ return {
76
+ execute: async (client) => {
77
+ return client.presignedPutObject(bucket, objectName, expirySeconds);
78
+ }
79
+ };
80
+ }
81
+ function createSimpleUploadOperation(client) {
82
+ return async (buffer, bucket, objectName, metadata) => {
83
+ return client.putObject(bucket, objectName, buffer, void 0, metadata);
84
+ };
85
+ }
86
+
87
+ // src/helpers/functions.ts
88
+ import { ensureServer as ensureServer2 } from "@settlemint/sdk-utils/runtime";
89
+ import { validate } from "@settlemint/sdk-utils/validation";
90
+
91
+ // src/helpers/executor.ts
92
+ import { ensureServer } from "@settlemint/sdk-utils/runtime";
93
+ async function executeMinioOperation(client, operation) {
94
+ ensureServer();
95
+ return operation.execute(client);
96
+ }
97
+
98
+ // src/helpers/schema.ts
99
+ import { z as z2 } from "zod";
100
+ var FileMetadataSchema = z2.object({
101
+ id: z2.string(),
102
+ name: z2.string(),
103
+ contentType: z2.string(),
104
+ size: z2.number(),
105
+ uploadedAt: z2.string().datetime(),
106
+ etag: z2.string(),
107
+ url: z2.string().url().optional()
108
+ });
109
+ var DEFAULT_BUCKET = "uploads";
110
+
111
+ // src/helpers/functions.ts
112
+ function normalizePath(path, fileName) {
113
+ if (path.length > 1e3) {
114
+ throw new Error("Path is too long");
115
+ }
116
+ const cleanPath = path.replace(/\/+$/, "");
117
+ if (!cleanPath) {
118
+ return fileName;
119
+ }
120
+ return `${cleanPath}/${fileName}`;
121
+ }
122
+ async function getFilesList(client, prefix = "", bucket = DEFAULT_BUCKET) {
123
+ ensureServer2();
124
+ console.log(`Listing files with prefix: "${prefix}" in bucket: "${bucket}"`);
125
+ try {
126
+ const listOperation = createListObjectsOperation(bucket, prefix);
127
+ const objects = await executeMinioOperation(client, listOperation);
128
+ console.log(`Found ${objects.length} files in MinIO`);
129
+ const fileObjects = await Promise.all(
130
+ objects.map(async (obj) => {
131
+ const presignedUrlOperation = createPresignedUrlOperation(
132
+ bucket,
133
+ obj.name,
134
+ 3600
135
+ // 1 hour expiry
136
+ );
137
+ const url = await executeMinioOperation(client, presignedUrlOperation);
138
+ return {
139
+ id: obj.name,
140
+ name: obj.name.split("/").pop() || obj.name,
141
+ contentType: "application/octet-stream",
142
+ // Default type
143
+ size: obj.size,
144
+ uploadedAt: obj.lastModified.toISOString(),
145
+ etag: obj.etag,
146
+ url
147
+ };
148
+ })
149
+ );
150
+ return validate(FileMetadataSchema.array(), fileObjects);
151
+ } catch (error) {
152
+ console.error("Failed to list files:", error);
153
+ throw new Error(`Failed to list files: ${error instanceof Error ? error.message : String(error)}`);
154
+ }
155
+ }
156
+ async function getFileByObjectName(client, objectName, bucket = DEFAULT_BUCKET) {
157
+ ensureServer2();
158
+ console.log(`Getting file details for: ${objectName} in bucket: ${bucket}`);
159
+ try {
160
+ const statOperation = createStatObjectOperation(bucket, objectName);
161
+ const statResult = await executeMinioOperation(client, statOperation);
162
+ const presignedUrlOperation = createPresignedUrlOperation(
163
+ bucket,
164
+ objectName,
165
+ 3600
166
+ // 1 hour expiry
167
+ );
168
+ const url = await executeMinioOperation(client, presignedUrlOperation);
169
+ let size = 0;
170
+ if (statResult.metaData["content-length"]) {
171
+ const parsedSize = Number.parseInt(statResult.metaData["content-length"], 10);
172
+ if (!Number.isNaN(parsedSize)) {
173
+ size = parsedSize;
174
+ }
175
+ } else if (typeof statResult.size === "number" && !Number.isNaN(statResult.size)) {
176
+ size = statResult.size;
177
+ }
178
+ const fileMetadata = {
179
+ id: objectName,
180
+ name: objectName.split("/").pop() || objectName,
181
+ contentType: statResult.metaData["content-type"] || "application/octet-stream",
182
+ size,
183
+ uploadedAt: statResult.lastModified.toISOString(),
184
+ etag: statResult.etag,
185
+ url
186
+ };
187
+ return validate(FileMetadataSchema, fileMetadata);
188
+ } catch (error) {
189
+ console.error(`Failed to get file ${objectName}:`, error);
190
+ throw new Error(`Failed to get file ${objectName}: ${error instanceof Error ? error.message : String(error)}`);
191
+ }
192
+ }
193
+ async function deleteFile(client, fileId, bucket = DEFAULT_BUCKET) {
194
+ ensureServer2();
195
+ try {
196
+ const deleteOperation = createDeleteOperation(bucket, fileId);
197
+ await executeMinioOperation(client, deleteOperation);
198
+ return true;
199
+ } catch (error) {
200
+ console.error(`Failed to delete file ${fileId}:`, error);
201
+ throw new Error(`Failed to delete file ${fileId}: ${error instanceof Error ? error.message : String(error)}`);
202
+ }
203
+ }
204
+ async function createPresignedUploadUrl(client, fileName, path = "", bucket = DEFAULT_BUCKET, expirySeconds = 3600) {
205
+ ensureServer2();
206
+ try {
207
+ const safeFileName = fileName.replace(/[^a-zA-Z0-9._-]/g, "_");
208
+ const objectName = normalizePath(path, safeFileName);
209
+ const presignedPutOperation = createPresignedPutOperation(bucket, objectName, expirySeconds);
210
+ const url = await executeMinioOperation(client, presignedPutOperation);
211
+ if (!url) {
212
+ throw new Error("Failed to generate presigned upload URL");
213
+ }
214
+ return url;
215
+ } catch (error) {
216
+ console.error("Failed to create presigned upload URL:", error);
217
+ throw new Error(`Failed to create presigned upload URL: ${error instanceof Error ? error.message : String(error)}`);
218
+ }
219
+ }
220
+ async function uploadFile(client, buffer, objectName, contentType, bucket = DEFAULT_BUCKET) {
221
+ ensureServer2();
222
+ try {
223
+ const metadata = {
224
+ "content-type": contentType,
225
+ "upload-time": (/* @__PURE__ */ new Date()).toISOString()
226
+ };
227
+ const simpleUploadFn = createSimpleUploadOperation(client);
228
+ const result = await simpleUploadFn(buffer, bucket, objectName, metadata);
229
+ const presignedUrlOperation = createPresignedUrlOperation(
230
+ bucket,
231
+ objectName,
232
+ 3600
233
+ // 1 hour expiry
234
+ );
235
+ const url = await executeMinioOperation(client, presignedUrlOperation);
236
+ const fileName = objectName.split("/").pop() || objectName;
237
+ const fileMetadata = {
238
+ id: objectName,
239
+ name: fileName,
240
+ contentType,
241
+ size: buffer.length,
242
+ uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
243
+ etag: result.etag,
244
+ url
245
+ };
246
+ return validate(FileMetadataSchema, fileMetadata);
247
+ } catch (error) {
248
+ console.error("Failed to upload file:", error);
249
+ throw new Error(`Failed to upload file: ${error instanceof Error ? error.message : String(error)}`);
250
+ }
251
+ }
252
+
20
253
  // src/minio.ts
21
254
  function createServerMinioClient(options) {
22
- ensureServer();
23
- const validatedOptions = validate(ServerClientOptionsSchema, options);
255
+ ensureServer3();
256
+ const validatedOptions = validate2(ServerClientOptionsSchema, options);
24
257
  const url = new URL(validatedOptions.instance);
25
258
  return {
26
259
  client: new Client({
@@ -34,6 +267,20 @@ function createServerMinioClient(options) {
34
267
  };
35
268
  }
36
269
  export {
37
- createServerMinioClient
270
+ DEFAULT_BUCKET,
271
+ createDeleteOperation,
272
+ createListObjectsOperation,
273
+ createPresignedPutOperation,
274
+ createPresignedUploadUrl,
275
+ createPresignedUrlOperation,
276
+ createServerMinioClient,
277
+ createSimpleUploadOperation,
278
+ createStatObjectOperation,
279
+ createUploadOperation,
280
+ deleteFile,
281
+ executeMinioOperation,
282
+ getFileByObjectName,
283
+ getFilesList,
284
+ uploadFile
38
285
  };
39
286
  //# sourceMappingURL=minio.mjs.map