@bike4mind/cli 0.1.0

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.
@@ -0,0 +1,92 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ BadRequestError,
4
+ checkStorageLimitForFile,
5
+ getFileExtension,
6
+ getMimeTypeByExtension,
7
+ getSettingsMap,
8
+ getSettingsValue,
9
+ secureParameters
10
+ } from "./chunk-JVPB6BB5.js";
11
+ import {
12
+ KnowledgeType,
13
+ SupportedFabFileMimeTypes
14
+ } from "./chunk-MKO2KCCS.js";
15
+
16
+ // ../../b4m-core/packages/services/dist/src/fabFileService/create.js
17
+ import { z } from "zod";
18
+ import { v4 as uuidv4 } from "uuid";
19
+ var createFabFileSchema = z.object({
20
+ fileName: z.string(),
21
+ mimeType: z.string(),
22
+ fileSize: z.number(),
23
+ type: z.nativeEnum(KnowledgeType),
24
+ content: z.union([z.string(), z.instanceof(Buffer)]).optional(),
25
+ organizationId: z.string().optional(),
26
+ /**
27
+ * Content type of the file
28
+ * @example 'text/markdown'
29
+ * @example 'application/pdf'
30
+ * @example 'application/octet-stream' for binary files
31
+ */
32
+ contentType: z.string().optional(),
33
+ public: z.boolean().optional(),
34
+ prefix: z.string().optional(),
35
+ system: z.boolean().optional(),
36
+ tags: z.array(z.object({ name: z.string(), strength: z.number() })).optional(),
37
+ systemPriority: z.number().optional(),
38
+ sessionId: z.string().optional()
39
+ });
40
+ var DEFAULT_MAX_FILE_SIZE = 20;
41
+ var DEFAULT_EXPIRE_IN_SECONDS = 3600 * 24 * 5;
42
+ var createFabFile = async (userId, parameters, { db, storage }) => {
43
+ const params = secureParameters(parameters, createFabFileSchema);
44
+ const user = await db.users.findById(userId);
45
+ if (!user)
46
+ throw new BadRequestError("User not found");
47
+ let ext = "";
48
+ let mimeType = parameters.mimeType;
49
+ ext = getFileExtension(params.fileName);
50
+ mimeType = params.mimeType || getMimeTypeByExtension(ext);
51
+ if (!mimeType) {
52
+ mimeType = SupportedFabFileMimeTypes.TXT_PLAIN;
53
+ }
54
+ if (!Object.values(SupportedFabFileMimeTypes).some((type) => type === mimeType)) {
55
+ throw new BadRequestError(`File type ${mimeType} is not supported`);
56
+ }
57
+ let filePath = params.prefix ? `${params.prefix}/` : "";
58
+ filePath += `${uuidv4()}${ext ? `.${ext}` : ".txt"}`;
59
+ const maxFileSize = getSettingsValue("MaxFileSize", await getSettingsMap(db), DEFAULT_MAX_FILE_SIZE) * 1024 * 1024;
60
+ if (params.fileSize >= maxFileSize)
61
+ throw new BadRequestError("File size exceeds maximum file size");
62
+ await checkStorageLimitForFile(user, params.fileSize, params.organizationId, db.organizations?.findById);
63
+ const buildData = {
64
+ userId,
65
+ ...params,
66
+ mimeType,
67
+ filePath,
68
+ users: [],
69
+ groups: [],
70
+ isGlobalRead: false,
71
+ isGlobalWrite: false,
72
+ createdAt: /* @__PURE__ */ new Date(),
73
+ updatedAt: /* @__PURE__ */ new Date()
74
+ };
75
+ if (params.content) {
76
+ await storage.upload(filePath, params.content, {
77
+ ContentType: params.contentType,
78
+ ContentLength: params.fileSize
79
+ });
80
+ buildData.fileUrl = await storage.generateSignedUrl(filePath, DEFAULT_EXPIRE_IN_SECONDS, "get");
81
+ buildData.fileUrlExpireAt = new Date(Date.now() + DEFAULT_EXPIRE_IN_SECONDS * 1e3);
82
+ } else {
83
+ buildData.presignedUrl = await storage.generateSignedUrl(filePath, 600, "put");
84
+ }
85
+ const result = await db.fabFiles.create(buildData);
86
+ return result;
87
+ };
88
+
89
+ export {
90
+ createFabFileSchema,
91
+ createFabFile
92
+ };