@stemy/backend 2.9.5 → 2.9.8
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/bundles/stemy-backend.umd.js +151 -24
- package/bundles/stemy-backend.umd.js.map +1 -1
- package/bundles/stemy-backend.umd.min.js +1 -1
- package/bundles/stemy-backend.umd.min.js.map +1 -1
- package/common-types.d.ts +5 -5
- package/esm2015/common-types.js +1 -1
- package/esm2015/services/asset-processor.js +5 -2
- package/esm2015/services/assets.js +23 -1
- package/esm2015/services/entities/asset.js +1 -1
- package/esm2015/services/entities/temp-asset.js +65 -0
- package/fesm2015/stemy-backend.js +106 -20
- package/fesm2015/stemy-backend.js.map +1 -1
- package/package.json +1 -1
- package/services/assets.d.ts +1 -0
- package/services/entities/asset.d.ts +2 -2
- package/services/entities/temp-asset.d.ts +20 -0
- package/stemy-backend.metadata.json +1 -1
|
@@ -36,7 +36,7 @@ import * as Handlebars from 'handlebars';
|
|
|
36
36
|
import { compare } from 'bcrypt';
|
|
37
37
|
import moment from 'moment';
|
|
38
38
|
|
|
39
|
-
var __awaiter$
|
|
39
|
+
var __awaiter$w = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
40
40
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
41
41
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
42
42
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
@@ -238,7 +238,7 @@ function hydratePopulated(modelType, json) {
|
|
|
238
238
|
return object;
|
|
239
239
|
}
|
|
240
240
|
function paginateAggregations(model, aggregations, params, metaProjection = {}) {
|
|
241
|
-
return __awaiter$
|
|
241
|
+
return __awaiter$w(this, void 0, void 0, function* () {
|
|
242
242
|
const sortField = !isString(params.sort) || !params.sort ? null : (params.sort.startsWith("-") ? params.sort.substr(1) : params.sort);
|
|
243
243
|
const sortAggregation = !sortField ? [] : [{
|
|
244
244
|
$sort: { [sortField]: sortField == params.sort ? 1 : -1 }
|
|
@@ -337,7 +337,7 @@ function readFile(path) {
|
|
|
337
337
|
});
|
|
338
338
|
}
|
|
339
339
|
function readAndDeleteFile(path, timeout = 5000) {
|
|
340
|
-
return __awaiter$
|
|
340
|
+
return __awaiter$w(this, void 0, void 0, function* () {
|
|
341
341
|
const data = yield readFile(path);
|
|
342
342
|
setTimeout(() => {
|
|
343
343
|
unlink(path, () => {
|
|
@@ -347,7 +347,7 @@ function readAndDeleteFile(path, timeout = 5000) {
|
|
|
347
347
|
});
|
|
348
348
|
}
|
|
349
349
|
function writeFile(path, data) {
|
|
350
|
-
return __awaiter$
|
|
350
|
+
return __awaiter$w(this, void 0, void 0, function* () {
|
|
351
351
|
yield mkdirRecursive(dirname(path));
|
|
352
352
|
return new Promise((res, rej) => {
|
|
353
353
|
writeFile$1(path, data, err => {
|
|
@@ -426,7 +426,7 @@ function ResolveEntity(model, extraCheck) {
|
|
|
426
426
|
const paramName = modelName.toLowerCase();
|
|
427
427
|
return createParamDecorator({
|
|
428
428
|
required: false,
|
|
429
|
-
value: (action) => __awaiter$
|
|
429
|
+
value: (action) => __awaiter$w(this, void 0, void 0, function* () {
|
|
430
430
|
const req = action.request;
|
|
431
431
|
const token = req.header(`x-${paramName}-token`);
|
|
432
432
|
const id = req.params[`${paramName}Id`];
|
|
@@ -750,7 +750,7 @@ var __decorate$w = (this && this.__decorate) || function (decorators, target, ke
|
|
|
750
750
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
751
751
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
752
752
|
};
|
|
753
|
-
var __awaiter$
|
|
753
|
+
var __awaiter$v = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
754
754
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
755
755
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
756
756
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
@@ -786,7 +786,7 @@ let AssetProcessor = AssetProcessor_1 = class AssetProcessor {
|
|
|
786
786
|
}
|
|
787
787
|
static fileTypeFromBuffer(buffer) {
|
|
788
788
|
var _a;
|
|
789
|
-
return __awaiter$
|
|
789
|
+
return __awaiter$v(this, void 0, void 0, function* () {
|
|
790
790
|
const type = ((_a = yield fromBuffer(buffer)) !== null && _a !== void 0 ? _a : { ext: "txt", mime: "text/plain" });
|
|
791
791
|
if (AssetProcessor_1.checkTextFileType(type)) {
|
|
792
792
|
return AssetProcessor_1.fixTextFileType(type, buffer);
|
|
@@ -813,9 +813,9 @@ let AssetProcessor = AssetProcessor_1 = class AssetProcessor {
|
|
|
813
813
|
return imageTypes.indexOf(contentType) >= 0;
|
|
814
814
|
}
|
|
815
815
|
static copyImageMeta(buffer, metadata, fileType) {
|
|
816
|
-
return __awaiter$
|
|
816
|
+
return __awaiter$v(this, void 0, void 0, function* () {
|
|
817
817
|
if (fileType.mime === "image/svg+xml") {
|
|
818
|
-
const match = /<svg(
|
|
818
|
+
const match = /<svg([^<>]+)>/gi.exec(buffer.toString("utf8"));
|
|
819
819
|
if (match && match.length > 1) {
|
|
820
820
|
const attrs = match[1].match(/([a-z]+)="([^"]+)"/gi);
|
|
821
821
|
attrs.forEach(attr => {
|
|
@@ -830,6 +830,9 @@ let AssetProcessor = AssetProcessor_1 = class AssetProcessor {
|
|
|
830
830
|
metadata.width = Number(parts[0]) + Number(parts[2]);
|
|
831
831
|
metadata.height = Number(parts[1]) + Number(parts[3]);
|
|
832
832
|
}
|
|
833
|
+
if (!isNaN(metadata.width) && !isNaN(metadata.height)) {
|
|
834
|
+
metadata.svgSize = { x: metadata.width, y: metadata.height };
|
|
835
|
+
}
|
|
833
836
|
}
|
|
834
837
|
return buffer;
|
|
835
838
|
}
|
|
@@ -849,7 +852,7 @@ let AssetProcessor = AssetProcessor_1 = class AssetProcessor {
|
|
|
849
852
|
});
|
|
850
853
|
}
|
|
851
854
|
process(buffer, metadata, fileType) {
|
|
852
|
-
return __awaiter$
|
|
855
|
+
return __awaiter$v(this, void 0, void 0, function* () {
|
|
853
856
|
if (AssetProcessor_1.isImage(fileType.mime)) {
|
|
854
857
|
buffer = yield AssetProcessor_1.copyImageMeta(buffer, metadata, fileType);
|
|
855
858
|
}
|
|
@@ -926,7 +929,7 @@ var __decorate$u = (this && this.__decorate) || function (decorators, target, ke
|
|
|
926
929
|
var __metadata$o = (this && this.__metadata) || function (k, v) {
|
|
927
930
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
928
931
|
};
|
|
929
|
-
var __awaiter$
|
|
932
|
+
var __awaiter$u = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
930
933
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
931
934
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
932
935
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
@@ -952,7 +955,7 @@ let MongoConnector = class MongoConnector {
|
|
|
952
955
|
return this.fsBucket;
|
|
953
956
|
}
|
|
954
957
|
connect() {
|
|
955
|
-
return __awaiter$
|
|
958
|
+
return __awaiter$u(this, void 0, void 0, function* () {
|
|
956
959
|
if (this.db)
|
|
957
960
|
return this.db;
|
|
958
961
|
this.conn = (yield connect(this.configuration.resolve("mongoUri"), {
|
|
@@ -973,7 +976,7 @@ MongoConnector = __decorate$u([
|
|
|
973
976
|
__metadata$o("design:paramtypes", [Configuration])
|
|
974
977
|
], MongoConnector);
|
|
975
978
|
|
|
976
|
-
var __awaiter$
|
|
979
|
+
var __awaiter$t = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
977
980
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
978
981
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
979
982
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
@@ -995,7 +998,7 @@ class BaseEntity {
|
|
|
995
998
|
return this.collection.updateOne({ _id: this.mId }, { $set: this.toJSON() });
|
|
996
999
|
}
|
|
997
1000
|
load() {
|
|
998
|
-
return __awaiter$
|
|
1001
|
+
return __awaiter$t(this, void 0, void 0, function* () {
|
|
999
1002
|
const res = yield this.collection.findOne({ _id: this.mId });
|
|
1000
1003
|
this.deleted = !res;
|
|
1001
1004
|
this.data = res || {};
|
|
@@ -1010,7 +1013,7 @@ class BaseEntity {
|
|
|
1010
1013
|
}
|
|
1011
1014
|
}
|
|
1012
1015
|
|
|
1013
|
-
var __awaiter$
|
|
1016
|
+
var __awaiter$s = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
1014
1017
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
1015
1018
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
1016
1019
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
@@ -1051,7 +1054,7 @@ class Asset extends BaseEntity {
|
|
|
1051
1054
|
};
|
|
1052
1055
|
}
|
|
1053
1056
|
static toImage(stream, meta, params) {
|
|
1054
|
-
return __awaiter$
|
|
1057
|
+
return __awaiter$s(this, void 0, void 0, function* () {
|
|
1055
1058
|
params = params || {};
|
|
1056
1059
|
// Get default crop info
|
|
1057
1060
|
const crop = Asset.toCropRegion(meta.crop);
|
|
@@ -1137,7 +1140,7 @@ class Asset extends BaseEntity {
|
|
|
1137
1140
|
return this.bucket.openDownloadStream(this.mId);
|
|
1138
1141
|
}
|
|
1139
1142
|
unlink() {
|
|
1140
|
-
return __awaiter$
|
|
1143
|
+
return __awaiter$s(this, void 0, void 0, function* () {
|
|
1141
1144
|
return deleteFromBucket(this.bucket, this.mId);
|
|
1142
1145
|
});
|
|
1143
1146
|
}
|
|
@@ -1145,7 +1148,7 @@ class Asset extends BaseEntity {
|
|
|
1145
1148
|
return streamToBuffer(this.stream);
|
|
1146
1149
|
}
|
|
1147
1150
|
download(metadata) {
|
|
1148
|
-
return __awaiter$
|
|
1151
|
+
return __awaiter$s(this, void 0, void 0, function* () {
|
|
1149
1152
|
metadata = Object.assign(this.metadata, metadata || {});
|
|
1150
1153
|
metadata.downloadCount = isNaN(metadata.downloadCount) || !metadata.firstDownload
|
|
1151
1154
|
? 1
|
|
@@ -1157,17 +1160,79 @@ class Asset extends BaseEntity {
|
|
|
1157
1160
|
});
|
|
1158
1161
|
}
|
|
1159
1162
|
getImage(params = null) {
|
|
1160
|
-
return __awaiter$
|
|
1163
|
+
return __awaiter$s(this, void 0, void 0, function* () {
|
|
1161
1164
|
return Asset.toImage(this.stream, this.metadata, params);
|
|
1162
1165
|
});
|
|
1163
1166
|
}
|
|
1164
1167
|
downloadImage(params, metadata) {
|
|
1165
|
-
return __awaiter$
|
|
1168
|
+
return __awaiter$s(this, void 0, void 0, function* () {
|
|
1166
1169
|
return Asset.toImage(yield this.download(metadata), this.metadata, params);
|
|
1167
1170
|
});
|
|
1168
1171
|
}
|
|
1169
1172
|
}
|
|
1170
1173
|
|
|
1174
|
+
var __awaiter$r = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
1175
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
1176
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
1177
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
1178
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
1179
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
1180
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
1181
|
+
});
|
|
1182
|
+
};
|
|
1183
|
+
class TempAsset {
|
|
1184
|
+
constructor(buffer, filename, contentType, metadata) {
|
|
1185
|
+
this.buffer = buffer;
|
|
1186
|
+
this.filename = filename;
|
|
1187
|
+
this.contentType = contentType;
|
|
1188
|
+
this.metadata = metadata;
|
|
1189
|
+
this.id = new ObjectId().toHexString();
|
|
1190
|
+
}
|
|
1191
|
+
get stream() {
|
|
1192
|
+
return bufferToStream(this.buffer);
|
|
1193
|
+
}
|
|
1194
|
+
unlink() {
|
|
1195
|
+
return __awaiter$r(this, void 0, void 0, function* () {
|
|
1196
|
+
throw new Error(`Temp asset '${this.id}' can not be removed!`);
|
|
1197
|
+
});
|
|
1198
|
+
}
|
|
1199
|
+
getBuffer() {
|
|
1200
|
+
return __awaiter$r(this, void 0, void 0, function* () {
|
|
1201
|
+
return this.buffer;
|
|
1202
|
+
});
|
|
1203
|
+
}
|
|
1204
|
+
download(metadata) {
|
|
1205
|
+
return __awaiter$r(this, void 0, void 0, function* () {
|
|
1206
|
+
return this.stream;
|
|
1207
|
+
});
|
|
1208
|
+
}
|
|
1209
|
+
downloadImage(params, metadata) {
|
|
1210
|
+
Object.assign(this.metadata, metadata || {});
|
|
1211
|
+
return Asset.toImage(this.stream, this.metadata, params);
|
|
1212
|
+
}
|
|
1213
|
+
getImage(params) {
|
|
1214
|
+
return this.downloadImage(params);
|
|
1215
|
+
}
|
|
1216
|
+
save() {
|
|
1217
|
+
return __awaiter$r(this, void 0, void 0, function* () {
|
|
1218
|
+
return this;
|
|
1219
|
+
});
|
|
1220
|
+
}
|
|
1221
|
+
load() {
|
|
1222
|
+
return __awaiter$r(this, void 0, void 0, function* () {
|
|
1223
|
+
return this;
|
|
1224
|
+
});
|
|
1225
|
+
}
|
|
1226
|
+
toJSON() {
|
|
1227
|
+
return {
|
|
1228
|
+
id: this.id,
|
|
1229
|
+
filename: this.filename,
|
|
1230
|
+
contentType: this.contentType,
|
|
1231
|
+
metadata: this.metadata
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1171
1236
|
var __decorate$t = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
1172
1237
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1173
1238
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -1235,6 +1300,27 @@ let Assets = class Assets {
|
|
|
1235
1300
|
return this.writeBuffer(buffer, metadata);
|
|
1236
1301
|
});
|
|
1237
1302
|
}
|
|
1303
|
+
download(url, contentType = null) {
|
|
1304
|
+
return __awaiter$q(this, void 0, void 0, function* () {
|
|
1305
|
+
let buffer = (yield axios({ url, responseType: "arraybuffer" })).data;
|
|
1306
|
+
let fileType = { ext: "", mime: contentType };
|
|
1307
|
+
try {
|
|
1308
|
+
fileType = yield AssetProcessor.fileTypeFromBuffer(buffer);
|
|
1309
|
+
}
|
|
1310
|
+
catch (e) {
|
|
1311
|
+
if (!fileType.mime) {
|
|
1312
|
+
throw `Can't determine mime type`;
|
|
1313
|
+
}
|
|
1314
|
+
console.log(`Can't determine mime type`, e);
|
|
1315
|
+
}
|
|
1316
|
+
const metadata = {
|
|
1317
|
+
filename: url,
|
|
1318
|
+
extension: (fileType.ext || "").trim()
|
|
1319
|
+
};
|
|
1320
|
+
buffer = yield this.assetProcessor.process(buffer, metadata, fileType);
|
|
1321
|
+
return new TempAsset(buffer, url, fileType.mime, metadata);
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1238
1324
|
read(id) {
|
|
1239
1325
|
return __awaiter$q(this, void 0, void 0, function* () {
|
|
1240
1326
|
return !id ? null : this.find({ _id: new ObjectId(id) });
|