fiberx-backend-toolkit 0.1.12 → 0.1.13
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[];
|
package/dist/config/constants.js
CHANGED
|
@@ -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"],
|
package/dist/storage/main.d.ts
CHANGED
|
@@ -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
|
-
|
|
5
|
+
import StorageDriverUtil from "./utils/storage_driver_util";
|
|
6
|
+
export { BaseStorageDriver, GCSStorageDriver, LocalStorageDriver, FileUploadProcessor, StorageDriverUtil };
|
package/dist/storage/main.js
CHANGED
|
@@ -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,23 @@
|
|
|
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
|
+
* Initialize driver based on environment
|
|
12
|
+
*/
|
|
13
|
+
private static initializeDriver;
|
|
14
|
+
/**
|
|
15
|
+
* Local driver (development)
|
|
16
|
+
*/
|
|
17
|
+
private static initLocalDriver;
|
|
18
|
+
/**
|
|
19
|
+
* GCS driver (staging/production)
|
|
20
|
+
*/
|
|
21
|
+
private static initGCSDriver;
|
|
22
|
+
}
|
|
23
|
+
export default StorageDriverUtil;
|
|
@@ -0,0 +1,97 @@
|
|
|
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
|
+
* Initialize driver based on environment
|
|
27
|
+
*/
|
|
28
|
+
static async initializeDriver() {
|
|
29
|
+
try {
|
|
30
|
+
if (main_1.InputValidatorUtil.isDevelopment()) {
|
|
31
|
+
return await StorageDriverUtil.initLocalDriver();
|
|
32
|
+
}
|
|
33
|
+
if (main_1.InputValidatorUtil.isLive()) {
|
|
34
|
+
const driver_type = StorageDriverUtil.env_manager.getEnvVar(constants_1.FILE_UPLOAD_ENV_KEYS.STORAGE_DRIVER_NAME, "");
|
|
35
|
+
switch (driver_type?.toLowerCase()) {
|
|
36
|
+
case "local": return StorageDriverUtil.initLocalDriver();
|
|
37
|
+
case "gcs": return await StorageDriverUtil.initGCSDriver();
|
|
38
|
+
default: throw new Error(`Unsupported storage driver type '${driver_type}' in live environment`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
throw new Error("Unsupported environment for storage driver");
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
this.logger.error("Failed to initialize storage driver", { error });
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Local driver (development)
|
|
50
|
+
*/
|
|
51
|
+
static async initLocalDriver() {
|
|
52
|
+
const base_dir = StorageDriverUtil.env_manager.getEnvVar(constants_1.FILE_UPLOAD_ENV_KEYS.LOCAL_STORAGE_BASE_DIR, "");
|
|
53
|
+
const public_base_url = StorageDriverUtil.env_manager.getEnvVar(constants_1.FILE_UPLOAD_ENV_KEYS.LOCAL_STORAGE_PUBLIC_URL, "");
|
|
54
|
+
if (!base_dir) {
|
|
55
|
+
throw new Error("LOCAL_STORAGE_BASE_DIR is required for local storage");
|
|
56
|
+
}
|
|
57
|
+
const config = {
|
|
58
|
+
type: "local",
|
|
59
|
+
base_dir,
|
|
60
|
+
public_base_url
|
|
61
|
+
};
|
|
62
|
+
const driver = new local_storage_driver_1.default();
|
|
63
|
+
await driver.initialize(config);
|
|
64
|
+
this.logger.info("Initialized Local Storage Driver");
|
|
65
|
+
return driver;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* GCS driver (staging/production)
|
|
69
|
+
*/
|
|
70
|
+
static async initGCSDriver() {
|
|
71
|
+
const project_id = StorageDriverUtil.env_manager.getEnvVar(constants_1.FILE_UPLOAD_ENV_KEYS.GCS_PROJECT_ID, "");
|
|
72
|
+
const bucket_name = StorageDriverUtil.env_manager.getEnvVar(constants_1.FILE_UPLOAD_ENV_KEYS.GCS_BUCKET_NAME, "");
|
|
73
|
+
;
|
|
74
|
+
const client_email = StorageDriverUtil.env_manager.getEnvVar(constants_1.FILE_UPLOAD_ENV_KEYS.GCS_CLIENT_EMAIL, "");
|
|
75
|
+
;
|
|
76
|
+
const private_key = StorageDriverUtil.env_manager.getEnvVar(constants_1.FILE_UPLOAD_ENV_KEYS.GCS_PRIVATE_KEY, "");
|
|
77
|
+
;
|
|
78
|
+
if (!project_id || !bucket_name || !client_email || !private_key) {
|
|
79
|
+
throw new Error("Missing required GCS environment variables");
|
|
80
|
+
}
|
|
81
|
+
const credentials = {
|
|
82
|
+
client_email,
|
|
83
|
+
private_key: private_key.replace(/\\n/g, "\n")
|
|
84
|
+
};
|
|
85
|
+
const config = {
|
|
86
|
+
type: "gcs",
|
|
87
|
+
project_id,
|
|
88
|
+
bucket_name,
|
|
89
|
+
credentials
|
|
90
|
+
};
|
|
91
|
+
const driver = new gcs_storage_driver_1.default();
|
|
92
|
+
await driver.initialize(config);
|
|
93
|
+
this.logger.info("Initialized GCS Storage Driver");
|
|
94
|
+
return driver;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.default = StorageDriverUtil;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fiberx-backend-toolkit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.13",
|
|
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",
|