@webtypen/webframez-core 0.1.15 → 0.1.17

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,143 @@
1
+ /// <reference types="node" />
2
+ import { BaseStorageDriver, StorageDeleteOptions, StorageMkdirOptions, StorageReadDirOptions } from "./StorageDrivers/BaseStorageDriver";
3
+ declare class StorageDriverRegistyFacade {
4
+ drivers: {
5
+ [key: string]: any;
6
+ };
7
+ constructor();
8
+ }
9
+ export declare const StorageDriverRegisty: StorageDriverRegistyFacade;
10
+ declare class StorageFileInstance {
11
+ driver: BaseStorageDriver;
12
+ filepath: string;
13
+ constructor(filepath: string, driver: any);
14
+ exists(): Promise<boolean>;
15
+ isDir(): Promise<boolean>;
16
+ isFile(): Promise<boolean>;
17
+ delete(options?: StorageDeleteOptions): Promise<{
18
+ status: string;
19
+ filepath: string | string[];
20
+ payload?: any;
21
+ }>;
22
+ extension(options?: StorageDeleteOptions): Promise<{
23
+ status: string;
24
+ filepath: string | string[];
25
+ payload?: any;
26
+ }>;
27
+ mime(): Promise<string | null>;
28
+ }
29
+ declare class StorageDisk {
30
+ diskKey: string;
31
+ config: any;
32
+ driver: BaseStorageDriver;
33
+ constructor(key: string);
34
+ generateUploadFilepath(): string;
35
+ put(contents: string | Buffer, filepath?: string): Promise<{
36
+ status: string;
37
+ filepath: string;
38
+ payload?: any;
39
+ }>;
40
+ copy(filepath: string, target: string): Promise<{
41
+ status: string;
42
+ filepath: string;
43
+ origin_path: string;
44
+ payload?: any;
45
+ }>;
46
+ move(filepath: string, target: string): Promise<{
47
+ status: string;
48
+ filepath: string;
49
+ old_filepath: string;
50
+ payload?: any;
51
+ }>;
52
+ delete(filepath: string | string[], options?: StorageDeleteOptions): Promise<{
53
+ status: string;
54
+ filepath: string | string[];
55
+ payload?: any;
56
+ }>;
57
+ mkdir(filepath: string, options?: StorageMkdirOptions): Promise<{
58
+ status: string;
59
+ dirpath: string;
60
+ payload?: any;
61
+ }>;
62
+ isDir(filepath: string): Promise<{
63
+ status: string;
64
+ dirpath: string;
65
+ payload?: any;
66
+ }>;
67
+ isFile(filepath: string): Promise<{
68
+ status: string;
69
+ dirpath: string;
70
+ payload?: any;
71
+ }>;
72
+ file(filepath: string): Promise<StorageFileInstance>;
73
+ readDir(dirpath: string, options: StorageReadDirOptions): Promise<any[]>;
74
+ }
75
+ declare class StorageFacade {
76
+ disks: {
77
+ [key: string]: StorageDisk;
78
+ };
79
+ drivers: {
80
+ [key: string]: any;
81
+ };
82
+ disk(diskKey?: string): StorageDisk;
83
+ put(contents: string | Buffer, filepath?: string): Promise<{
84
+ status: string;
85
+ filepath: string;
86
+ payload?: any;
87
+ }>;
88
+ copy(filepath: string, target: string): Promise<{
89
+ status: string;
90
+ filepath: string;
91
+ origin_path: string;
92
+ payload?: any;
93
+ }>;
94
+ move(filepath: string, target: string): Promise<{
95
+ status: string;
96
+ filepath: string;
97
+ old_filepath: string;
98
+ payload?: any;
99
+ }>;
100
+ delete(filepath: string | string[], options: StorageDeleteOptions): Promise<{
101
+ status: string;
102
+ filepath: string | string[];
103
+ payload?: any;
104
+ }>;
105
+ mkdir(filepath: string, options: StorageMkdirOptions): Promise<{
106
+ status: string;
107
+ dirpath: string;
108
+ payload?: any;
109
+ }>;
110
+ isDir(filepath: string): Promise<{
111
+ status: string;
112
+ dirpath: string;
113
+ payload?: any;
114
+ }>;
115
+ isFile(filepath: string): Promise<{
116
+ status: string;
117
+ dirpath: string;
118
+ payload?: any;
119
+ }>;
120
+ file(filepath: string): Promise<StorageFileInstance>;
121
+ readDir(dirpath: string, options: StorageReadDirOptions): Promise<any[]>;
122
+ }
123
+ declare class StorageFileFacade {
124
+ storage: StorageFacade;
125
+ constructor(storage: StorageFacade);
126
+ exists(filepath: string, diskKey?: string): Promise<boolean>;
127
+ isDir(filepath: string, diskKey?: string): Promise<boolean>;
128
+ isFile(filepath: string, diskKey?: string): Promise<boolean>;
129
+ delete(filepath: string, options?: StorageDeleteOptions, diskKey?: string): Promise<{
130
+ status: string;
131
+ filepath: string | string[];
132
+ payload?: any;
133
+ }>;
134
+ extension(filepath: string, diskKey?: string): Promise<{
135
+ status: string;
136
+ filepath: string | string[];
137
+ payload?: any;
138
+ }>;
139
+ mime(filepath: string, diskKey?: string): Promise<string | null>;
140
+ }
141
+ export declare const Storage: StorageFacade;
142
+ export declare const StorageFile: StorageFileFacade;
143
+ export {};
@@ -0,0 +1,238 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.StorageFile = exports.Storage = exports.StorageDriverRegisty = void 0;
16
+ const path_1 = __importDefault(require("path"));
17
+ const Config_1 = require("../Config");
18
+ const LocalStorageDriver_1 = require("./StorageDrivers/LocalStorageDriver");
19
+ const StringFunctions_1 = require("../Functions/StringFunctions");
20
+ class StorageDriverRegistyFacade {
21
+ constructor() {
22
+ this.drivers = {};
23
+ this.drivers.local = LocalStorageDriver_1.LocalStorageDriver;
24
+ }
25
+ }
26
+ exports.StorageDriverRegisty = new StorageDriverRegistyFacade();
27
+ class StorageFileInstance {
28
+ constructor(filepath, driver) {
29
+ this.driver = driver;
30
+ this.filepath = filepath;
31
+ }
32
+ exists() {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ return this.driver.exists(this.filepath);
35
+ });
36
+ }
37
+ isDir() {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ return this.driver.isDir(this.filepath);
40
+ });
41
+ }
42
+ isFile() {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ return this.driver.isFile(this.filepath);
45
+ });
46
+ }
47
+ delete(options) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ return this.driver.delete(this.filepath, options);
50
+ });
51
+ }
52
+ extension(options) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ return this.driver.delete(this.filepath, options);
55
+ });
56
+ }
57
+ mime() {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ return this.driver.mime(this.filepath);
60
+ });
61
+ }
62
+ }
63
+ class StorageDisk {
64
+ constructor(key) {
65
+ 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}'.`);
69
+ }
70
+ if (!exports.StorageDriverRegisty || !exports.StorageDriverRegisty.drivers || !exports.StorageDriverRegisty.drivers[this.config.driver]) {
71
+ throw new Error(`[STORAGE ERROR] Invalid driver '${this.config.driver}' for disk '${this.diskKey}'.`);
72
+ }
73
+ this.driver = new exports.StorageDriverRegisty.drivers[this.config.driver](this.config);
74
+ }
75
+ generateUploadFilepath() {
76
+ const date = new Date();
77
+ return path_1.default.join(this.config.uploadsDir && this.config.uploadsDir.trim() !== "" ? this.config.uploadsDir : "uploads", date.getFullYear().toString(), date.getMonth().toString() + "-" + date.getDate().toString(), date.valueOf().toString() + "_" + StringFunctions_1.StringFunctions.random(24));
78
+ }
79
+ put(contents, filepath) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ if (!this.driver)
82
+ throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
83
+ return yield this.driver.put(filepath && filepath.trim() !== "" ? filepath : this.generateUploadFilepath(), contents);
84
+ });
85
+ }
86
+ copy(filepath, target) {
87
+ return __awaiter(this, void 0, void 0, function* () {
88
+ if (!this.driver)
89
+ throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
90
+ return yield this.driver.copy(filepath, target);
91
+ });
92
+ }
93
+ move(filepath, target) {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ if (!this.driver)
96
+ throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
97
+ return yield this.driver.move(filepath, target);
98
+ });
99
+ }
100
+ delete(filepath, options) {
101
+ return __awaiter(this, void 0, void 0, function* () {
102
+ if (!this.driver)
103
+ throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
104
+ return yield this.driver.delete(filepath, options);
105
+ });
106
+ }
107
+ mkdir(filepath, options) {
108
+ return __awaiter(this, void 0, void 0, function* () {
109
+ if (!this.driver)
110
+ throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
111
+ return yield this.driver.mkdir(filepath, options);
112
+ });
113
+ }
114
+ isDir(filepath) {
115
+ return __awaiter(this, void 0, void 0, function* () {
116
+ if (!this.driver)
117
+ throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
118
+ return yield this.driver.mkdir(filepath);
119
+ });
120
+ }
121
+ isFile(filepath) {
122
+ return __awaiter(this, void 0, void 0, function* () {
123
+ if (!this.driver)
124
+ throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
125
+ return yield this.driver.mkdir(filepath);
126
+ });
127
+ }
128
+ file(filepath) {
129
+ return __awaiter(this, void 0, void 0, function* () {
130
+ if (!this.driver)
131
+ throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
132
+ return new StorageFileInstance(filepath, this.driver);
133
+ });
134
+ }
135
+ readDir(dirpath, options) {
136
+ return __awaiter(this, void 0, void 0, function* () {
137
+ if (!this.driver)
138
+ throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
139
+ return yield this.driver.readDir(dirpath, options);
140
+ });
141
+ }
142
+ }
143
+ class StorageFacade {
144
+ constructor() {
145
+ this.disks = {};
146
+ this.drivers = {};
147
+ }
148
+ disk(diskKey) {
149
+ const key = diskKey !== null && diskKey !== void 0 ? diskKey : Config_1.Config.get("storage.defaultStorage");
150
+ if (this.disks[key]) {
151
+ return this.disks[key];
152
+ }
153
+ this.disks[key] = new StorageDisk(key);
154
+ return this.disks[key];
155
+ }
156
+ put(contents, filepath) {
157
+ return __awaiter(this, void 0, void 0, function* () {
158
+ return yield this.disk().put(contents, filepath);
159
+ });
160
+ }
161
+ copy(filepath, target) {
162
+ return __awaiter(this, void 0, void 0, function* () {
163
+ return yield this.disk().copy(filepath, target);
164
+ });
165
+ }
166
+ move(filepath, target) {
167
+ return __awaiter(this, void 0, void 0, function* () {
168
+ return yield this.disk().move(filepath, target);
169
+ });
170
+ }
171
+ delete(filepath, options) {
172
+ return __awaiter(this, void 0, void 0, function* () {
173
+ return yield this.disk().delete(filepath, options);
174
+ });
175
+ }
176
+ mkdir(filepath, options) {
177
+ return __awaiter(this, void 0, void 0, function* () {
178
+ return yield this.disk().mkdir(filepath, options);
179
+ });
180
+ }
181
+ isDir(filepath) {
182
+ return __awaiter(this, void 0, void 0, function* () {
183
+ return yield this.disk().isDir(filepath);
184
+ });
185
+ }
186
+ isFile(filepath) {
187
+ return __awaiter(this, void 0, void 0, function* () {
188
+ return yield this.disk().isFile(filepath);
189
+ });
190
+ }
191
+ file(filepath) {
192
+ return __awaiter(this, void 0, void 0, function* () {
193
+ return yield this.disk().file(filepath);
194
+ });
195
+ }
196
+ readDir(dirpath, options) {
197
+ return __awaiter(this, void 0, void 0, function* () {
198
+ return yield this.disk().readDir(dirpath, options);
199
+ });
200
+ }
201
+ }
202
+ class StorageFileFacade {
203
+ constructor(storage) {
204
+ this.storage = storage;
205
+ }
206
+ exists(filepath, diskKey) {
207
+ return __awaiter(this, void 0, void 0, function* () {
208
+ return new StorageFileInstance(filepath, this.storage.disk(diskKey)).exists();
209
+ });
210
+ }
211
+ isDir(filepath, diskKey) {
212
+ return __awaiter(this, void 0, void 0, function* () {
213
+ return new StorageFileInstance(filepath, this.storage.disk(diskKey)).isDir();
214
+ });
215
+ }
216
+ isFile(filepath, diskKey) {
217
+ return __awaiter(this, void 0, void 0, function* () {
218
+ return new StorageFileInstance(filepath, this.storage.disk(diskKey)).isFile();
219
+ });
220
+ }
221
+ delete(filepath, options, diskKey) {
222
+ return __awaiter(this, void 0, void 0, function* () {
223
+ return new StorageFileInstance(filepath, this.storage.disk(diskKey)).delete(options);
224
+ });
225
+ }
226
+ extension(filepath, diskKey) {
227
+ return __awaiter(this, void 0, void 0, function* () {
228
+ return new StorageFileInstance(filepath, this.storage.disk(diskKey)).extension();
229
+ });
230
+ }
231
+ mime(filepath, diskKey) {
232
+ return __awaiter(this, void 0, void 0, function* () {
233
+ return new StorageFileInstance(filepath, this.storage.disk(diskKey)).mime();
234
+ });
235
+ }
236
+ }
237
+ exports.Storage = new StorageFacade();
238
+ exports.StorageFile = new StorageFileFacade(exports.Storage);
@@ -0,0 +1,47 @@
1
+ /// <reference types="node" />
2
+ export type StorageDeleteOptions = {
3
+ recursive?: boolean;
4
+ force?: boolean;
5
+ };
6
+ export type StorageMkdirOptions = {
7
+ recursive?: boolean;
8
+ };
9
+ export type StorageReadDirOptions = {
10
+ onyFiles?: boolean;
11
+ onlyDirs?: boolean;
12
+ };
13
+ export interface BaseStorageDriver {
14
+ put(filepath: string, contents: string | Buffer): Promise<{
15
+ status: string;
16
+ filepath: string;
17
+ payload?: any;
18
+ }>;
19
+ copy(filepath: string, target: string): Promise<{
20
+ status: string;
21
+ filepath: string;
22
+ origin_path: string;
23
+ payload?: any;
24
+ }>;
25
+ move(filepath: string, target: string): Promise<{
26
+ status: string;
27
+ filepath: string;
28
+ old_filepath: string;
29
+ payload?: any;
30
+ }>;
31
+ delete(filepath: string | string[], options?: StorageDeleteOptions): Promise<{
32
+ status: string;
33
+ filepath: string | string[];
34
+ payload?: any;
35
+ }>;
36
+ mkdir(dirpath: string, options?: StorageMkdirOptions): Promise<{
37
+ status: string;
38
+ dirpath: string;
39
+ payload?: any;
40
+ }>;
41
+ exists(filepath: string): Promise<boolean>;
42
+ isDir(dirpath: string): Promise<boolean>;
43
+ isFile(filepath: string): Promise<boolean>;
44
+ extension(filepath: string): Promise<string | null>;
45
+ mime(filepath: string): Promise<string | null>;
46
+ readDir(dirpath: string, options?: StorageReadDirOptions): Promise<any[]>;
47
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,41 @@
1
+ /// <reference types="node" />
2
+ import { BaseStorageDriver, StorageDeleteOptions, StorageMkdirOptions, StorageReadDirOptions } from "./BaseStorageDriver";
3
+ export declare class LocalStorageDriver implements BaseStorageDriver {
4
+ config: any;
5
+ basepath: string;
6
+ constructor(conf: any);
7
+ path(...args: string[]): string;
8
+ put(filepath: string, contents: string | Buffer): Promise<{
9
+ status: string;
10
+ filepath: string;
11
+ payload: {};
12
+ }>;
13
+ copy(filepath: string, target: string): Promise<{
14
+ status: string;
15
+ filepath: string;
16
+ origin_path: string;
17
+ payload: {};
18
+ }>;
19
+ move(filepath: string, target: string): Promise<{
20
+ status: string;
21
+ filepath: string;
22
+ old_filepath: string;
23
+ payload: {};
24
+ }>;
25
+ delete(filepath: string | string[], options?: StorageDeleteOptions): Promise<{
26
+ status: string;
27
+ filepath: string | string[];
28
+ payload: {};
29
+ }>;
30
+ mkdir(dirpath: string, options?: StorageMkdirOptions): Promise<{
31
+ status: string;
32
+ dirpath: string;
33
+ payload: {};
34
+ }>;
35
+ exists(filepath: string): Promise<boolean>;
36
+ isDir(dirpath: string): Promise<boolean>;
37
+ isFile(filepath: string): Promise<boolean>;
38
+ extension(filepath: string): Promise<string | null>;
39
+ mime(filepath: string): Promise<string | null>;
40
+ readDir(dirpath: string, options?: StorageReadDirOptions): Promise<string[]>;
41
+ }
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.LocalStorageDriver = void 0;
16
+ const fs_1 = __importDefault(require("fs"));
17
+ const path_1 = __importDefault(require("path"));
18
+ const mime_types_1 = __importDefault(require("mime-types"));
19
+ const FileFunctions_1 = require("../../Functions/FileFunctions");
20
+ class LocalStorageDriver {
21
+ constructor(conf) {
22
+ this.config = conf;
23
+ this.basepath = conf.basepath && conf.basepath.trim() !== "" ? conf.basepath : (0, FileFunctions_1.storageDir)();
24
+ }
25
+ path(...args) {
26
+ return path_1.default.join(this.basepath, ...args);
27
+ }
28
+ put(filepath, contents) {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ fs_1.default.writeFileSync(this.path(filepath), contents);
31
+ return {
32
+ status: "success",
33
+ filepath: filepath,
34
+ payload: {},
35
+ };
36
+ });
37
+ }
38
+ copy(filepath, target) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ fs_1.default.cpSync(this.path(filepath), target);
41
+ return {
42
+ status: "success",
43
+ filepath: target,
44
+ origin_path: filepath,
45
+ payload: {},
46
+ };
47
+ });
48
+ }
49
+ move(filepath, target) {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ fs_1.default.renameSync(this.path(filepath), target);
52
+ return {
53
+ status: "success",
54
+ filepath: target,
55
+ old_filepath: filepath,
56
+ payload: {},
57
+ };
58
+ });
59
+ }
60
+ delete(filepath, options) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ if (Array.isArray(filepath)) {
63
+ for (let el of filepath) {
64
+ if (options === null || options === void 0 ? void 0 : options.recursive) {
65
+ fs_1.default.rmSync(this.path(el), { recursive: true, force: options.force ? true : false });
66
+ }
67
+ else {
68
+ fs_1.default.rmSync(this.path(el), { recursive: true, force: (options === null || options === void 0 ? void 0 : options.force) ? true : false });
69
+ }
70
+ }
71
+ }
72
+ else {
73
+ fs_1.default.rmSync(this.path(filepath), { recursive: (options === null || options === void 0 ? void 0 : options.recursive) ? true : false, force: (options === null || options === void 0 ? void 0 : options.force) ? true : false });
74
+ }
75
+ return {
76
+ status: "success",
77
+ filepath: filepath,
78
+ payload: {},
79
+ };
80
+ });
81
+ }
82
+ mkdir(dirpath, options) {
83
+ return __awaiter(this, void 0, void 0, function* () {
84
+ fs_1.default.mkdirSync(this.path(dirpath));
85
+ return {
86
+ status: "success",
87
+ dirpath: dirpath,
88
+ payload: {},
89
+ };
90
+ });
91
+ }
92
+ exists(filepath) {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ return fs_1.default.existsSync(this.path(filepath));
95
+ });
96
+ }
97
+ isDir(dirpath) {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ const stats = fs_1.default.lstatSync(this.path(dirpath));
100
+ return stats.isDirectory();
101
+ });
102
+ }
103
+ isFile(filepath) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ const stats = fs_1.default.lstatSync(this.path(filepath));
106
+ return stats.isFile();
107
+ });
108
+ }
109
+ extension(filepath) {
110
+ return __awaiter(this, void 0, void 0, function* () {
111
+ const temp = filepath.indexOf(".") > 0 ? filepath.substring(filepath.lastIndexOf(".") + 1) : null;
112
+ return temp && temp.trim() !== "" ? temp.trim() : null;
113
+ });
114
+ }
115
+ mime(filepath) {
116
+ return __awaiter(this, void 0, void 0, function* () {
117
+ const temp = mime_types_1.default.lookup(this.path(filepath));
118
+ return temp ? temp : null;
119
+ });
120
+ }
121
+ readDir(dirpath, options) {
122
+ return __awaiter(this, void 0, void 0, function* () {
123
+ const entries = fs_1.default.readdirSync(this.path(dirpath));
124
+ return entries && entries.length > 0 ? entries : [];
125
+ });
126
+ }
127
+ }
128
+ exports.LocalStorageDriver = LocalStorageDriver;
package/dist/index.d.ts CHANGED
@@ -18,3 +18,5 @@ export * from "./Router/Response";
18
18
  export * from "./Database/BaseDBDriver";
19
19
  export * from "./Database/DBDrivers";
20
20
  export * from "./Database/QueryBuilder";
21
+ export * from "./Facades/StorageDrivers/BaseStorageDriver";
22
+ export * from "./Facades/Storage";
package/dist/index.js CHANGED
@@ -34,3 +34,5 @@ __exportStar(require("./Router/Response"), exports);
34
34
  __exportStar(require("./Database/BaseDBDriver"), exports);
35
35
  __exportStar(require("./Database/DBDrivers"), exports);
36
36
  __exportStar(require("./Database/QueryBuilder"), exports);
37
+ __exportStar(require("./Facades/StorageDrivers/BaseStorageDriver"), exports);
38
+ __exportStar(require("./Facades/Storage"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
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",
package/src/Config.ts CHANGED
@@ -1,37 +1,37 @@
1
1
  import lodash from "lodash";
2
2
 
3
3
  type ConfigType = {
4
- key: string;
5
- component: any;
4
+ key: string;
5
+ component: any;
6
6
  };
7
7
 
8
8
  class ConfigFacade {
9
- configs: { [key: string]: any } = {};
9
+ configs: { [key: string]: any } = {};
10
10
 
11
- register(key: string | ConfigType[], component: { [key: string]: any }) {
12
- if (typeof key === "string") {
13
- this.configs[key] = component;
14
- } else if (Array.isArray(key)) {
15
- for (let conf of key) {
16
- if (!conf.key || !conf.component) {
17
- continue;
11
+ register(key: string | ConfigType[], component: { [key: string]: any }) {
12
+ if (typeof key === "string") {
13
+ this.configs[key] = component;
14
+ } else if (Array.isArray(key)) {
15
+ for (let conf of key) {
16
+ if (!conf.key || !conf.component) {
17
+ continue;
18
+ }
19
+ this.configs[conf.key] = conf.component;
20
+ }
18
21
  }
19
- this.configs[conf.key] = conf.component;
20
- }
22
+ return this;
21
23
  }
22
- return this;
23
- }
24
24
 
25
- get(key: string) {
26
- return lodash.get(this.configs, key);
27
- }
25
+ get(key: string) {
26
+ return lodash.get(this.configs, key);
27
+ }
28
28
 
29
- set(path: string, value: any | null | undefined) {
30
- const newConfig = { ...this.configs };
31
- lodash.set(newConfig, path, value);
32
- this.configs = newConfig;
33
- return this;
34
- }
29
+ set(path: string, value: any | null | undefined) {
30
+ const newConfig = { ...this.configs };
31
+ lodash.set(newConfig, path, value);
32
+ this.configs = newConfig;
33
+ return this;
34
+ }
35
35
  }
36
36
 
37
37
  export const Config = new ConfigFacade();
@@ -0,0 +1,208 @@
1
+ import path from "path";
2
+ import { Config } from "../Config";
3
+ import { BaseStorageDriver, StorageDeleteOptions, StorageMkdirOptions, StorageReadDirOptions } from "./StorageDrivers/BaseStorageDriver";
4
+ import { LocalStorageDriver } from "./StorageDrivers/LocalStorageDriver";
5
+ import { StringFunctions } from "../Functions/StringFunctions";
6
+
7
+ class StorageDriverRegistyFacade {
8
+ drivers: { [key: string]: any } = {};
9
+
10
+ constructor() {
11
+ this.drivers.local = LocalStorageDriver;
12
+ }
13
+ }
14
+ export const StorageDriverRegisty = new StorageDriverRegistyFacade();
15
+
16
+ class StorageFileInstance {
17
+ driver: BaseStorageDriver;
18
+ filepath: string;
19
+
20
+ constructor(filepath: string, driver: any) {
21
+ this.driver = driver;
22
+ this.filepath = filepath;
23
+ }
24
+
25
+ async exists() {
26
+ return this.driver.exists(this.filepath);
27
+ }
28
+
29
+ async isDir() {
30
+ return this.driver.isDir(this.filepath);
31
+ }
32
+
33
+ async isFile() {
34
+ return this.driver.isFile(this.filepath);
35
+ }
36
+
37
+ async delete(options?: StorageDeleteOptions) {
38
+ return this.driver.delete(this.filepath, options);
39
+ }
40
+
41
+ async extension(options?: StorageDeleteOptions) {
42
+ return this.driver.delete(this.filepath, options);
43
+ }
44
+
45
+ async mime() {
46
+ return this.driver.mime(this.filepath);
47
+ }
48
+ }
49
+
50
+ class StorageDisk {
51
+ diskKey: string;
52
+ config: any;
53
+ driver: BaseStorageDriver;
54
+
55
+ constructor(key: string) {
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}'.`);
60
+ }
61
+
62
+ if (!StorageDriverRegisty || !StorageDriverRegisty.drivers || !StorageDriverRegisty.drivers[this.config.driver]) {
63
+ throw new Error(`[STORAGE ERROR] Invalid driver '${this.config.driver}' for disk '${this.diskKey}'.`);
64
+ }
65
+ this.driver = new StorageDriverRegisty.drivers[this.config.driver](this.config);
66
+ }
67
+
68
+ generateUploadFilepath() {
69
+ const date = new Date();
70
+ return path.join(
71
+ this.config.uploadsDir && this.config.uploadsDir.trim() !== "" ? this.config.uploadsDir : "uploads",
72
+ date.getFullYear().toString(),
73
+ date.getMonth().toString() + "-" + date.getDate().toString(),
74
+ date.valueOf().toString() + "_" + StringFunctions.random(24)
75
+ );
76
+ }
77
+
78
+ async put(contents: string | Buffer, filepath?: string) {
79
+ if (!this.driver) throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
80
+ return await this.driver.put(filepath && filepath.trim() !== "" ? filepath : this.generateUploadFilepath(), contents);
81
+ }
82
+
83
+ async copy(filepath: string, target: string) {
84
+ if (!this.driver) throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
85
+ return await this.driver.copy(filepath, target);
86
+ }
87
+
88
+ async move(filepath: string, target: string) {
89
+ if (!this.driver) throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
90
+ return await this.driver.move(filepath, target);
91
+ }
92
+
93
+ async delete(filepath: string | string[], options?: StorageDeleteOptions) {
94
+ if (!this.driver) throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
95
+ return await this.driver.delete(filepath, options);
96
+ }
97
+
98
+ async mkdir(filepath: string, options?: StorageMkdirOptions) {
99
+ if (!this.driver) throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
100
+ return await this.driver.mkdir(filepath, options);
101
+ }
102
+
103
+ async isDir(filepath: string) {
104
+ if (!this.driver) throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
105
+ return await this.driver.mkdir(filepath);
106
+ }
107
+
108
+ async isFile(filepath: string) {
109
+ if (!this.driver) throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
110
+ return await this.driver.mkdir(filepath);
111
+ }
112
+
113
+ async file(filepath: string) {
114
+ if (!this.driver) throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
115
+ return new StorageFileInstance(filepath, this.driver);
116
+ }
117
+
118
+ async readDir(dirpath: string, options: StorageReadDirOptions) {
119
+ if (!this.driver) throw new Error(`[STORAGE ERROR] No driver configured for disk '${this.diskKey}'.`);
120
+ return await this.driver.readDir(dirpath, options);
121
+ }
122
+ }
123
+
124
+ class StorageFacade {
125
+ disks: { [key: string]: StorageDisk } = {};
126
+ drivers: { [key: string]: any } = {};
127
+
128
+ disk(diskKey?: string) {
129
+ const key = diskKey ?? Config.get("storage.defaultStorage");
130
+ if (this.disks[key]) {
131
+ return this.disks[key];
132
+ }
133
+
134
+ this.disks[key] = new StorageDisk(key);
135
+ return this.disks[key];
136
+ }
137
+
138
+ async put(contents: string | Buffer, filepath?: string) {
139
+ return await this.disk().put(contents, filepath);
140
+ }
141
+
142
+ async copy(filepath: string, target: string) {
143
+ return await this.disk().copy(filepath, target);
144
+ }
145
+
146
+ async move(filepath: string, target: string) {
147
+ return await this.disk().move(filepath, target);
148
+ }
149
+
150
+ async delete(filepath: string | string[], options: StorageDeleteOptions) {
151
+ return await this.disk().delete(filepath, options);
152
+ }
153
+
154
+ async mkdir(filepath: string, options: StorageMkdirOptions) {
155
+ return await this.disk().mkdir(filepath, options);
156
+ }
157
+
158
+ async isDir(filepath: string) {
159
+ return await this.disk().isDir(filepath);
160
+ }
161
+
162
+ async isFile(filepath: string) {
163
+ return await this.disk().isFile(filepath);
164
+ }
165
+
166
+ async file(filepath: string) {
167
+ return await this.disk().file(filepath);
168
+ }
169
+
170
+ async readDir(dirpath: string, options: StorageReadDirOptions) {
171
+ return await this.disk().readDir(dirpath, options);
172
+ }
173
+ }
174
+
175
+ class StorageFileFacade {
176
+ storage: StorageFacade;
177
+
178
+ constructor(storage: StorageFacade) {
179
+ this.storage = storage;
180
+ }
181
+
182
+ async exists(filepath: string, diskKey?: string) {
183
+ return new StorageFileInstance(filepath, this.storage.disk(diskKey)).exists();
184
+ }
185
+
186
+ async isDir(filepath: string, diskKey?: string) {
187
+ return new StorageFileInstance(filepath, this.storage.disk(diskKey)).isDir();
188
+ }
189
+
190
+ async isFile(filepath: string, diskKey?: string) {
191
+ return new StorageFileInstance(filepath, this.storage.disk(diskKey)).isFile();
192
+ }
193
+
194
+ async delete(filepath: string, options?: StorageDeleteOptions, diskKey?: string) {
195
+ return new StorageFileInstance(filepath, this.storage.disk(diskKey)).delete(options);
196
+ }
197
+
198
+ async extension(filepath: string, diskKey?: string) {
199
+ return new StorageFileInstance(filepath, this.storage.disk(diskKey)).extension();
200
+ }
201
+
202
+ async mime(filepath: string, diskKey?: string) {
203
+ return new StorageFileInstance(filepath, this.storage.disk(diskKey)).mime();
204
+ }
205
+ }
206
+
207
+ export const Storage = new StorageFacade();
208
+ export const StorageFile = new StorageFileFacade(Storage);
@@ -0,0 +1,30 @@
1
+ export type StorageDeleteOptions = {
2
+ recursive?: boolean;
3
+ force?: boolean;
4
+ };
5
+
6
+ export type StorageMkdirOptions = {
7
+ recursive?: boolean;
8
+ };
9
+
10
+ export type StorageReadDirOptions = {
11
+ onyFiles?: boolean;
12
+ onlyDirs?: boolean;
13
+ };
14
+
15
+ export interface BaseStorageDriver {
16
+ put(filepath: string, contents: string | Buffer): Promise<{ status: string; filepath: string; payload?: any }>;
17
+ copy(filepath: string, target: string): Promise<{ status: string; filepath: string; origin_path: string; payload?: any }>;
18
+ move(filepath: string, target: string): Promise<{ status: string; filepath: string; old_filepath: string; payload?: any }>;
19
+ delete(
20
+ filepath: string | string[],
21
+ options?: StorageDeleteOptions
22
+ ): Promise<{ status: string; filepath: string | string[]; payload?: any }>;
23
+ mkdir(dirpath: string, options?: StorageMkdirOptions): Promise<{ status: string; dirpath: string; payload?: any }>;
24
+ exists(filepath: string): Promise<boolean>;
25
+ isDir(dirpath: string): Promise<boolean>;
26
+ isFile(filepath: string): Promise<boolean>;
27
+ extension(filepath: string): Promise<string | null>;
28
+ mime(filepath: string): Promise<string | null>;
29
+ readDir(dirpath: string, options?: StorageReadDirOptions): Promise<any[]>;
30
+ }
@@ -0,0 +1,110 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import mime from "mime-types";
4
+ import { BaseStorageDriver, StorageDeleteOptions, StorageMkdirOptions, StorageReadDirOptions } from "./BaseStorageDriver";
5
+ import { storageDir } from "../../Functions/FileFunctions";
6
+
7
+ export class LocalStorageDriver implements BaseStorageDriver {
8
+ config: any;
9
+ basepath: string;
10
+
11
+ constructor(conf: any) {
12
+ this.config = conf;
13
+ this.basepath = conf.basepath && conf.basepath.trim() !== "" ? conf.basepath : storageDir();
14
+ }
15
+
16
+ path(...args: string[]) {
17
+ return path.join(this.basepath, ...args);
18
+ }
19
+
20
+ async put(filepath: string, contents: string | Buffer) {
21
+ fs.writeFileSync(this.path(filepath), contents);
22
+
23
+ return {
24
+ status: "success",
25
+ filepath: filepath,
26
+ payload: {},
27
+ };
28
+ }
29
+
30
+ async copy(filepath: string, target: string) {
31
+ fs.cpSync(this.path(filepath), target);
32
+
33
+ return {
34
+ status: "success",
35
+ filepath: target,
36
+ origin_path: filepath,
37
+ payload: {},
38
+ };
39
+ }
40
+
41
+ async move(filepath: string, target: string) {
42
+ fs.renameSync(this.path(filepath), target);
43
+
44
+ return {
45
+ status: "success",
46
+ filepath: target,
47
+ old_filepath: filepath,
48
+ payload: {},
49
+ };
50
+ }
51
+
52
+ async delete(filepath: string | string[], options?: StorageDeleteOptions) {
53
+ if (Array.isArray(filepath)) {
54
+ for (let el of filepath) {
55
+ if (options?.recursive) {
56
+ fs.rmSync(this.path(el), { recursive: true, force: options.force ? true : false });
57
+ } else {
58
+ fs.rmSync(this.path(el), { recursive: true, force: options?.force ? true : false });
59
+ }
60
+ }
61
+ } else {
62
+ fs.rmSync(this.path(filepath), { recursive: options?.recursive ? true : false, force: options?.force ? true : false });
63
+ }
64
+
65
+ return {
66
+ status: "success",
67
+ filepath: filepath,
68
+ payload: {},
69
+ };
70
+ }
71
+
72
+ async mkdir(dirpath: string, options?: StorageMkdirOptions) {
73
+ fs.mkdirSync(this.path(dirpath));
74
+
75
+ return {
76
+ status: "success",
77
+ dirpath: dirpath,
78
+ payload: {},
79
+ };
80
+ }
81
+
82
+ async exists(filepath: string) {
83
+ return fs.existsSync(this.path(filepath));
84
+ }
85
+
86
+ async isDir(dirpath: string) {
87
+ const stats = fs.lstatSync(this.path(dirpath));
88
+ return stats.isDirectory();
89
+ }
90
+
91
+ async isFile(filepath: string) {
92
+ const stats = fs.lstatSync(this.path(filepath));
93
+ return stats.isFile();
94
+ }
95
+
96
+ async extension(filepath: string) {
97
+ const temp = filepath.indexOf(".") > 0 ? filepath.substring(filepath.lastIndexOf(".") + 1) : null;
98
+ return temp && temp.trim() !== "" ? temp.trim() : null;
99
+ }
100
+
101
+ async mime(filepath: string) {
102
+ const temp = mime.lookup(this.path(filepath));
103
+ return temp ? temp : null;
104
+ }
105
+
106
+ async readDir(dirpath: string, options?: StorageReadDirOptions) {
107
+ const entries = fs.readdirSync(this.path(dirpath));
108
+ return entries && entries.length > 0 ? entries : [];
109
+ }
110
+ }
@@ -1,47 +1,46 @@
1
1
  class StringFunctionsFacade {
2
- slug(string: string) {
3
- string = (string + "").toLowerCase();
4
- string = string.replace(new RegExp(" ", "g"), " ");
5
- string = string.replace(new RegExp(" ", "g"), " ");
6
- string = string.replace(new RegExp(" ", "g"), "-");
7
- string = string.replace(new RegExp("_", "g"), "-");
2
+ slug(string: string) {
3
+ string = (string + "").toLowerCase();
4
+ string = string.replace(new RegExp(" ", "g"), " ");
5
+ string = string.replace(new RegExp(" ", "g"), " ");
6
+ string = string.replace(new RegExp(" ", "g"), "-");
7
+ string = string.replace(new RegExp("_", "g"), "-");
8
8
 
9
- const replacements: { [key: string]: string } = {
10
- Ä: "AE",
11
- ä: "ae",
12
- Ö: "OE",
13
- ö: "oe",
14
- Ü: "UE",
15
- ü: "ue",
16
- Đ: "D",
17
- đ: "d",
18
- "&": "and",
19
- "€": "euro",
20
- "<": "less",
21
- ">": "greater",
22
- };
23
- for (let i in replacements) {
24
- string = string.replace(new RegExp(i, "g"), replacements[i]);
25
- }
9
+ const replacements: { [key: string]: string } = {
10
+ Ä: "AE",
11
+ ä: "ae",
12
+ Ö: "OE",
13
+ ö: "oe",
14
+ Ü: "UE",
15
+ ü: "ue",
16
+ Đ: "D",
17
+ đ: "d",
18
+ "&": "and",
19
+ "€": "euro",
20
+ "<": "less",
21
+ ">": "greater",
22
+ };
23
+ for (let i in replacements) {
24
+ string = string.replace(new RegExp(i, "g"), replacements[i]);
25
+ }
26
26
 
27
- return string.replace(/[^a-zA-Z0-9\- ]/g, "").trim();
28
- }
27
+ return string.replace(/[^a-zA-Z0-9\- ]/g, "").trim();
28
+ }
29
29
 
30
- random(length: number) {
31
- const result = [];
32
- const chars =
33
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
34
- for (let i = 0; i < length; i++) {
35
- result.push(chars.charAt(Math.floor(Math.random() * chars.length)));
30
+ random(length: number) {
31
+ const result = [];
32
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
33
+ for (let i = 0; i < length; i++) {
34
+ result.push(chars.charAt(Math.floor(Math.random() * chars.length)));
35
+ }
36
+ return result.join("");
36
37
  }
37
- return result.join("");
38
- }
39
38
 
40
- nl2br(str: string, replaceMode?: boolean, isXhtml?: boolean) {
41
- const breakTag = isXhtml ? "<br />" : "<br>";
42
- const replaceStr = replaceMode ? "$1" + breakTag : "$1" + breakTag + "$2";
43
- return (str + "").replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, replaceStr);
44
- }
39
+ nl2br(str: string, replaceMode?: boolean, isXhtml?: boolean) {
40
+ const breakTag = isXhtml ? "<br />" : "<br>";
41
+ const replaceStr = replaceMode ? "$1" + breakTag : "$1" + breakTag + "$2";
42
+ return (str + "").replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, replaceStr);
43
+ }
45
44
  }
46
45
 
47
46
  export const StringFunctions = new StringFunctionsFacade();
package/src/index.ts CHANGED
@@ -18,3 +18,5 @@ export * from "./Router/Response";
18
18
  export * from "./Database/BaseDBDriver";
19
19
  export * from "./Database/DBDrivers";
20
20
  export * from "./Database/QueryBuilder";
21
+ export * from "./Facades/StorageDrivers/BaseStorageDriver";
22
+ export * from "./Facades/Storage";