@webtypen/webframez-core 0.3.19 → 0.3.21

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.
@@ -119,6 +119,11 @@ declare class StorageFacade {
119
119
  }>;
120
120
  file(filepath: string): Promise<StorageFileInstance>;
121
121
  readDir(dirpath: string, options: StorageReadDirOptions): Promise<any[]>;
122
+ upload(req: any, options: {
123
+ storagePath: string;
124
+ storageFilename: string;
125
+ payload?: any;
126
+ }): Promise<boolean>;
122
127
  }
123
128
  declare class StorageFileFacade {
124
129
  storage: StorageFacade;
@@ -62,10 +62,11 @@ class StorageFileInstance {
62
62
  }
63
63
  class StorageDisk {
64
64
  constructor(key) {
65
+ var _a;
65
66
  this.diskKey = key;
66
- this.config = Config_1.Config.get("storage.storages." + this.diskKey);
67
- if (!this.config.driver) {
68
- throw new Error(`[STORAGE ERROR] Missing driver for disk '${this.diskKey}'.`);
67
+ this.config = Config_1.Config.get("storage.disks." + this.diskKey);
68
+ if (!((_a = this.config) === null || _a === void 0 ? void 0 : _a.driver)) {
69
+ throw new Error(`[STORAGE ERROR] Missing config / config-driver-key for disk '${this.diskKey}'.`);
69
70
  }
70
71
  if (!exports.StorageDriverRegisty || !exports.StorageDriverRegisty.drivers || !exports.StorageDriverRegisty.drivers[this.config.driver]) {
71
72
  throw new Error(`[STORAGE ERROR] Invalid driver '${this.config.driver}' for disk '${this.diskKey}'.`);
@@ -146,7 +147,7 @@ class StorageFacade {
146
147
  this.drivers = {};
147
148
  }
148
149
  disk(diskKey) {
149
- const key = diskKey !== null && diskKey !== void 0 ? diskKey : Config_1.Config.get("storage.defaultStorage");
150
+ const key = diskKey !== null && diskKey !== void 0 ? diskKey : Config_1.Config.get("storage.defaultDisk");
150
151
  if (this.disks[key]) {
151
152
  return this.disks[key];
152
153
  }
@@ -198,6 +199,11 @@ class StorageFacade {
198
199
  return yield this.disk().readDir(dirpath, options);
199
200
  });
200
201
  }
202
+ upload(req, options) {
203
+ return __awaiter(this, void 0, void 0, function* () {
204
+ return yield this.disk().driver.upload(req, options);
205
+ });
206
+ }
201
207
  }
202
208
  class StorageFileFacade {
203
209
  constructor(storage) {
@@ -44,4 +44,9 @@ export interface BaseStorageDriver {
44
44
  extension(filepath: string): Promise<string | null>;
45
45
  mime(filepath: string): Promise<string | null>;
46
46
  readDir(dirpath: string, options?: StorageReadDirOptions): Promise<any[]>;
47
+ upload(req: any, options: {
48
+ storagePath: string;
49
+ storageFilename: string;
50
+ payload?: any;
51
+ }): Promise<boolean>;
47
52
  }
@@ -1,14 +1,15 @@
1
1
  /// <reference types="node" />
2
2
  import { BaseStorageDriver, StorageDeleteOptions, StorageMkdirOptions, StorageReadDirOptions } from "./BaseStorageDriver";
3
+ import { Request } from "../../Router/Request";
3
4
  export declare class LocalStorageDriver implements BaseStorageDriver {
4
5
  config: any;
5
6
  basepath: string;
6
7
  constructor(conf: any);
7
8
  path(...args: string[]): string;
8
- put(filepath: string, contents: string | Buffer): Promise<{
9
+ put(filepath: string, contents: string | Buffer, payload?: any): Promise<{
9
10
  status: string;
10
11
  filepath: string;
11
- payload: {};
12
+ payload: any;
12
13
  }>;
13
14
  copy(filepath: string, target: string): Promise<{
14
15
  status: string;
@@ -38,4 +39,9 @@ export declare class LocalStorageDriver implements BaseStorageDriver {
38
39
  extension(filepath: string): Promise<string | null>;
39
40
  mime(filepath: string): Promise<string | null>;
40
41
  readDir(dirpath: string, options?: StorageReadDirOptions): Promise<string[]>;
42
+ upload(req: Request, options: {
43
+ storagePath: string;
44
+ storageFilename: string;
45
+ payload?: any;
46
+ }): Promise<boolean>;
41
47
  }
@@ -16,6 +16,7 @@ exports.LocalStorageDriver = void 0;
16
16
  const fs_1 = __importDefault(require("fs"));
17
17
  const path_1 = __importDefault(require("path"));
18
18
  const mime_types_1 = __importDefault(require("mime-types"));
19
+ const busboy_1 = __importDefault(require("busboy"));
19
20
  const FileFunctions_1 = require("../../Functions/FileFunctions");
20
21
  class LocalStorageDriver {
21
22
  constructor(conf) {
@@ -25,13 +26,33 @@ class LocalStorageDriver {
25
26
  path(...args) {
26
27
  return path_1.default.join(this.basepath, ...args);
27
28
  }
28
- put(filepath, contents) {
29
+ put(filepath, contents, payload) {
29
30
  return __awaiter(this, void 0, void 0, function* () {
30
- fs_1.default.writeFileSync(this.path(filepath), contents);
31
+ const p = this.path(filepath);
32
+ // @ts-ignore
33
+ fs_1.default.writeFileSync(p, contents);
34
+ if (this.config.fileHandlers) {
35
+ const keys = Object.keys(this.config.fileHandlers);
36
+ const mime = yield this.mime(p);
37
+ if (mime) {
38
+ const configKey = keys.find((key) => {
39
+ if (key.includes("*")) {
40
+ const regex = new RegExp("^" + key.replace("*", ".+") + "$");
41
+ return regex.test(mime);
42
+ }
43
+ return key === mime;
44
+ });
45
+ if (configKey && this.config.fileHandlers[configKey]) {
46
+ for (let handler of this.config.fileHandlers[configKey]) {
47
+ yield handler(p, contents, payload);
48
+ }
49
+ }
50
+ }
51
+ }
31
52
  return {
32
53
  status: "success",
33
54
  filepath: filepath,
34
- payload: {},
55
+ payload: payload,
35
56
  };
36
57
  });
37
58
  }
@@ -124,5 +145,61 @@ class LocalStorageDriver {
124
145
  return entries && entries.length > 0 ? entries : [];
125
146
  });
126
147
  }
148
+ upload(req, options) {
149
+ return __awaiter(this, void 0, void 0, function* () {
150
+ const busboy = (0, busboy_1.default)({ headers: req.headers });
151
+ const dir = path_1.default.join((0, FileFunctions_1.storageDir)(), options.storagePath);
152
+ if (!fs_1.default.existsSync(dir)) {
153
+ fs_1.default.mkdirSync(dir, { recursive: true });
154
+ }
155
+ let hasSuccess = false;
156
+ const promises = [
157
+ new Promise((resolve) => {
158
+ if (!req.message) {
159
+ throw Error("Missing IncomingMessage ...");
160
+ }
161
+ let fileOptions = null;
162
+ let filepath = null;
163
+ busboy.on("file", (fieldname, file, options) => __awaiter(this, void 0, void 0, function* () {
164
+ if (fileOptions === null && options) {
165
+ fileOptions = options;
166
+ }
167
+ filepath = path_1.default.join(dir, options.storageFilename);
168
+ const writeStream = fs_1.default.createWriteStream(filepath);
169
+ file.pipe(writeStream);
170
+ }));
171
+ busboy.on("error", (error, error2) => {
172
+ console.error("Busboy error:", error);
173
+ resolve(false);
174
+ });
175
+ busboy.on("finish", () => __awaiter(this, void 0, void 0, function* () {
176
+ if (this.config.fileHandlers) {
177
+ const keys = Object.keys(this.config.fileHandlers);
178
+ const mime = yield this.mime(filepath);
179
+ if (mime) {
180
+ const configKey = keys.find((key) => {
181
+ if (key.includes("*")) {
182
+ const regex = new RegExp("^" + key.replace("*", ".+") + "$");
183
+ return regex.test(mime);
184
+ }
185
+ return key === mime;
186
+ });
187
+ if (configKey && this.config.fileHandlers[configKey]) {
188
+ for (let handler of this.config.fileHandlers[configKey]) {
189
+ yield handler(filepath, fs_1.default.readFileSync(filepath), options.payload);
190
+ }
191
+ }
192
+ }
193
+ }
194
+ hasSuccess = true;
195
+ resolve(true);
196
+ }));
197
+ req.message.pipe(busboy);
198
+ }),
199
+ ];
200
+ yield Promise.all(promises);
201
+ return hasSuccess;
202
+ });
203
+ }
127
204
  }
128
205
  exports.LocalStorageDriver = LocalStorageDriver;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.3.19",
3
+ "version": "0.3.21",
4
4
  "description": "The core package of the typescript based webframez (web-framework) by webtypen.de",
5
5
  "homepage": "https://webtypen.de/",
6
6
  "license": "MIT",
@@ -54,9 +54,9 @@ class StorageDisk {
54
54
 
55
55
  constructor(key: string) {
56
56
  this.diskKey = key;
57
- this.config = Config.get("storage.storages." + this.diskKey);
58
- if (!this.config.driver) {
59
- throw new Error(`[STORAGE ERROR] Missing driver for disk '${this.diskKey}'.`);
57
+ this.config = Config.get("storage.disks." + this.diskKey);
58
+ if (!this.config?.driver) {
59
+ throw new Error(`[STORAGE ERROR] Missing config / config-driver-key for disk '${this.diskKey}'.`);
60
60
  }
61
61
 
62
62
  if (!StorageDriverRegisty || !StorageDriverRegisty.drivers || !StorageDriverRegisty.drivers[this.config.driver]) {
@@ -126,7 +126,7 @@ class StorageFacade {
126
126
  drivers: { [key: string]: any } = {};
127
127
 
128
128
  disk(diskKey?: string) {
129
- const key = diskKey ?? Config.get("storage.defaultStorage");
129
+ const key = diskKey ?? Config.get("storage.defaultDisk");
130
130
  if (this.disks[key]) {
131
131
  return this.disks[key];
132
132
  }
@@ -170,6 +170,10 @@ class StorageFacade {
170
170
  async readDir(dirpath: string, options: StorageReadDirOptions) {
171
171
  return await this.disk().readDir(dirpath, options);
172
172
  }
173
+
174
+ async upload(req: any, options: { storagePath: string; storageFilename: string; payload?: any }) {
175
+ return await this.disk().driver.upload(req, options);
176
+ }
173
177
  }
174
178
 
175
179
  class StorageFileFacade {
@@ -27,4 +27,5 @@ export interface BaseStorageDriver {
27
27
  extension(filepath: string): Promise<string | null>;
28
28
  mime(filepath: string): Promise<string | null>;
29
29
  readDir(dirpath: string, options?: StorageReadDirOptions): Promise<any[]>;
30
+ upload(req: any, options: { storagePath: string; storageFilename: string; payload?: any }): Promise<boolean>;
30
31
  }
@@ -1,8 +1,10 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
3
  import mime from "mime-types";
4
+ import Busboy from "busboy";
4
5
  import { BaseStorageDriver, StorageDeleteOptions, StorageMkdirOptions, StorageReadDirOptions } from "./BaseStorageDriver";
5
6
  import { storageDir } from "../../Functions/FileFunctions";
7
+ import { Request } from "../../Router/Request";
6
8
 
7
9
  export class LocalStorageDriver implements BaseStorageDriver {
8
10
  config: any;
@@ -17,13 +19,35 @@ export class LocalStorageDriver implements BaseStorageDriver {
17
19
  return path.join(this.basepath, ...args);
18
20
  }
19
21
 
20
- async put(filepath: string, contents: string | Buffer) {
21
- fs.writeFileSync(this.path(filepath), contents);
22
+ async put(filepath: string, contents: string | Buffer, payload?: any) {
23
+ const p = this.path(filepath);
24
+ // @ts-ignore
25
+ fs.writeFileSync(p, contents);
26
+
27
+ if (this.config.fileHandlers) {
28
+ const keys = Object.keys(this.config.fileHandlers);
29
+ const mime = await this.mime(p);
30
+ if (mime) {
31
+ const configKey = keys.find((key) => {
32
+ if (key.includes("*")) {
33
+ const regex = new RegExp("^" + key.replace("*", ".+") + "$");
34
+ return regex.test(mime);
35
+ }
36
+ return key === mime;
37
+ });
38
+
39
+ if (configKey && this.config.fileHandlers[configKey]) {
40
+ for (let handler of this.config.fileHandlers[configKey]) {
41
+ await handler(p, contents, payload);
42
+ }
43
+ }
44
+ }
45
+ }
22
46
 
23
47
  return {
24
48
  status: "success",
25
49
  filepath: filepath,
26
- payload: {},
50
+ payload: payload,
27
51
  };
28
52
  }
29
53
 
@@ -107,4 +131,68 @@ export class LocalStorageDriver implements BaseStorageDriver {
107
131
  const entries = fs.readdirSync(this.path(dirpath));
108
132
  return entries && entries.length > 0 ? entries : [];
109
133
  }
134
+
135
+ async upload(req: Request, options: { storagePath: string; storageFilename: string; payload?: any }) {
136
+ const busboy = Busboy({ headers: req.headers });
137
+ const dir = path.join(storageDir(), options.storagePath);
138
+ if (!fs.existsSync(dir)) {
139
+ fs.mkdirSync(dir, { recursive: true });
140
+ }
141
+
142
+ let hasSuccess = false;
143
+ const promises = [
144
+ new Promise((resolve) => {
145
+ if (!req.message) {
146
+ throw Error("Missing IncomingMessage ...");
147
+ }
148
+
149
+ let fileOptions: any = null;
150
+ let filepath: any = null;
151
+ busboy.on("file", async (fieldname: any, file: any, options: any) => {
152
+ if (fileOptions === null && options) {
153
+ fileOptions = options;
154
+ }
155
+
156
+ filepath = path.join(dir, options.storageFilename);
157
+ const writeStream = fs.createWriteStream(filepath);
158
+ file.pipe(writeStream);
159
+ });
160
+
161
+ busboy.on("error", (error: any, error2: any) => {
162
+ console.error("Busboy error:", error);
163
+ resolve(false);
164
+ });
165
+
166
+ busboy.on("finish", async () => {
167
+ if (this.config.fileHandlers) {
168
+ const keys = Object.keys(this.config.fileHandlers);
169
+ const mime = await this.mime(filepath);
170
+ if (mime) {
171
+ const configKey = keys.find((key) => {
172
+ if (key.includes("*")) {
173
+ const regex = new RegExp("^" + key.replace("*", ".+") + "$");
174
+ return regex.test(mime);
175
+ }
176
+ return key === mime;
177
+ });
178
+
179
+ if (configKey && this.config.fileHandlers[configKey]) {
180
+ for (let handler of this.config.fileHandlers[configKey]) {
181
+ await handler(filepath, fs.readFileSync(filepath), options.payload);
182
+ }
183
+ }
184
+ }
185
+ }
186
+
187
+ hasSuccess = true;
188
+ resolve(true);
189
+ });
190
+
191
+ req.message.pipe(busboy);
192
+ }),
193
+ ];
194
+ await Promise.all(promises);
195
+
196
+ return hasSuccess;
197
+ }
110
198
  }