blixify-server 0.3.28 → 1.0.0
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.
- package/dist/apis/authWrapper.d.ts.map +1 -1
- package/dist/apis/authWrapper.js +1 -228
- package/dist/apis/crypto.d.ts.map +1 -1
- package/dist/apis/crypto.js +1 -63
- package/dist/apis/dayjs.d.ts +3 -0
- package/dist/apis/dayjs.d.ts.map +1 -0
- package/dist/apis/dayjs.js +1 -0
- package/dist/apis/fbWrapper.d.ts +6 -6
- package/dist/apis/fbWrapper.d.ts.map +1 -1
- package/dist/apis/fbWrapper.js +1 -839
- package/dist/apis/googleAnalyticsWrapper.d.ts.map +1 -1
- package/dist/apis/googleAnalyticsWrapper.js +1 -223
- package/dist/apis/index.js +1 -21
- package/dist/apis/mondayWrapper.d.ts +2 -2
- package/dist/apis/mondayWrapper.d.ts.map +1 -1
- package/dist/apis/mondayWrapper.js +32 -527
- package/dist/apis/mongoWrapper.d.ts +6 -6
- package/dist/apis/mongoWrapper.d.ts.map +1 -1
- package/dist/apis/mongoWrapper.js +1 -1524
- package/dist/apis/postgresqlWrapper.d.ts +3 -3
- package/dist/apis/postgresqlWrapper.d.ts.map +1 -1
- package/dist/apis/postgresqlWrapper.js +1 -514
- package/dist/apis/security.d.ts.map +1 -1
- package/dist/apis/security.js +1 -80
- package/dist/apis/trackVisionWrapper.d.ts.map +1 -1
- package/dist/apis/trackVisionWrapper.js +1 -150
- package/dist/apis/uploadWrapper.d.ts.map +1 -1
- package/dist/apis/uploadWrapper.js +1 -481
- package/dist/apis/utils.d.ts +1 -1
- package/dist/apis/utils.d.ts.map +1 -1
- package/dist/apis/utils.js +1 -25
- package/dist/buildtsconfig.tsbuildinfo +1 -0
- package/dist/model/Log.js +1 -2
- package/dist/model/QueryModel.d.ts.map +1 -1
- package/dist/model/QueryModel.js +1 -118
- package/dist/model/SecurityConfig.js +1 -2
- package/package.json +21 -12
|
@@ -1,150 +1 @@
|
|
|
1
|
-
"
|
|
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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.TrackVisionWrapper = void 0;
|
|
13
|
-
class TrackVisionWrapper {
|
|
14
|
-
constructor(username, password, lib) {
|
|
15
|
-
this.handleSafeTruckRequest = (endpoint, data) => __awaiter(this, void 0, void 0, function* () {
|
|
16
|
-
const response = yield fetch(`${TrackVisionWrapper.BASE_URL}${endpoint}`, {
|
|
17
|
-
method: "POST",
|
|
18
|
-
headers: {
|
|
19
|
-
"Content-Type": "application/json",
|
|
20
|
-
},
|
|
21
|
-
body: JSON.stringify(Object.assign({ username: this.username, password: this.password }, data)),
|
|
22
|
-
});
|
|
23
|
-
const result = yield response.json();
|
|
24
|
-
if (result.isERROR) {
|
|
25
|
-
throw new Error(result.errorMessage);
|
|
26
|
-
}
|
|
27
|
-
return result;
|
|
28
|
-
});
|
|
29
|
-
this.initGetVehiclesPositionData = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
30
|
-
try {
|
|
31
|
-
const result = yield this.handleSafeTruckRequest("/api2/gps/getVehiclePositionData", {});
|
|
32
|
-
res.send(result);
|
|
33
|
-
}
|
|
34
|
-
catch (err) {
|
|
35
|
-
res.status(400).json({ err: err.message || err });
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
this.initGetVehiclePositionDataByPlateNo = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
try {
|
|
40
|
-
const { plateNo } = req.body;
|
|
41
|
-
const result = yield this.handleSafeTruckRequest("/api2/gps/getVehiclePositionDataByPlateNo", { plateNo });
|
|
42
|
-
res.send(result);
|
|
43
|
-
}
|
|
44
|
-
catch (err) {
|
|
45
|
-
res.status(400).json({ err: err.message || err });
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
this.initGetVehicleHistoricalPlayback = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
49
|
-
try {
|
|
50
|
-
const { plateNo, fromDateTime, toDateTime } = req.body;
|
|
51
|
-
const result = yield this.handleSafeTruckRequest("/api2/gps/getHistoricalPB", {
|
|
52
|
-
plateNo,
|
|
53
|
-
fromDateTime,
|
|
54
|
-
toDateTime,
|
|
55
|
-
});
|
|
56
|
-
res.send(result);
|
|
57
|
-
}
|
|
58
|
-
catch (err) {
|
|
59
|
-
res.status(400).json({ err: err.message || err });
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
this.initGetVehicleListing = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
63
|
-
try {
|
|
64
|
-
const result = yield this.handleSafeTruckRequest("/api2/vehicle/getVehicleListingData", {});
|
|
65
|
-
res.send(result);
|
|
66
|
-
}
|
|
67
|
-
catch (err) {
|
|
68
|
-
res.status(400).json({ err: err.message || err });
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
this.initGetVehicleLicenseDocuments = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
72
|
-
try {
|
|
73
|
-
const result = yield this.handleSafeTruckRequest("/api2/vehicle/getVehiclesLicenseDocumentsData", {});
|
|
74
|
-
res.send(result);
|
|
75
|
-
}
|
|
76
|
-
catch (err) {
|
|
77
|
-
res.status(400).json({ err: err.message || err });
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
this.initGetGeofenceListing = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
81
|
-
try {
|
|
82
|
-
const result = yield this.handleSafeTruckRequest("/api2/gps/getGeofence", {});
|
|
83
|
-
res.send(result);
|
|
84
|
-
}
|
|
85
|
-
catch (err) {
|
|
86
|
-
res.status(400).json({ err: err.message || err });
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
this.initGetGeofenceNotification = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
90
|
-
try {
|
|
91
|
-
const { fromDateTime, toDateTime } = req.body;
|
|
92
|
-
const result = yield this.handleSafeTruckRequest("/api2/gps/getGeofenceNotification", {
|
|
93
|
-
fromDateTime,
|
|
94
|
-
toDateTime,
|
|
95
|
-
});
|
|
96
|
-
res.send(result);
|
|
97
|
-
}
|
|
98
|
-
catch (err) {
|
|
99
|
-
res.status(400).json({ err: err.message || err });
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
this.init = () => {
|
|
103
|
-
const router = this.lib.express.Router();
|
|
104
|
-
/**
|
|
105
|
-
* @Input :
|
|
106
|
-
* None
|
|
107
|
-
*/
|
|
108
|
-
router.post("/getVehiclesPositionData", this.initGetVehiclesPositionData);
|
|
109
|
-
/**
|
|
110
|
-
* @Input :
|
|
111
|
-
* plateNo - string
|
|
112
|
-
*/
|
|
113
|
-
router.post("/getVehiclePositionDataByPlateNo", this.initGetVehiclePositionDataByPlateNo);
|
|
114
|
-
/**
|
|
115
|
-
* @Input :
|
|
116
|
-
* plateNo - string
|
|
117
|
-
* fromDateTime - string (Optional)
|
|
118
|
-
* toDateTime - string (Optional)
|
|
119
|
-
*/
|
|
120
|
-
router.post("/getVehicleHistoricalPlayback", this.initGetVehicleHistoricalPlayback);
|
|
121
|
-
/**
|
|
122
|
-
* @Input :
|
|
123
|
-
* None
|
|
124
|
-
*/
|
|
125
|
-
router.post("/getVehicleListing", this.initGetVehicleListing);
|
|
126
|
-
/**
|
|
127
|
-
* @Input :
|
|
128
|
-
* None
|
|
129
|
-
*/
|
|
130
|
-
router.post("/getVehicleLicenseDocuments", this.initGetVehicleLicenseDocuments);
|
|
131
|
-
/**
|
|
132
|
-
* @Input :
|
|
133
|
-
* None
|
|
134
|
-
*/
|
|
135
|
-
router.post("/getGeofenceListing", this.initGetGeofenceListing);
|
|
136
|
-
/**
|
|
137
|
-
* @Input :
|
|
138
|
-
* fromDateTime - string
|
|
139
|
-
* toDateTime - string
|
|
140
|
-
*/
|
|
141
|
-
router.post("/getGeofenceNotification", this.initGetGeofenceNotification);
|
|
142
|
-
return router;
|
|
143
|
-
};
|
|
144
|
-
this.username = username;
|
|
145
|
-
this.password = password;
|
|
146
|
-
this.lib = lib;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
exports.TrackVisionWrapper = TrackVisionWrapper;
|
|
150
|
-
TrackVisionWrapper.BASE_URL = "http://trackvision2.ddns.net";
|
|
1
|
+
var __awaiter=this&&this.__awaiter||function(e,n,r,c){return new(r=r||Promise)(function(i,t){function s(e){try{o(c.next(e))}catch(e){t(e)}}function a(e){try{o(c.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?i(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(s,a)}o((c=c.apply(e,n||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.TrackVisionWrapper=void 0;class TrackVisionWrapper{constructor(e,t,i){this.handleSafeTruckRequest=(t,i)=>__awaiter(this,void 0,void 0,function*(){var e=yield(yield fetch(""+TrackVisionWrapper.BASE_URL+t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(Object.assign({username:this.username,password:this.password},i))})).json();if(e.isERROR)throw new Error(e.errorMessage);return e}),this.initGetVehiclesPositionData=(e,t)=>__awaiter(this,void 0,void 0,function*(){try{var e=yield this.handleSafeTruckRequest("/api2/gps/getVehiclePositionData",{});t.send(e)}catch(e){t.status(400).json({err:e.message||e})}}),this.initGetVehiclePositionDataByPlateNo=(i,s)=>__awaiter(this,void 0,void 0,function*(){try{var e=i.body.plateNo,t=yield this.handleSafeTruckRequest("/api2/gps/getVehiclePositionDataByPlateNo",{plateNo:e});s.send(t)}catch(e){s.status(400).json({err:e.message||e})}}),this.initGetVehicleHistoricalPlayback=(a,o)=>__awaiter(this,void 0,void 0,function*(){try{var{plateNo:e,fromDateTime:t,toDateTime:i}=a.body,s=yield this.handleSafeTruckRequest("/api2/gps/getHistoricalPB",{plateNo:e,fromDateTime:t,toDateTime:i});o.send(s)}catch(e){o.status(400).json({err:e.message||e})}}),this.initGetVehicleListing=(e,t)=>__awaiter(this,void 0,void 0,function*(){try{var e=yield this.handleSafeTruckRequest("/api2/vehicle/getVehicleListingData",{});t.send(e)}catch(e){t.status(400).json({err:e.message||e})}}),this.initGetVehicleLicenseDocuments=(e,t)=>__awaiter(this,void 0,void 0,function*(){try{var e=yield this.handleSafeTruckRequest("/api2/vehicle/getVehiclesLicenseDocumentsData",{});t.send(e)}catch(e){t.status(400).json({err:e.message||e})}}),this.initGetGeofenceListing=(e,t)=>__awaiter(this,void 0,void 0,function*(){try{var e=yield this.handleSafeTruckRequest("/api2/gps/getGeofence",{});t.send(e)}catch(e){t.status(400).json({err:e.message||e})}}),this.initGetGeofenceNotification=(s,a)=>__awaiter(this,void 0,void 0,function*(){try{var{fromDateTime:e,toDateTime:t}=s.body,i=yield this.handleSafeTruckRequest("/api2/gps/getGeofenceNotification",{fromDateTime:e,toDateTime:t});a.send(i)}catch(e){a.status(400).json({err:e.message||e})}}),this.init=()=>{var e=this.lib.express.Router();return e.post("/getVehiclesPositionData",this.initGetVehiclesPositionData),e.post("/getVehiclePositionDataByPlateNo",this.initGetVehiclePositionDataByPlateNo),e.post("/getVehicleHistoricalPlayback",this.initGetVehicleHistoricalPlayback),e.post("/getVehicleListing",this.initGetVehicleListing),e.post("/getVehicleLicenseDocuments",this.initGetVehicleLicenseDocuments),e.post("/getGeofenceListing",this.initGetGeofenceListing),e.post("/getGeofenceNotification",this.initGetGeofenceNotification),e},this.username=e,this.password=t,this.lib=i}}(exports.TrackVisionWrapper=TrackVisionWrapper).BASE_URL="http://trackvision2.ddns.net";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uploadWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/uploadWrapper.ts"],"names":[],"mappings":"AAKA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AAErD,UAAU,SAAS;IACjB,MAAM,EAAE,GAAG,CAAC;IACZ,UAAU,EAAE,GAAG,CAAC;IAChB,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,IAAI,CAAC,EAAE,GAAG,CAAC;CACZ;AAED;;;GAGG;AACH,qBAAa,aAAa;IACxB,WAAW,EAAE,GAAG,CAAM;IACtB,MAAM,UAAS;IACf,MAAM,EAAE,cAAc,CAQpB;IACF,GAAG,EAAE,SAAS,CAAC;IACf,WAAW,SAAc;IAEzB,YAAY,
|
|
1
|
+
{"version":3,"file":"uploadWrapper.d.ts","sourceRoot":"","sources":["../../src/apis/uploadWrapper.ts"],"names":[],"mappings":"AAKA,OAAO,cAAc,MAAM,yBAAyB,CAAC;AAErD,UAAU,SAAS;IACjB,MAAM,EAAE,GAAG,CAAC;IACZ,UAAU,EAAE,GAAG,CAAC;IAChB,OAAO,CAAC,EAAE,GAAG,CAAC;IACd,IAAI,CAAC,EAAE,GAAG,CAAC;CACZ;AAED;;;GAGG;AACH,qBAAa,aAAa;IACxB,WAAW,EAAE,GAAG,CAAM;IACtB,MAAM,UAAS;IACf,MAAM,EAAE,cAAc,CAQpB;IACF,GAAG,EAAE,SAAS,CAAC;IACf,WAAW,SAAc;IAEzB,YAAY,GAAI,KAAK,GAAG,EAAE,oBAAoB,OAAO,aAUnD;gBAGA,WAAW,EAAE,GAAG,EAChB,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,cAAc,EACtB,GAAG,EAAE,SAAS,EACd,WAAW,CAAC,EAAE,UAAU,GAAG,KAAK,GAAG,MAAM;IAS3C,OAAO,CAAC,qBAAqB;IAM7B,OAAO,CAAC,gBAAgB;IAkBxB,UAAU,GAAU,KAAK,GAAG,EAAE,KAAK,GAAG,EAAE,MAAM,QAAQ,GAAG,QAAQ,mBA4H/D;IAEF,UAAU,GAAU,KAAK,GAAG,EAAE,KAAK,GAAG,mBAiDpC;IAEF,0BAA0B,GAAU,WAAW,GAAG,mBAwBhD;IAEF,sBAAsB,GACpB,YAAY,MAAM,EAClB,iBAAiB,MAAM,mBAiBvB;IAEF,yBAAyB,GAAU,YAAY,MAAM,mBAenD;IAEF,mBAAmB,GAAU,KAAK,GAAG,EAAE,KAAK,GAAG,mBA6C7C;IAEF,wBAAwB,GAAU,KAAK,GAAG,EAAE,KAAK,GAAG,mBAmDlD;IAEF,OAAO,GAAU,KAAK,GAAG,EAAE,KAAK,GAAG,mBA2DjC;IAEF,IAAI,YA6EF;CACH"}
|
|
@@ -1,481 +1 @@
|
|
|
1
|
-
"
|
|
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.UploadWrapper = void 0;
|
|
16
|
-
const axios_1 = __importDefault(require("axios"));
|
|
17
|
-
const fs_1 = __importDefault(require("fs"));
|
|
18
|
-
const mime_1 = __importDefault(require("mime"));
|
|
19
|
-
const path_1 = __importDefault(require("path"));
|
|
20
|
-
const QueryModel_1 = require("../model/QueryModel");
|
|
21
|
-
/**
|
|
22
|
-
* @Wrapper
|
|
23
|
-
*
|
|
24
|
-
*/
|
|
25
|
-
class UploadWrapper {
|
|
26
|
-
constructor(uploadAdmin, isProd, config, lib, storageType) {
|
|
27
|
-
this.uploadAdmin = "";
|
|
28
|
-
this.isProd = false;
|
|
29
|
-
this.config = {
|
|
30
|
-
baseConfig: [],
|
|
31
|
-
opsConfig: {
|
|
32
|
-
read: [],
|
|
33
|
-
create: [],
|
|
34
|
-
update: [],
|
|
35
|
-
delete: [],
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
this.storageType = "firebase";
|
|
39
|
-
this.modelChecker = (obj, isDeleteDirectory) => {
|
|
40
|
-
if (obj.assetCollectionName &&
|
|
41
|
-
obj.assetParentId &&
|
|
42
|
-
(isDeleteDirectory || obj.assetFileName)) {
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
this.initUpload = (req, res, type) => __awaiter(this, void 0, void 0, function* () {
|
|
50
|
-
try {
|
|
51
|
-
const respData = req.body.data;
|
|
52
|
-
const valid = this.modelChecker(respData) && (req.file || respData.downloadLink);
|
|
53
|
-
if (valid) {
|
|
54
|
-
//INFO : Security Checker
|
|
55
|
-
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
56
|
-
const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, type, req, respData);
|
|
57
|
-
if (!validBaseConfig || !validOpsConfig) {
|
|
58
|
-
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
const token = this.lib.randomUUID();
|
|
62
|
-
if (this.storageType === "aws") {
|
|
63
|
-
const s3UploadPromise = (type, response) => {
|
|
64
|
-
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
65
|
-
const buffer = type === "buffer";
|
|
66
|
-
const s3 = new this.uploadAdmin.S3();
|
|
67
|
-
let contentType;
|
|
68
|
-
if (!buffer) {
|
|
69
|
-
const extension = req.file.originalname.split(".").pop();
|
|
70
|
-
if (extension === "mbtiles")
|
|
71
|
-
contentType = "application/vnd.mapbox-vector-tile";
|
|
72
|
-
else
|
|
73
|
-
contentType = mime_1.default.getType(extension) || req.file.mimetype;
|
|
74
|
-
}
|
|
75
|
-
const uploadParams = {
|
|
76
|
-
Bucket: `${respData.bucketName}/${respData.assetCollectionName}/${respData.assetParentId}`,
|
|
77
|
-
Key: respData.assetFileName,
|
|
78
|
-
Body: buffer ? response.data : req.file.buffer,
|
|
79
|
-
ContentType: buffer
|
|
80
|
-
? response.headers["content-type"]
|
|
81
|
-
: contentType,
|
|
82
|
-
};
|
|
83
|
-
s3.upload(uploadParams, function (err) {
|
|
84
|
-
if (err) {
|
|
85
|
-
reject(err);
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
resolve("aws");
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
}));
|
|
92
|
-
};
|
|
93
|
-
if (respData.downloadLink) {
|
|
94
|
-
const response = yield (0, axios_1.default)({
|
|
95
|
-
method: "GET",
|
|
96
|
-
url: respData.downloadLink,
|
|
97
|
-
responseType: "arraybuffer",
|
|
98
|
-
});
|
|
99
|
-
if (response.data)
|
|
100
|
-
yield s3UploadPromise("buffer", response);
|
|
101
|
-
}
|
|
102
|
-
else
|
|
103
|
-
yield s3UploadPromise("file");
|
|
104
|
-
res.send({ data: token });
|
|
105
|
-
}
|
|
106
|
-
else if (this.storageType === "file") {
|
|
107
|
-
if (respData.downloadLink) {
|
|
108
|
-
const response = yield (0, axios_1.default)({
|
|
109
|
-
method: "GET",
|
|
110
|
-
url: respData.downloadLink,
|
|
111
|
-
responseType: "arraybuffer",
|
|
112
|
-
});
|
|
113
|
-
if (response.data) {
|
|
114
|
-
const filePath = this.getLocalFilePath(respData.bucketName, respData.assetCollectionName, respData.assetParentId, respData.assetFileName);
|
|
115
|
-
fs_1.default.writeFileSync(filePath, response.data);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
else if (req.file) {
|
|
119
|
-
const filePath = this.getLocalFilePath(respData.bucketName, respData.assetCollectionName, respData.assetParentId, respData.assetFileName);
|
|
120
|
-
fs_1.default.writeFileSync(filePath, req.file.buffer);
|
|
121
|
-
}
|
|
122
|
-
res.send({ data: token });
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
const firebaseBucket = this.uploadAdmin.storage().bucket();
|
|
126
|
-
const uploadFilePromise = (type, response) => {
|
|
127
|
-
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
128
|
-
const buffer = type === "buffer";
|
|
129
|
-
const uploadMetadata = {
|
|
130
|
-
contentType: buffer
|
|
131
|
-
? response.headers["content-type"]
|
|
132
|
-
: req.file.mimetype,
|
|
133
|
-
customMetadata: {
|
|
134
|
-
"file-name": type === "buffer"
|
|
135
|
-
? respData.assetFileName
|
|
136
|
-
: req.file.originalname,
|
|
137
|
-
},
|
|
138
|
-
metadata: {
|
|
139
|
-
firebaseStorageDownloadTokens: token,
|
|
140
|
-
},
|
|
141
|
-
};
|
|
142
|
-
const stream = firebaseBucket
|
|
143
|
-
.file(`${respData.assetCollectionName}/${respData.assetParentId}/${respData.assetFileName}`)
|
|
144
|
-
.createWriteStream({ metadata: uploadMetadata });
|
|
145
|
-
stream.end(buffer ? response.data : req.file.buffer);
|
|
146
|
-
stream.on("error", reject);
|
|
147
|
-
stream.on("finish", resolve);
|
|
148
|
-
}));
|
|
149
|
-
};
|
|
150
|
-
if (respData.downloadLink) {
|
|
151
|
-
const response = yield (0, axios_1.default)({
|
|
152
|
-
method: "GET",
|
|
153
|
-
url: respData.downloadLink,
|
|
154
|
-
responseType: "arraybuffer",
|
|
155
|
-
});
|
|
156
|
-
if (response.data)
|
|
157
|
-
yield uploadFilePromise("buffer", response);
|
|
158
|
-
}
|
|
159
|
-
else
|
|
160
|
-
yield uploadFilePromise("file");
|
|
161
|
-
res.send({ data: token });
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
else {
|
|
165
|
-
res.status(400).json({ err: "Invalid Fields" });
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
catch (err) {
|
|
169
|
-
res.status(400).json({ err: err });
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
this.initDelete = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
173
|
-
try {
|
|
174
|
-
const respData = req.body.data;
|
|
175
|
-
const valid = this.modelChecker(respData);
|
|
176
|
-
if (valid) {
|
|
177
|
-
//INFO : Security Checker
|
|
178
|
-
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
179
|
-
const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "delete", req, respData);
|
|
180
|
-
if (!validBaseConfig || !validOpsConfig) {
|
|
181
|
-
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
if (this.storageType === "aws") {
|
|
185
|
-
const s3 = new this.uploadAdmin.S3();
|
|
186
|
-
const params = {
|
|
187
|
-
Bucket: `${respData.bucketName}/${respData.assetCollectionName}/${respData.assetParentId}`,
|
|
188
|
-
Key: respData.assetFileName,
|
|
189
|
-
};
|
|
190
|
-
yield s3.deleteObject(params).promise();
|
|
191
|
-
}
|
|
192
|
-
else if (this.storageType === "file") {
|
|
193
|
-
const filePath = this.getLocalFilePath(respData.bucketName, respData.assetCollectionName, respData.assetParentId, respData.assetFileName);
|
|
194
|
-
if (fs_1.default.existsSync(filePath)) {
|
|
195
|
-
fs_1.default.unlinkSync(filePath);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
else {
|
|
199
|
-
const firebaseBucket = this.uploadAdmin.storage().bucket();
|
|
200
|
-
yield firebaseBucket
|
|
201
|
-
.file(`${respData.assetCollectionName}/${respData.assetParentId}/${respData.assetFileName}`)
|
|
202
|
-
.delete();
|
|
203
|
-
}
|
|
204
|
-
res.send({ success: true });
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
res.status(400).json({ err: "Invalid Fields" });
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
catch (err) {
|
|
211
|
-
res.status(400).json({ err: err });
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
this.deleteGoogleFolderContents = (folderRef) => __awaiter(this, void 0, void 0, function* () {
|
|
215
|
-
try {
|
|
216
|
-
const listOptions = {
|
|
217
|
-
prefix: folderRef.name,
|
|
218
|
-
autoPaginate: false,
|
|
219
|
-
};
|
|
220
|
-
const [files] = yield this.uploadAdmin
|
|
221
|
-
.storage()
|
|
222
|
-
.bucket()
|
|
223
|
-
.getFiles(listOptions);
|
|
224
|
-
const deletionPromises = [];
|
|
225
|
-
for (const file of files) {
|
|
226
|
-
deletionPromises.push(file.delete());
|
|
227
|
-
}
|
|
228
|
-
for (const file of files) {
|
|
229
|
-
const isPrefix = file.name.endsWith("/");
|
|
230
|
-
if (isPrefix) {
|
|
231
|
-
deletionPromises.push(this.deleteGoogleFolderContents(file));
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
yield Promise.all(deletionPromises);
|
|
235
|
-
}
|
|
236
|
-
catch (error) { }
|
|
237
|
-
});
|
|
238
|
-
this.deleteAWSFolderContent = (bucketName, directoryPrefix) => __awaiter(this, void 0, void 0, function* () {
|
|
239
|
-
try {
|
|
240
|
-
const s3 = new this.uploadAdmin.S3();
|
|
241
|
-
const data = yield s3
|
|
242
|
-
.listObjectsV2({ Bucket: bucketName, Prefix: directoryPrefix })
|
|
243
|
-
.promise();
|
|
244
|
-
const files = data.Contents.map((obj) => obj.Key);
|
|
245
|
-
const deletionPromises = [];
|
|
246
|
-
for (const file of files) {
|
|
247
|
-
deletionPromises.push(s3.deleteObject({ Bucket: bucketName, Key: file }).promise());
|
|
248
|
-
}
|
|
249
|
-
yield Promise.all(deletionPromises);
|
|
250
|
-
}
|
|
251
|
-
catch (error) { }
|
|
252
|
-
});
|
|
253
|
-
this.deleteLocalFolderContents = (folderPath) => __awaiter(this, void 0, void 0, function* () {
|
|
254
|
-
try {
|
|
255
|
-
if (fs_1.default.existsSync(folderPath)) {
|
|
256
|
-
const files = fs_1.default.readdirSync(folderPath);
|
|
257
|
-
for (const file of files) {
|
|
258
|
-
const curPath = path_1.default.join(folderPath, file);
|
|
259
|
-
if (fs_1.default.lstatSync(curPath).isDirectory()) {
|
|
260
|
-
yield this.deleteLocalFolderContents(curPath);
|
|
261
|
-
fs_1.default.rmdirSync(curPath);
|
|
262
|
-
}
|
|
263
|
-
else {
|
|
264
|
-
fs_1.default.unlinkSync(curPath);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
catch (error) { }
|
|
270
|
-
});
|
|
271
|
-
this.initDeleteDirectory = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
272
|
-
try {
|
|
273
|
-
const respData = req.body.data;
|
|
274
|
-
const valid = this.modelChecker(respData, true);
|
|
275
|
-
if (valid) {
|
|
276
|
-
//INFO : Security Checker
|
|
277
|
-
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
278
|
-
const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "delete", req, respData);
|
|
279
|
-
if (!validBaseConfig || !validOpsConfig) {
|
|
280
|
-
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
if (this.storageType === "aws") {
|
|
284
|
-
yield this.deleteAWSFolderContent(`${respData.bucketName}`, `${respData.assetCollectionName}/${respData.assetParentId}/`);
|
|
285
|
-
}
|
|
286
|
-
else if (this.storageType === "file") {
|
|
287
|
-
const folderPath = path_1.default.join(this.uploadAdmin, respData.bucketName, respData.assetCollectionName, respData.assetParentId);
|
|
288
|
-
yield this.deleteLocalFolderContents(folderPath);
|
|
289
|
-
}
|
|
290
|
-
else {
|
|
291
|
-
const folderRef = this.uploadAdmin
|
|
292
|
-
.storage()
|
|
293
|
-
.bucket()
|
|
294
|
-
.file(`${respData.assetCollectionName}/${respData.assetParentId}/`);
|
|
295
|
-
yield this.deleteGoogleFolderContents(folderRef);
|
|
296
|
-
}
|
|
297
|
-
res.send({ success: true });
|
|
298
|
-
}
|
|
299
|
-
else {
|
|
300
|
-
res.status(400).json({ err: "Invalid Fields" });
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
catch (err) {
|
|
304
|
-
res.status(400).json({ err: err });
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
this.initBatchDeleteDirectory = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
308
|
-
try {
|
|
309
|
-
const respData = req.body.data;
|
|
310
|
-
const assetParentIds = JSON.parse(respData.assetParentId);
|
|
311
|
-
const valid = this.modelChecker(respData, true);
|
|
312
|
-
if (valid) {
|
|
313
|
-
//INFO : Security Checker
|
|
314
|
-
const validBaseConfig = (0, QueryModel_1.checkBaseConfig)(this.config, req);
|
|
315
|
-
const validOpsConfig = (0, QueryModel_1.checkOpsConfig)(this.config, "delete", req, respData);
|
|
316
|
-
if (!validBaseConfig || !validOpsConfig) {
|
|
317
|
-
res.status(400).json({ err: "Invalid Security Configuration" });
|
|
318
|
-
return;
|
|
319
|
-
}
|
|
320
|
-
yield Promise.all(assetParentIds.map((eachId) => __awaiter(this, void 0, void 0, function* () {
|
|
321
|
-
if (this.storageType === "aws") {
|
|
322
|
-
yield this.deleteAWSFolderContent(`${respData.bucketName}`, `${respData.assetCollectionName}/${eachId}/`);
|
|
323
|
-
}
|
|
324
|
-
else if (this.storageType === "file") {
|
|
325
|
-
const folderPath = path_1.default.join(this.uploadAdmin, respData.bucketName, respData.assetCollectionName, eachId);
|
|
326
|
-
yield this.deleteLocalFolderContents(folderPath);
|
|
327
|
-
}
|
|
328
|
-
else {
|
|
329
|
-
const folderRef = this.uploadAdmin
|
|
330
|
-
.storage()
|
|
331
|
-
.bucket()
|
|
332
|
-
.file(`${respData.assetCollectionName}/${eachId}/`);
|
|
333
|
-
yield this.deleteGoogleFolderContents(folderRef);
|
|
334
|
-
}
|
|
335
|
-
})));
|
|
336
|
-
res.send({ success: true });
|
|
337
|
-
}
|
|
338
|
-
else {
|
|
339
|
-
res.status(400).json({ err: "Invalid Fields" });
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
catch (err) {
|
|
343
|
-
res.status(400).json({ err: err });
|
|
344
|
-
}
|
|
345
|
-
});
|
|
346
|
-
this.initGet = (req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
347
|
-
//TODO: add FB initGet
|
|
348
|
-
try {
|
|
349
|
-
const { bucketName, assetCollectionName, assetParentId } = req.params;
|
|
350
|
-
const fullPath = req.path;
|
|
351
|
-
const basePath = `/get/${bucketName}/${assetCollectionName}/${assetParentId}/`;
|
|
352
|
-
const nestedPath = fullPath.replace(basePath, "");
|
|
353
|
-
if (this.storageType === "aws") {
|
|
354
|
-
const s3 = new this.uploadAdmin.S3();
|
|
355
|
-
const key = `${assetCollectionName}/${assetParentId}/${nestedPath}`;
|
|
356
|
-
const params = {
|
|
357
|
-
Bucket: bucketName,
|
|
358
|
-
Key: key,
|
|
359
|
-
};
|
|
360
|
-
s3.headObject(params, (err) => __awaiter(this, void 0, void 0, function* () {
|
|
361
|
-
if (err && err.code === "NotFound") {
|
|
362
|
-
res.status(404).send({ message: "File not found" });
|
|
363
|
-
}
|
|
364
|
-
else {
|
|
365
|
-
const resp = yield s3.getObject(params).promise();
|
|
366
|
-
const extension = nestedPath.split(".").pop();
|
|
367
|
-
let contentType;
|
|
368
|
-
if (extension === "mbtiles") {
|
|
369
|
-
contentType = "application/vnd.mapbox-vector-tile";
|
|
370
|
-
}
|
|
371
|
-
else {
|
|
372
|
-
contentType = mime_1.default.getType(extension) || resp.ContentType;
|
|
373
|
-
}
|
|
374
|
-
res.set("Content-Type", contentType);
|
|
375
|
-
res.send(resp.Body);
|
|
376
|
-
}
|
|
377
|
-
}));
|
|
378
|
-
}
|
|
379
|
-
else if (this.storageType === "file") {
|
|
380
|
-
const filePath = path_1.default.join(this.uploadAdmin, bucketName, assetCollectionName, assetParentId, nestedPath);
|
|
381
|
-
if (fs_1.default.existsSync(filePath)) {
|
|
382
|
-
const extension = nestedPath.split(".").pop();
|
|
383
|
-
let contentType;
|
|
384
|
-
if (extension === "mbtiles") {
|
|
385
|
-
contentType = "application/vnd.mapbox-vector-tile";
|
|
386
|
-
}
|
|
387
|
-
else {
|
|
388
|
-
contentType = mime_1.default.getType(extension) || "application/octet-stream";
|
|
389
|
-
}
|
|
390
|
-
res.set("Content-Type", contentType);
|
|
391
|
-
res.sendFile(filePath);
|
|
392
|
-
}
|
|
393
|
-
else {
|
|
394
|
-
res.status(404).send({ message: "File not found" });
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
catch (error) {
|
|
399
|
-
res.status(400).json({ err: error });
|
|
400
|
-
}
|
|
401
|
-
});
|
|
402
|
-
this.init = () => {
|
|
403
|
-
const router = this.lib.express.Router();
|
|
404
|
-
if (this.lib.cors) {
|
|
405
|
-
router.use(this.lib.cors());
|
|
406
|
-
}
|
|
407
|
-
/**
|
|
408
|
-
* @Input :
|
|
409
|
-
* assetCollectionName - string
|
|
410
|
-
* assetFileName - string
|
|
411
|
-
* assetParentId - string
|
|
412
|
-
* file? - File
|
|
413
|
-
* downloadLink? - Asset Download link instead of file
|
|
414
|
-
* bucketName? - Optional, only required for Google Storage & AWS S3
|
|
415
|
-
*/
|
|
416
|
-
router.post("/upload", this.lib.multer.single("file"), (req, res) => {
|
|
417
|
-
this.initUpload(req, res, "create");
|
|
418
|
-
});
|
|
419
|
-
/**
|
|
420
|
-
* @Input :
|
|
421
|
-
* assetCollectionName - string
|
|
422
|
-
* assetFileName - string
|
|
423
|
-
* assetParentId - string
|
|
424
|
-
* file? - File
|
|
425
|
-
* downloadLink? - Asset Download link instead of file
|
|
426
|
-
* bucketName? - Optional, only required for Google Storage & AWS S3
|
|
427
|
-
*/
|
|
428
|
-
router.post("/update", this.lib.multer.single("file"), (req, res) => {
|
|
429
|
-
this.initUpload(req, res, "update");
|
|
430
|
-
});
|
|
431
|
-
/**
|
|
432
|
-
* @Input :
|
|
433
|
-
* assetCollectionName - string
|
|
434
|
-
* assetFileName - string
|
|
435
|
-
* assetParentId - string
|
|
436
|
-
* bucketName? - Optional, only required for Google Storage & AWS S3
|
|
437
|
-
*/
|
|
438
|
-
router.post("/delete", this.initDelete);
|
|
439
|
-
/**
|
|
440
|
-
* @Input :
|
|
441
|
-
* assetCollectionName - string
|
|
442
|
-
* assetParentId - string
|
|
443
|
-
* bucketName? - Optional, only required for Google Storage & AWS S3
|
|
444
|
-
*/
|
|
445
|
-
router.post("/deleteDirectory", this.initDeleteDirectory);
|
|
446
|
-
/**
|
|
447
|
-
* @Input :
|
|
448
|
-
* assetCollectionName - string
|
|
449
|
-
* assetParentId - string[]
|
|
450
|
-
* bucketName? - Optional, only required for Google Storage & AWS S3
|
|
451
|
-
*/
|
|
452
|
-
router.post("/batchDeleteDirectory", this.initBatchDeleteDirectory);
|
|
453
|
-
/**
|
|
454
|
-
* @Input :
|
|
455
|
-
* bucketName - string
|
|
456
|
-
* assetCollectionName - string
|
|
457
|
-
* assetParentId - string
|
|
458
|
-
* * - string - Any number of nested paths and the final filename
|
|
459
|
-
*/
|
|
460
|
-
router.get("/get/:bucketName/:assetCollectionName/:assetParentId/*", this.initGet);
|
|
461
|
-
return router;
|
|
462
|
-
};
|
|
463
|
-
this.uploadAdmin = uploadAdmin;
|
|
464
|
-
this.isProd = isProd;
|
|
465
|
-
this.config = config;
|
|
466
|
-
this.lib = lib;
|
|
467
|
-
this.storageType = storageType !== null && storageType !== void 0 ? storageType : "firebase";
|
|
468
|
-
}
|
|
469
|
-
ensureDirectoryExists(dirPath) {
|
|
470
|
-
if (!fs_1.default.existsSync(dirPath)) {
|
|
471
|
-
fs_1.default.mkdirSync(dirPath, { recursive: true });
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
getLocalFilePath(bucketName, assetCollectionName, assetParentId, assetFileName) {
|
|
475
|
-
const basePath = this.uploadAdmin;
|
|
476
|
-
const fullPath = path_1.default.join(basePath, bucketName, assetCollectionName, assetParentId, assetFileName);
|
|
477
|
-
this.ensureDirectoryExists(path_1.default.dirname(fullPath));
|
|
478
|
-
return fullPath;
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
exports.UploadWrapper = UploadWrapper;
|
|
1
|
+
var __awaiter=this&&this.__awaiter||function(e,l,n,r){return new(n=n||Promise)(function(i,t){function a(e){try{o(r.next(e))}catch(e){t(e)}}function s(e){try{o(r.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?i(e.value):((t=e.value)instanceof n?t:new n(function(e){e(t)})).then(a,s)}o((r=r.apply(e,l||[])).next())})},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.UploadWrapper=void 0;let axios_1=__importDefault(require("axios")),fs_1=__importDefault(require("fs")),mime_1=__importDefault(require("mime")),path_1=__importDefault(require("path")),QueryModel_1=require("../model/QueryModel");class UploadWrapper{constructor(e,t,i,a,s){this.uploadAdmin="",this.isProd=!1,this.config={baseConfig:[],opsConfig:{read:[],create:[],update:[],delete:[]}},this.storageType="firebase",this.modelChecker=(e,t)=>!(!e.assetCollectionName||!e.assetParentId||!t&&!e.assetFileName),this.initUpload=(u,f,n)=>__awaiter(this,void 0,void 0,function*(){try{let r=u.body.data;if(this.modelChecker(r)&&(u.file||r.downloadLink)){var e,t,i,a=(0,QueryModel_1.checkBaseConfig)(this.config,u),s=(0,QueryModel_1.checkOpsConfig)(this.config,n,u,r);if(a&&s){let n=this.lib.randomUUID();if("aws"===this.storageType){var o,l=(l,n)=>new Promise((s,o)=>__awaiter(this,void 0,void 0,function*(){var e="buffer"===l,t=new this.uploadAdmin.S3;let i;e||(a=u.file.originalname.split(".").pop(),i="mbtiles"===a?"application/vnd.mapbox-vector-tile":mime_1.default.getType(a)||u.file.mimetype);var a={Bucket:`${r.bucketName}/${r.assetCollectionName}/`+r.assetParentId,Key:r.assetFileName,Body:e?n.data:u.file.buffer,ContentType:e?n.headers["content-type"]:i};t.upload(a,function(e){e?o(e):s("aws")})}));r.downloadLink?(o=yield(0,axios_1.default)({method:"GET",url:r.downloadLink,responseType:"arraybuffer"})).data&&(yield l("buffer",o)):yield l("file")}else if("file"===this.storageType)r.downloadLink?(e=yield(0,axios_1.default)({method:"GET",url:r.downloadLink,responseType:"arraybuffer"})).data&&(t=this.getLocalFilePath(r.bucketName,r.assetCollectionName,r.assetParentId,r.assetFileName),fs_1.default.writeFileSync(t,e.data)):u.file&&(i=this.getLocalFilePath(r.bucketName,r.assetCollectionName,r.assetParentId,r.assetFileName),fs_1.default.writeFileSync(i,u.file.buffer));else{let l=this.uploadAdmin.storage().bucket();var d,c=(s,o)=>new Promise((i,a)=>__awaiter(this,void 0,void 0,function*(){var e="buffer"===s,t={contentType:e?o.headers["content-type"]:u.file.mimetype,customMetadata:{"file-name":"buffer"===s?r.assetFileName:u.file.originalname},metadata:{firebaseStorageDownloadTokens:n}},t=l.file(`${r.assetCollectionName}/${r.assetParentId}/`+r.assetFileName).createWriteStream({metadata:t});t.end(e?o.data:u.file.buffer),t.on("error",a),t.on("finish",i)}));r.downloadLink?(d=yield(0,axios_1.default)({method:"GET",url:r.downloadLink,responseType:"arraybuffer"})).data&&(yield c("buffer",d)):yield c("file")}f.send({data:n})}else f.status(400).json({err:"Invalid Security Configuration"})}else f.status(400).json({err:"Invalid Fields"})}catch(e){f.status(400).json({err:e})}}),this.initDelete=(l,n)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,a,s,o=l.body.data;this.modelChecker(o)?(e=(0,QueryModel_1.checkBaseConfig)(this.config,l),t=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",l,o),e&&t?("aws"===this.storageType?(i=new this.uploadAdmin.S3,a={Bucket:`${o.bucketName}/${o.assetCollectionName}/`+o.assetParentId,Key:o.assetFileName},yield i.deleteObject(a).promise()):"file"===this.storageType?(s=this.getLocalFilePath(o.bucketName,o.assetCollectionName,o.assetParentId,o.assetFileName),fs_1.default.existsSync(s)&&fs_1.default.unlinkSync(s)):yield this.uploadAdmin.storage().bucket().file(`${o.assetCollectionName}/${o.assetParentId}/`+o.assetFileName).delete(),n.send({success:!0})):n.status(400).json({err:"Invalid Security Configuration"})):n.status(400).json({err:"Invalid Fields"})}catch(e){n.status(400).json({err:e})}}),this.deleteGoogleFolderContents=o=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i={prefix:o.name,autoPaginate:!1},[a]=yield this.uploadAdmin.storage().bucket().getFiles(i),s=[];for(e of a)s.push(e.delete());for(t of a)t.name.endsWith("/")&&s.push(this.deleteGoogleFolderContents(t));yield Promise.all(s)}catch(e){}}),this.deleteAWSFolderContent=(s,o)=>__awaiter(this,void 0,void 0,function*(){try{var e,t=new this.uploadAdmin.S3,i=(yield t.listObjectsV2({Bucket:s,Prefix:o}).promise()).Contents.map(e=>e.Key),a=[];for(e of i)a.push(t.deleteObject({Bucket:s,Key:e}).promise());yield Promise.all(a)}catch(e){}}),this.deleteLocalFolderContents=i=>__awaiter(this,void 0,void 0,function*(){try{var e;if(fs_1.default.existsSync(i))for(e of fs_1.default.readdirSync(i)){var t=path_1.default.join(i,e);fs_1.default.lstatSync(t).isDirectory()?(yield this.deleteLocalFolderContents(t),fs_1.default.rmdirSync(t)):fs_1.default.unlinkSync(t)}}catch(e){}}),this.initDeleteDirectory=(o,l)=>__awaiter(this,void 0,void 0,function*(){try{var e,t,i,a,s=o.body.data;this.modelChecker(s,!0)?(e=(0,QueryModel_1.checkBaseConfig)(this.config,o),t=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",o,s),e&&t?("aws"===this.storageType?yield this.deleteAWSFolderContent(""+s.bucketName,`${s.assetCollectionName}/${s.assetParentId}/`):"file"===this.storageType?(i=path_1.default.join(this.uploadAdmin,s.bucketName,s.assetCollectionName,s.assetParentId),yield this.deleteLocalFolderContents(i)):(a=this.uploadAdmin.storage().bucket().file(`${s.assetCollectionName}/${s.assetParentId}/`),yield this.deleteGoogleFolderContents(a)),l.send({success:!0})):l.status(400).json({err:"Invalid Security Configuration"})):l.status(400).json({err:"Invalid Fields"})}catch(e){l.status(400).json({err:e})}}),this.initBatchDeleteDirectory=(s,o)=>__awaiter(this,void 0,void 0,function*(){try{let i=s.body.data;var e,t,a=JSON.parse(i.assetParentId);this.modelChecker(i,!0)?(e=(0,QueryModel_1.checkBaseConfig)(this.config,s),t=(0,QueryModel_1.checkOpsConfig)(this.config,"delete",s,i),e&&t?(yield Promise.all(a.map(t=>__awaiter(this,void 0,void 0,function*(){var e;"aws"===this.storageType?yield this.deleteAWSFolderContent(""+i.bucketName,i.assetCollectionName+`/${t}/`):"file"===this.storageType?(e=path_1.default.join(this.uploadAdmin,i.bucketName,i.assetCollectionName,t),yield this.deleteLocalFolderContents(e)):(e=this.uploadAdmin.storage().bucket().file(i.assetCollectionName+`/${t}/`),yield this.deleteGoogleFolderContents(e))}))),o.send({success:!0})):o.status(400).json({err:"Invalid Security Configuration"})):o.status(400).json({err:"Invalid Fields"})}catch(e){o.status(400).json({err:e})}}),this.initGet=(n,r)=>__awaiter(this,void 0,void 0,function*(){try{var{bucketName:e,assetCollectionName:t,assetParentId:i}=n.params,a=`/get/${e}/${t}/${i}/`;let o=n.path.replace(a,"");if("aws"===this.storageType){let a=new this.uploadAdmin.S3;let s={Bucket:e,Key:t+`/${i}/`+o};a.headObject(s,e=>__awaiter(this,void 0,void 0,function*(){if(e&&"NotFound"===e.code)r.status(404).send({message:"File not found"});else{var t=yield a.getObject(s).promise(),i=o.split(".").pop();let e;e="mbtiles"===i?"application/vnd.mapbox-vector-tile":mime_1.default.getType(i)||t.ContentType,r.set("Content-Type",e),r.send(t.Body)}}))}else if("file"===this.storageType){var s=path_1.default.join(this.uploadAdmin,e,t,i,o);if(fs_1.default.existsSync(s)){var l=o.split(".").pop();let e;e="mbtiles"===l?"application/vnd.mapbox-vector-tile":mime_1.default.getType(l)||"application/octet-stream",r.set("Content-Type",e),r.sendFile(s)}else r.status(404).send({message:"File not found"})}}catch(e){r.status(400).json({err:e})}}),this.init=()=>{var e=this.lib.express.Router();return this.lib.cors&&e.use(this.lib.cors()),e.post("/upload",this.lib.multer.single("file"),(e,t)=>{this.initUpload(e,t,"create")}),e.post("/update",this.lib.multer.single("file"),(e,t)=>{this.initUpload(e,t,"update")}),e.post("/delete",this.initDelete),e.post("/deleteDirectory",this.initDeleteDirectory),e.post("/batchDeleteDirectory",this.initBatchDeleteDirectory),e.get("/get/:bucketName/:assetCollectionName/:assetParentId/*",this.initGet),e},this.uploadAdmin=e,this.isProd=t,this.config=i,this.lib=a,this.storageType=null!=s?s:"firebase"}ensureDirectoryExists(e){fs_1.default.existsSync(e)||fs_1.default.mkdirSync(e,{recursive:!0})}getLocalFilePath(e,t,i,a){var s=this.uploadAdmin,s=path_1.default.join(s,e,t,i,a);return this.ensureDirectoryExists(path_1.default.dirname(s)),s}}exports.UploadWrapper=UploadWrapper;
|