fiberx-backend-toolkit 0.1.15 → 0.1.16

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.
@@ -2,7 +2,12 @@ import BaseStorageDriver from "./base_storage_driver";
2
2
  import { StorageInitConfig } from "../../types/storage_type";
3
3
  declare class GCSStorageDriver extends BaseStorageDriver {
4
4
  private bucket;
5
+ bucket_name: string;
5
6
  initialize(config: StorageInitConfig): Promise<void>;
7
+ /**
8
+ * Ensure bucket is initialized
9
+ */
10
+ private getBucket;
6
11
  upload(key: string, file: Buffer, mime_type: string, is_public?: boolean): Promise<{
7
12
  key: string;
8
13
  url: string;
@@ -16,6 +21,9 @@ declare class GCSStorageDriver extends BaseStorageDriver {
16
21
  key: string;
17
22
  url: string;
18
23
  } | null>;
19
- list(prefix?: string): Promise<any>;
24
+ list(prefix?: string): Promise<{
25
+ key: any;
26
+ url: string;
27
+ }[]>;
20
28
  }
21
29
  export default GCSStorageDriver;
@@ -7,6 +7,7 @@ const storage_1 = require("@google-cloud/storage");
7
7
  const base_storage_driver_1 = __importDefault(require("./base_storage_driver"));
8
8
  class GCSStorageDriver extends base_storage_driver_1.default {
9
9
  bucket;
10
+ bucket_name = "";
10
11
  async initialize(config) {
11
12
  if (config.type !== "gcs") {
12
13
  throw new Error("Invalid config for GCS");
@@ -15,10 +16,21 @@ class GCSStorageDriver extends base_storage_driver_1.default {
15
16
  projectId: config.project_id,
16
17
  credentials: config.credentials
17
18
  });
19
+ this.bucket_name = config.bucket_name;
18
20
  this.bucket = storage.bucket(config.bucket_name);
19
21
  }
22
+ /**
23
+ * Ensure bucket is initialized
24
+ */
25
+ getBucket() {
26
+ if (!this.bucket) {
27
+ throw new Error("GCS bucket not initialized");
28
+ }
29
+ return this.bucket;
30
+ }
20
31
  async upload(key, file, mime_type, is_public = true) {
21
- const file_ref = this.bucket.file(key);
32
+ const bucket = this.getBucket();
33
+ const file_ref = bucket.file(key);
22
34
  await file_ref.save(file, {
23
35
  contentType: mime_type
24
36
  });
@@ -33,14 +45,16 @@ class GCSStorageDriver extends base_storage_driver_1.default {
33
45
  };
34
46
  }
35
47
  getPublicUrl(key) {
36
- return `https://storage.googleapis.com/${this.bucket.name}/${key}`;
48
+ return `https://storage.googleapis.com/${this.bucket_name}/${key}`;
37
49
  }
38
50
  async exists(key) {
39
- const [exists] = await this.bucket.file(key).exists();
51
+ const bucket = this.getBucket();
52
+ const [exists] = await bucket.file(key).exists();
40
53
  return exists;
41
54
  }
42
55
  async delete(key) {
43
- await this.bucket.file(key).delete();
56
+ const bucket = this.getBucket();
57
+ await bucket.file(key).delete();
44
58
  }
45
59
  async get(key) {
46
60
  const exists = await this.exists(key);
@@ -52,7 +66,8 @@ class GCSStorageDriver extends base_storage_driver_1.default {
52
66
  };
53
67
  }
54
68
  async list(prefix = "") {
55
- const [files] = await this.bucket.getFiles({ prefix });
69
+ const bucket = this.getBucket();
70
+ const [files] = await bucket.getFiles({ prefix });
56
71
  return files.map((f) => ({
57
72
  key: f.name,
58
73
  url: this.getPublicUrl(f.name)
@@ -66,7 +66,9 @@ class FileUploadProcessor {
66
66
  mime_type: input.mime_type,
67
67
  category,
68
68
  size: input.size,
69
- url: stored.url
69
+ url: stored.url,
70
+ provider: this.driver.constructor.name,
71
+ bucket_name: this.driver?.bucket_name || null
70
72
  };
71
73
  this.logger.info("File uploaded successfully", {
72
74
  key,
@@ -40,6 +40,8 @@ export interface FileUploadDataPayloadInterface {
40
40
  category: FileCategory;
41
41
  size: number;
42
42
  url: string;
43
+ provider?: string | null;
44
+ bucket_name?: string | null;
43
45
  }
44
46
  export interface FileUploadResult {
45
47
  status: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fiberx-backend-toolkit",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
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",