fiberx-backend-toolkit 0.1.12 → 0.1.14

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.
@@ -83,6 +83,13 @@ export declare const FILE_UPLOAD_ENV_KEYS: {
83
83
  IMAGE_MIME_TYPES: string;
84
84
  VIDEO_MIME_TYPES: string;
85
85
  DOCUMENT_MIME_TYPES: string;
86
+ LOCAL_STORAGE_BASE_DIR: string;
87
+ LOCAL_STORAGE_PUBLIC_URL: string;
88
+ GCS_PROJECT_ID: string;
89
+ GCS_BUCKET_NAME: string;
90
+ GCS_CLIENT_EMAIL: string;
91
+ GCS_PRIVATE_KEY: string;
92
+ STORAGE_DRIVER_NAME: string;
86
93
  };
87
94
  export declare const FILE_UPLOAD_ALLOWED_MIME_TYPES: {
88
95
  image: string[];
@@ -133,7 +133,14 @@ exports.FILE_UPLOAD_ENV_KEYS = {
133
133
  DOCUMENT_MAX_SIZE: "FILE_UPLOAD_DOCUMENT_MAX_SIZE",
134
134
  IMAGE_MIME_TYPES: "FILE_UPLOAD_IMAGE_MIME_TYPES",
135
135
  VIDEO_MIME_TYPES: "FILE_UPLOAD_VIDEO_MIME_TYPES",
136
- DOCUMENT_MIME_TYPES: "FILE_UPLOAD_DOCUMENT_MIME_TYPES"
136
+ DOCUMENT_MIME_TYPES: "FILE_UPLOAD_DOCUMENT_MIME_TYPES",
137
+ LOCAL_STORAGE_BASE_DIR: "FILE_UPLOAD_LOCAL_STORAGE_BASE_DIR",
138
+ LOCAL_STORAGE_PUBLIC_URL: "FILE_UPLOAD_LOCAL_STORAGE_PUBLIC_URL",
139
+ GCS_PROJECT_ID: "FILE_UPLOAD_GCS_PROJECT_ID",
140
+ GCS_BUCKET_NAME: "FILE_UPLOAD_GCS_BUCKET_NAME",
141
+ GCS_CLIENT_EMAIL: "FILE_UPLOAD_GCS_CLIENT_EMAIL",
142
+ GCS_PRIVATE_KEY: "FILE_UPLOAD_GCS_PRIVATE_KEY",
143
+ STORAGE_DRIVER_NAME: "FILE_UPLOAD_STORAGE_DRIVER_NAME"
137
144
  };
138
145
  exports.FILE_UPLOAD_ALLOWED_MIME_TYPES = {
139
146
  image: ["image/png", "image/jpeg", "image/webp"],
@@ -2,4 +2,5 @@ import BaseStorageDriver from "./drivers/base_storage_driver";
2
2
  import GCSStorageDriver from "./drivers/gcs_storage_driver";
3
3
  import LocalStorageDriver from "./drivers/local_storage_driver";
4
4
  import FileUploadProcessor from "./processors/file_upload_processor";
5
- export { BaseStorageDriver, GCSStorageDriver, LocalStorageDriver, FileUploadProcessor, };
5
+ import StorageDriverUtil from "./utils/storage_driver_util";
6
+ export { BaseStorageDriver, GCSStorageDriver, LocalStorageDriver, FileUploadProcessor, StorageDriverUtil };
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.FileUploadProcessor = exports.LocalStorageDriver = exports.GCSStorageDriver = exports.BaseStorageDriver = void 0;
6
+ exports.StorageDriverUtil = exports.FileUploadProcessor = exports.LocalStorageDriver = exports.GCSStorageDriver = exports.BaseStorageDriver = void 0;
7
7
  const base_storage_driver_1 = __importDefault(require("./drivers/base_storage_driver"));
8
8
  exports.BaseStorageDriver = base_storage_driver_1.default;
9
9
  const gcs_storage_driver_1 = __importDefault(require("./drivers/gcs_storage_driver"));
@@ -12,3 +12,5 @@ const local_storage_driver_1 = __importDefault(require("./drivers/local_storage_
12
12
  exports.LocalStorageDriver = local_storage_driver_1.default;
13
13
  const file_upload_processor_1 = __importDefault(require("./processors/file_upload_processor"));
14
14
  exports.FileUploadProcessor = file_upload_processor_1.default;
15
+ const storage_driver_util_1 = __importDefault(require("./utils/storage_driver_util"));
16
+ exports.StorageDriverUtil = storage_driver_util_1.default;
@@ -0,0 +1,27 @@
1
+ import BaseStorageDriver from "../../storage/drivers/base_storage_driver";
2
+ declare class StorageDriverUtil {
3
+ private static instance;
4
+ private static logger;
5
+ private static env_manager;
6
+ /**
7
+ * Get initialized storage driver (singleton)
8
+ */
9
+ static getDriver(): Promise<BaseStorageDriver>;
10
+ /**
11
+ * Resolve driver synchronously based on env
12
+ */
13
+ static getDriverSync(): BaseStorageDriver;
14
+ /**
15
+ * Initialize driver based on environment
16
+ */
17
+ private static initializeDriver;
18
+ /**
19
+ * Local driver (development)
20
+ */
21
+ private static initLocalDriver;
22
+ /**
23
+ * GCS driver (staging/production)
24
+ */
25
+ private static initGCSDriver;
26
+ }
27
+ export default StorageDriverUtil;
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const main_1 = require("../../utils/main");
7
+ const constants_1 = require("../../config/constants");
8
+ const gcs_storage_driver_1 = __importDefault(require("../../storage/drivers/gcs_storage_driver"));
9
+ const local_storage_driver_1 = __importDefault(require("../../storage/drivers/local_storage_driver"));
10
+ class StorageDriverUtil {
11
+ static instance = null;
12
+ static logger = new main_1.LoggerUtil("storage_driver_util");
13
+ static env_manager = main_1.EnvManagerUtil.getInstance();
14
+ /**
15
+ * Get initialized storage driver (singleton)
16
+ */
17
+ static async getDriver() {
18
+ if (this.instance) {
19
+ return this.instance;
20
+ }
21
+ const driver = await this.initializeDriver();
22
+ this.instance = driver;
23
+ return driver;
24
+ }
25
+ /**
26
+ * Resolve driver synchronously based on env
27
+ */
28
+ static getDriverSync() {
29
+ if (this.instance) {
30
+ return this.instance;
31
+ }
32
+ throw new Error("Storage driver not initialized yet. Call getDriver() first to initialize.");
33
+ }
34
+ /**
35
+ * Initialize driver based on environment
36
+ */
37
+ static async initializeDriver() {
38
+ try {
39
+ if (main_1.InputValidatorUtil.isDevelopment()) {
40
+ return await StorageDriverUtil.initLocalDriver();
41
+ }
42
+ if (main_1.InputValidatorUtil.isLive()) {
43
+ const driver_type = StorageDriverUtil.env_manager.getEnvVar(constants_1.FILE_UPLOAD_ENV_KEYS.STORAGE_DRIVER_NAME, "");
44
+ switch (driver_type?.toLowerCase()) {
45
+ case "local": return StorageDriverUtil.initLocalDriver();
46
+ case "gcs": return await StorageDriverUtil.initGCSDriver();
47
+ default: throw new Error(`Unsupported storage driver type '${driver_type}' in live environment`);
48
+ }
49
+ }
50
+ throw new Error("Unsupported environment for storage driver");
51
+ }
52
+ catch (error) {
53
+ this.logger.error("Failed to initialize storage driver", { error });
54
+ throw error;
55
+ }
56
+ }
57
+ /**
58
+ * Local driver (development)
59
+ */
60
+ static async initLocalDriver() {
61
+ const base_dir = StorageDriverUtil.env_manager.getEnvVar(constants_1.FILE_UPLOAD_ENV_KEYS.LOCAL_STORAGE_BASE_DIR, "");
62
+ const public_base_url = StorageDriverUtil.env_manager.getEnvVar(constants_1.FILE_UPLOAD_ENV_KEYS.LOCAL_STORAGE_PUBLIC_URL, "");
63
+ if (!base_dir) {
64
+ throw new Error("LOCAL_STORAGE_BASE_DIR is required for local storage");
65
+ }
66
+ const config = {
67
+ type: "local",
68
+ base_dir,
69
+ public_base_url
70
+ };
71
+ const driver = new local_storage_driver_1.default();
72
+ await driver.initialize(config);
73
+ this.logger.info("Initialized Local Storage Driver");
74
+ return driver;
75
+ }
76
+ /**
77
+ * GCS driver (staging/production)
78
+ */
79
+ static async initGCSDriver() {
80
+ const project_id = StorageDriverUtil.env_manager.getEnvVar(constants_1.FILE_UPLOAD_ENV_KEYS.GCS_PROJECT_ID, "");
81
+ const bucket_name = StorageDriverUtil.env_manager.getEnvVar(constants_1.FILE_UPLOAD_ENV_KEYS.GCS_BUCKET_NAME, "");
82
+ ;
83
+ const client_email = StorageDriverUtil.env_manager.getEnvVar(constants_1.FILE_UPLOAD_ENV_KEYS.GCS_CLIENT_EMAIL, "");
84
+ ;
85
+ const private_key = StorageDriverUtil.env_manager.getEnvVar(constants_1.FILE_UPLOAD_ENV_KEYS.GCS_PRIVATE_KEY, "");
86
+ ;
87
+ if (!project_id || !bucket_name || !client_email || !private_key) {
88
+ throw new Error("Missing required GCS environment variables");
89
+ }
90
+ const credentials = {
91
+ client_email,
92
+ private_key: private_key.replace(/\\n/g, "\n")
93
+ };
94
+ const config = {
95
+ type: "gcs",
96
+ project_id,
97
+ bucket_name,
98
+ credentials
99
+ };
100
+ const driver = new gcs_storage_driver_1.default();
101
+ await driver.initialize(config);
102
+ this.logger.info("Initialized GCS Storage Driver");
103
+ return driver;
104
+ }
105
+ }
106
+ exports.default = StorageDriverUtil;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fiberx-backend-toolkit",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "description": "A TypeScript backend toolkit providing shared domain logic, infrastructure helpers, and utilities for FiberX server-side applications and services.",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",