@stemy/backend 2.7.4 → 2.8.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.
@@ -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$u = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
39
+ var __awaiter$v = (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$u(this, void 0, void 0, function* () {
241
+ return __awaiter$v(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$u(this, void 0, void 0, function* () {
340
+ return __awaiter$v(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$u(this, void 0, void 0, function* () {
350
+ return __awaiter$v(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$u(this, void 0, void 0, function* () {
429
+ value: (action) => __awaiter$v(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$t = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
753
+ var __awaiter$u = (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); } }
@@ -775,7 +775,7 @@ const fontProps = [
775
775
  ];
776
776
  let AssetProcessor = AssetProcessor_1 = class AssetProcessor {
777
777
  static getMimeType(buffer, mimeType) {
778
- return __awaiter$t(this, void 0, void 0, function* () {
778
+ return __awaiter$u(this, void 0, void 0, function* () {
779
779
  try {
780
780
  mimeType = (yield fromBuffer(buffer)).mime;
781
781
  }
@@ -807,7 +807,7 @@ let AssetProcessor = AssetProcessor_1 = class AssetProcessor {
807
807
  return imageTypes.indexOf(contentType) >= 0;
808
808
  }
809
809
  static copyImageMeta(buffer, metadata) {
810
- return __awaiter$t(this, void 0, void 0, function* () {
810
+ return __awaiter$u(this, void 0, void 0, function* () {
811
811
  const output = yield sharp$3(buffer).rotate().toBuffer({ resolveWithObject: true });
812
812
  Object.assign(metadata, output.info);
813
813
  return output.data;
@@ -824,7 +824,7 @@ let AssetProcessor = AssetProcessor_1 = class AssetProcessor {
824
824
  });
825
825
  }
826
826
  process(buffer, metadata, contentType) {
827
- return __awaiter$t(this, void 0, void 0, function* () {
827
+ return __awaiter$u(this, void 0, void 0, function* () {
828
828
  if (AssetProcessor_1.isImage(contentType)) {
829
829
  buffer = yield AssetProcessor_1.copyImageMeta(buffer, metadata);
830
830
  }
@@ -899,7 +899,7 @@ var __decorate$u = (this && this.__decorate) || function (decorators, target, ke
899
899
  var __metadata$o = (this && this.__metadata) || function (k, v) {
900
900
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
901
901
  };
902
- var __awaiter$s = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
902
+ var __awaiter$t = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
903
903
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
904
904
  return new (P || (P = Promise))(function (resolve, reject) {
905
905
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -925,7 +925,7 @@ let MongoConnector = class MongoConnector {
925
925
  return this.fsBucket;
926
926
  }
927
927
  connect() {
928
- return __awaiter$s(this, void 0, void 0, function* () {
928
+ return __awaiter$t(this, void 0, void 0, function* () {
929
929
  if (this.db)
930
930
  return this.db;
931
931
  this.conn = (yield connect(this.configuration.resolve("mongoUri"), {
@@ -946,6 +946,43 @@ MongoConnector = __decorate$u([
946
946
  __metadata$o("design:paramtypes", [Configuration])
947
947
  ], MongoConnector);
948
948
 
949
+ var __awaiter$s = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
950
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
951
+ return new (P || (P = Promise))(function (resolve, reject) {
952
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
953
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
954
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
955
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
956
+ });
957
+ };
958
+ class BaseEntity {
959
+ constructor(mId, data, collection) {
960
+ this.mId = mId;
961
+ this.data = data;
962
+ this.collection = collection;
963
+ }
964
+ get id() {
965
+ return this.mId.toHexString();
966
+ }
967
+ save() {
968
+ return this.collection.updateOne({ _id: this.mId }, { $set: this.toJSON() });
969
+ }
970
+ load() {
971
+ return __awaiter$s(this, void 0, void 0, function* () {
972
+ const res = yield this.collection.findOne({ _id: this.mId });
973
+ this.deleted = !res;
974
+ this.data = res || {};
975
+ return this;
976
+ });
977
+ }
978
+ toJSON() {
979
+ const ret = Object.assign({}, this.data);
980
+ delete ret._id;
981
+ ret.id = this.id;
982
+ return ret;
983
+ }
984
+ }
985
+
949
986
  var __awaiter$r = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
950
987
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
951
988
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -962,14 +999,10 @@ const cropInterface = {
962
999
  w: "number",
963
1000
  h: "number"
964
1001
  };
965
- class Asset {
966
- constructor(fileId, filename, contentType, metadata, bucket, collection) {
967
- this.fileId = fileId;
968
- this.filename = filename;
969
- this.contentType = contentType;
970
- this.metadata = metadata;
1002
+ class Asset extends BaseEntity {
1003
+ constructor(id, data, collection, bucket) {
1004
+ super(id, data, collection);
971
1005
  this.bucket = bucket;
972
- this.collection = collection;
973
1006
  }
974
1007
  static toCropRegion(cropInfo) {
975
1008
  let crop = cropInfo;
@@ -1064,15 +1097,21 @@ class Asset {
1064
1097
  }
1065
1098
  });
1066
1099
  }
1067
- get id() {
1068
- return this.fileId.toHexString();
1100
+ get filename() {
1101
+ return this.data.filename;
1102
+ }
1103
+ get contentType() {
1104
+ return this.data.contentType;
1105
+ }
1106
+ get metadata() {
1107
+ return this.data.metadata;
1069
1108
  }
1070
1109
  get stream() {
1071
- return this.bucket.openDownloadStream(this.fileId);
1110
+ return this.bucket.openDownloadStream(this.mId);
1072
1111
  }
1073
1112
  unlink() {
1074
1113
  return __awaiter$r(this, void 0, void 0, function* () {
1075
- return deleteFromBucket(this.bucket, this.fileId);
1114
+ return deleteFromBucket(this.bucket, this.mId);
1076
1115
  });
1077
1116
  }
1078
1117
  getBuffer() {
@@ -1086,7 +1125,7 @@ class Asset {
1086
1125
  : metadata.downloadCount + 1;
1087
1126
  metadata.firstDownload = metadata.firstDownload || new Date();
1088
1127
  metadata.lastDownload = new Date();
1089
- yield this.collection.updateOne({ _id: this.fileId }, { $set: { metadata } });
1128
+ yield this.collection.updateOne({ _id: this.mId }, { $set: { metadata } });
1090
1129
  return this.stream;
1091
1130
  });
1092
1131
  }
@@ -1100,14 +1139,6 @@ class Asset {
1100
1139
  return Asset.toImage(yield this.download(metadata), this.metadata, params);
1101
1140
  });
1102
1141
  }
1103
- toJSON() {
1104
- return {
1105
- id: this.id,
1106
- filename: this.filename,
1107
- contentType: this.contentType,
1108
- metadata: this.metadata
1109
- };
1110
- }
1111
1142
  }
1112
1143
 
1113
1144
  var __decorate$t = (this && this.__decorate) || function (decorators, target, key, desc) {
@@ -1168,8 +1199,12 @@ let Assets = class Assets {
1168
1199
  reject(error.message || error);
1169
1200
  })
1170
1201
  .on("finish", () => {
1171
- const asset = new Asset(uploaderStream.id, metadata.filename, contentType, metadata, this.bucket, this.collection);
1172
- this.collection.updateOne({ _id: uploaderStream.id }, { $set: asset.toJSON() }).then(() => {
1202
+ const asset = new Asset(uploaderStream.id, {
1203
+ filename: metadata.filename,
1204
+ contentType,
1205
+ metadata
1206
+ }, this.collection, this.bucket);
1207
+ asset.save().then(() => {
1173
1208
  resolve(asset);
1174
1209
  }, error => {
1175
1210
  reject(error.message || error);
@@ -1200,7 +1235,7 @@ let Assets = class Assets {
1200
1235
  find(where) {
1201
1236
  return __awaiter$q(this, void 0, void 0, function* () {
1202
1237
  const data = yield this.collection.findOne(where);
1203
- return !data ? null : new Asset(data._id, data.filename, data.contentType, data.metadata, this.bucket, this.collection);
1238
+ return !data ? null : new Asset(data._id, data, this.collection, this.bucket);
1204
1239
  });
1205
1240
  }
1206
1241
  unlink(id) {
@@ -1227,52 +1262,61 @@ var __awaiter$p = (this && this.__awaiter) || function (thisArg, _arguments, P,
1227
1262
  step((generator = generator.apply(thisArg, _arguments || [])).next());
1228
1263
  });
1229
1264
  };
1230
- class LazyAsset {
1231
- constructor(lazyId, jobName, jobParams, jobQue, mProgressId, mAssetId, assets, progresses, jobMan, collection) {
1232
- this.lazyId = lazyId;
1233
- this.jobName = jobName;
1234
- this.jobParams = jobParams;
1235
- this.jobQue = jobQue;
1236
- this.mProgressId = mProgressId;
1237
- this.mAssetId = mAssetId;
1265
+ class LazyAsset extends BaseEntity {
1266
+ constructor(id, data, collection, assets, progresses, jobMan) {
1267
+ super(id, data, collection);
1238
1268
  this.assets = assets;
1239
1269
  this.progresses = progresses;
1240
1270
  this.jobMan = jobMan;
1241
- this.collection = collection;
1242
1271
  }
1243
- get id() {
1244
- return this.lazyId.toHexString();
1272
+ get jobName() {
1273
+ return this.data.jobName;
1274
+ }
1275
+ get jobParams() {
1276
+ return this.data.jobParams;
1277
+ }
1278
+ get jobQue() {
1279
+ return this.data.jobQue;
1245
1280
  }
1246
1281
  get progressId() {
1247
- return this.mProgressId;
1282
+ return this.data.progressId;
1248
1283
  }
1249
1284
  get assetId() {
1250
- return this.mAssetId;
1285
+ return this.data.assetId;
1251
1286
  }
1252
1287
  unlink() {
1253
1288
  return __awaiter$p(this, void 0, void 0, function* () {
1254
- yield this.collection.deleteOne({ _id: this.lazyId });
1255
- return deleteFromBucket(this.assets.bucket, new ObjectId(this.mAssetId));
1289
+ yield this.load();
1290
+ if (!this.progressId) {
1291
+ yield this.collection.deleteOne({ _id: this.mId });
1292
+ }
1293
+ return deleteFromBucket(this.assets.bucket, new ObjectId(this.assetId));
1256
1294
  });
1257
1295
  }
1258
1296
  startWorking() {
1259
- if (this.mProgressId)
1260
- return;
1261
- this.startWorkingOnAsset().then(() => {
1262
- console.log(`Started working on lazy asset: ${this.id}`);
1263
- }).catch(reason => {
1264
- console.log(`Can't start working on lazy asset: ${this.id}\nReason: ${reason}`);
1297
+ this.load().then(() => {
1298
+ if (this.deleted)
1299
+ return;
1300
+ const progressPromise = !this.progressId ? Promise.resolve(null) : this.progresses.get(this.progressId).then(p => p.cancel());
1301
+ progressPromise.then(() => {
1302
+ this.startWorkingOnAsset().then(() => {
1303
+ console.log(`Started working on lazy asset: ${this.id}`);
1304
+ }).catch(reason => {
1305
+ console.log(`Can't start working on lazy asset: ${this.id}\nReason: ${reason}`);
1306
+ });
1307
+ });
1265
1308
  });
1266
1309
  }
1267
1310
  loadAsset() {
1268
1311
  return __awaiter$p(this, void 0, void 0, function* () {
1269
- if (this.mAssetId) {
1270
- return this.assets.read(this.mAssetId);
1312
+ yield this.load();
1313
+ if (this.deleted)
1314
+ return null;
1315
+ if (this.assetId) {
1316
+ return this.assets.read(this.assetId);
1271
1317
  }
1272
- if (this.mProgressId) {
1273
- yield this.progresses.waitToFinish(this.mProgressId);
1274
- const data = yield this.collection.findOne({ _id: this.lazyId });
1275
- this.mAssetId = data.assetId;
1318
+ if (this.progressId) {
1319
+ yield this.progresses.waitToFinish(this.progressId);
1276
1320
  return this.loadAsset();
1277
1321
  }
1278
1322
  yield this.startWorkingOnAsset();
@@ -1281,30 +1325,17 @@ class LazyAsset {
1281
1325
  }
1282
1326
  writeAsset(asset) {
1283
1327
  return __awaiter$p(this, void 0, void 0, function* () {
1284
- this.mAssetId = asset.id;
1328
+ this.data.assetId = asset.id;
1285
1329
  yield this.save();
1286
1330
  return asset;
1287
1331
  });
1288
1332
  }
1289
- save() {
1290
- return this.collection.updateOne({ _id: this.lazyId }, { $set: this.toJSON() });
1291
- }
1292
- toJSON() {
1293
- return {
1294
- id: this.id,
1295
- jobName: this.jobName,
1296
- jobParams: this.jobParams,
1297
- jobQue: this.jobQue,
1298
- progressId: this.progressId,
1299
- assetId: this.assetId,
1300
- };
1301
- }
1302
1333
  startWorkingOnAsset() {
1303
1334
  return __awaiter$p(this, void 0, void 0, function* () {
1304
- const progress = yield this.progresses.create();
1305
- this.mProgressId = progress.id;
1335
+ const { id } = yield this.progresses.create();
1336
+ this.data.progressId = id;
1306
1337
  yield this.save();
1307
- yield this.jobMan.enqueueWithName(this.jobName, Object.assign(Object.assign({}, this.jobParams), { lazyId: this.id }));
1338
+ yield this.jobMan.enqueueWithName(this.data.jobName, Object.assign(Object.assign({}, this.data.jobParams), { lazyId: this.id }));
1308
1339
  });
1309
1340
  }
1310
1341
  }
@@ -1513,36 +1544,31 @@ var __awaiter$n = (this && this.__awaiter) || function (thisArg, _arguments, P,
1513
1544
  step((generator = generator.apply(thisArg, _arguments || [])).next());
1514
1545
  });
1515
1546
  };
1516
- class Progress {
1517
- constructor(progressId, mCurrent, mMax, mMessage, mError, client, collection) {
1518
- this.progressId = progressId;
1519
- this.mCurrent = mCurrent;
1520
- this.mMax = mMax;
1521
- this.mMessage = mMessage;
1522
- this.mError = mError;
1547
+ class Progress extends BaseEntity {
1548
+ constructor(id, data, collection, client) {
1549
+ super(id, data, collection);
1523
1550
  this.client = client;
1524
- this.collection = collection;
1525
- }
1526
- get id() {
1527
- return this.progressId.toHexString();
1528
1551
  }
1529
1552
  get current() {
1530
- return this.mCurrent;
1553
+ return this.data.current;
1531
1554
  }
1532
1555
  get max() {
1533
- return this.mMax;
1556
+ return this.data.max;
1534
1557
  }
1535
1558
  get message() {
1536
- return this.mMessage;
1559
+ return this.data.message;
1537
1560
  }
1538
1561
  get error() {
1539
- return this.mError;
1562
+ return this.data.error;
1563
+ }
1564
+ get canceled() {
1565
+ return this.data.canceled;
1540
1566
  }
1541
1567
  get percent() {
1542
- return this.mMax > 0 ? Math.round(this.mCurrent / this.mMax * 100) : 0;
1568
+ return this.max > 0 ? Math.round(this.current / this.max * 100) : 0;
1543
1569
  }
1544
1570
  get remaining() {
1545
- return this.mMax > 0 ? this.mMax - this.mCurrent : 0;
1571
+ return this.max > 0 ? this.max - this.current : 0;
1546
1572
  }
1547
1573
  createSubProgress(progressValue, max, message) {
1548
1574
  return __awaiter$n(this, void 0, void 0, function* () {
@@ -1550,10 +1576,10 @@ class Progress {
1550
1576
  yield this.advance(progressValue);
1551
1577
  }
1552
1578
  if (message !== null) {
1553
- this.mMessage = message;
1579
+ this.data.message = message;
1554
1580
  yield this.save();
1555
1581
  }
1556
- return new SubProgress(this, this.mCurrent, progressValue, Math.max(max, 1));
1582
+ return new SubProgress(this, this.current, progressValue, Math.max(max, 1));
1557
1583
  });
1558
1584
  }
1559
1585
  setMax(max) {
@@ -1561,13 +1587,19 @@ class Progress {
1561
1587
  if (isNaN(max) || max <= 0) {
1562
1588
  throw "Max progress value must be bigger than zero";
1563
1589
  }
1564
- this.mMax = max;
1590
+ this.data.max = max;
1591
+ yield this.save();
1592
+ });
1593
+ }
1594
+ setMessage(message) {
1595
+ return __awaiter$n(this, void 0, void 0, function* () {
1596
+ this.data.message = message;
1565
1597
  yield this.save();
1566
1598
  });
1567
1599
  }
1568
1600
  setError(error) {
1569
1601
  return __awaiter$n(this, void 0, void 0, function* () {
1570
- this.mError = error;
1602
+ this.data.error = error;
1571
1603
  yield this.save();
1572
1604
  });
1573
1605
  }
@@ -1576,63 +1608,60 @@ class Progress {
1576
1608
  if (isNaN(value) || value <= 0) {
1577
1609
  throw "Advance value must be bigger than zero";
1578
1610
  }
1579
- this.mCurrent = Math.min(this.mMax, this.mCurrent + value);
1611
+ yield this.load();
1612
+ if (this.deleted || this.canceled)
1613
+ return null;
1614
+ this.data.current = Math.min(this.max, this.current + value);
1580
1615
  yield this.save();
1581
1616
  if (!this.client)
1582
1617
  return;
1583
1618
  this.client.emit("background-progress", this.id);
1584
1619
  });
1585
1620
  }
1586
- toJSON() {
1587
- return {
1588
- id: this.id,
1589
- current: this.current,
1590
- max: this.max,
1591
- message: this.message,
1592
- error: this.error
1593
- };
1594
- }
1595
- save() {
1596
- return this.collection.updateOne({ _id: this.progressId }, { $set: this.toJSON() });
1621
+ cancel() {
1622
+ return __awaiter$n(this, void 0, void 0, function* () {
1623
+ this.data.canceled = true;
1624
+ yield this.save();
1625
+ });
1597
1626
  }
1598
1627
  }
1599
1628
  class SubProgress {
1600
- constructor(parent, progressFrom, progressValue, max = 100) {
1629
+ constructor(parent, progressFrom, progressValue, mMax = 100) {
1601
1630
  this.parent = parent;
1602
1631
  this.progressFrom = progressFrom;
1603
1632
  this.progressValue = progressValue;
1604
- this.max = max;
1633
+ this.mMax = mMax;
1605
1634
  if (progressFrom < 0) {
1606
1635
  throw "Progress from must be bigger than or zero";
1607
1636
  }
1608
1637
  if (progressValue <= 0) {
1609
1638
  throw "Progress value must be bigger than zero";
1610
1639
  }
1611
- this.currentValue = 0;
1640
+ this.mCurrent = 0;
1612
1641
  }
1613
1642
  get id() {
1614
1643
  return this.parent.id;
1615
1644
  }
1645
+ get current() {
1646
+ return this.mCurrent;
1647
+ }
1648
+ get max() {
1649
+ return this.mMax;
1650
+ }
1616
1651
  get message() {
1617
1652
  return this.parent.message;
1618
1653
  }
1619
- set message(value) {
1620
- this.parent.message = value;
1621
- }
1622
1654
  get error() {
1623
1655
  return this.parent.error;
1624
1656
  }
1625
- set error(value) {
1626
- this.parent.error = value;
1627
- }
1628
1657
  get percent() {
1629
1658
  return this.parent.percent;
1630
1659
  }
1631
- get current() {
1632
- return this.currentValue;
1633
- }
1634
1660
  get remaining() {
1635
- return this.max - this.currentValue;
1661
+ return this.max - this.mCurrent;
1662
+ }
1663
+ get canceled() {
1664
+ return !this.parent || this.parent.canceled;
1636
1665
  }
1637
1666
  createSubProgress(progressValue, max, message) {
1638
1667
  return __awaiter$n(this, void 0, void 0, function* () {
@@ -1640,8 +1669,7 @@ class SubProgress {
1640
1669
  yield this.advance(progressValue);
1641
1670
  }
1642
1671
  if (message !== null) {
1643
- this.message = message;
1644
- yield this.parent.save();
1672
+ yield this.setMessage(message);
1645
1673
  }
1646
1674
  return new SubProgress(this, this.current, progressValue, Math.max(max, 1));
1647
1675
  });
@@ -1651,14 +1679,22 @@ class SubProgress {
1651
1679
  if (isNaN(max) || max <= 0) {
1652
1680
  throw "Max progress value must be bigger than zero";
1653
1681
  }
1654
- this.max = max;
1682
+ this.mMax = max;
1655
1683
  yield this.save();
1656
1684
  });
1657
1685
  }
1686
+ setMessage(message) {
1687
+ return __awaiter$n(this, void 0, void 0, function* () {
1688
+ if (!this.parent)
1689
+ return null;
1690
+ yield this.parent.setMessage(message);
1691
+ });
1692
+ }
1658
1693
  setError(error) {
1659
1694
  return __awaiter$n(this, void 0, void 0, function* () {
1660
- this.error = error || null;
1661
- yield this.save();
1695
+ if (!this.parent)
1696
+ return null;
1697
+ yield this.parent.setError(error);
1662
1698
  });
1663
1699
  }
1664
1700
  advance(value = 1) {
@@ -1666,13 +1702,20 @@ class SubProgress {
1666
1702
  if (isNaN(value) || value <= 0) {
1667
1703
  throw "Advance value must be bigger than zero";
1668
1704
  }
1669
- this.currentValue = Math.min(this.max, this.currentValue + value);
1705
+ this.mCurrent = Math.min(this.max, this.mCurrent + value);
1670
1706
  yield this.save();
1671
1707
  });
1672
1708
  }
1709
+ cancel() {
1710
+ return __awaiter$n(this, void 0, void 0, function* () {
1711
+ if (!this.parent)
1712
+ return null;
1713
+ yield this.parent.cancel();
1714
+ });
1715
+ }
1673
1716
  save() {
1674
1717
  return __awaiter$n(this, void 0, void 0, function* () {
1675
- const ratio = this.max > 0 ? this.currentValue / this.max : 0;
1718
+ const ratio = this.max > 0 ? this.mCurrent / this.max : 0;
1676
1719
  const newProgress = this.progressFrom + Math.round(this.progressValue * ratio);
1677
1720
  const current = this.parent.current;
1678
1721
  if (newProgress <= current)
@@ -1680,6 +1723,11 @@ class SubProgress {
1680
1723
  yield this.parent.advance(newProgress);
1681
1724
  });
1682
1725
  }
1726
+ load() {
1727
+ return __awaiter$n(this, void 0, void 0, function* () {
1728
+ return null;
1729
+ });
1730
+ }
1683
1731
  toJSON() {
1684
1732
  return this.parent.toJSON();
1685
1733
  }
@@ -1740,7 +1788,7 @@ let Progresses = class Progresses {
1740
1788
  find(where) {
1741
1789
  return __awaiter$m(this, void 0, void 0, function* () {
1742
1790
  const data = yield this.collection.findOne(where);
1743
- return !data ? null : new Progress(data._id, data.current, data.max, data.message, data.error, this.client, this.collection);
1791
+ return !data ? null : new Progress(data._id, data, this.collection, this.client);
1744
1792
  });
1745
1793
  }
1746
1794
  create(max = 100) {
@@ -1748,11 +1796,15 @@ let Progresses = class Progresses {
1748
1796
  if (isNaN(max) || max <= 0) {
1749
1797
  throw "Max progress value must be bigger than zero";
1750
1798
  }
1751
- const res = yield this.collection.insertOne({
1799
+ const data = {
1752
1800
  current: 0,
1753
- max
1754
- });
1755
- return new Progress(res.insertedId, 0, max, "", "", this.client, this.collection);
1801
+ max: max,
1802
+ message: "",
1803
+ error: "",
1804
+ canceled: false
1805
+ };
1806
+ const res = yield this.collection.insertOne(data);
1807
+ return new Progress(res.insertedId, data, this.collection, this.client);
1756
1808
  });
1757
1809
  }
1758
1810
  remove(id) {
@@ -1797,12 +1849,16 @@ let LazyAssets = class LazyAssets {
1797
1849
  create(jobType, jobParams = {}, jobQue = "main") {
1798
1850
  return __awaiter$l(this, void 0, void 0, function* () {
1799
1851
  const jobName = this.jobMan.tryResolve(jobType, Object.assign(Object.assign({}, jobParams), { lazyId: "" }));
1800
- const res = yield this.collection.insertOne({
1852
+ const data = {
1801
1853
  jobName,
1802
1854
  jobParams,
1803
1855
  jobQue
1804
- });
1805
- return new LazyAsset(res.insertedId, jobName, jobParams, jobQue, null, null, this.assets, this.progresses, this.jobMan, this.collection);
1856
+ };
1857
+ const existingAsset = yield this.find(data);
1858
+ if (existingAsset)
1859
+ return existingAsset;
1860
+ const res = yield this.collection.insertOne(data);
1861
+ return new LazyAsset(res.insertedId, data, this.collection, this.assets, this.progresses, this.jobMan);
1806
1862
  });
1807
1863
  }
1808
1864
  read(id) {
@@ -1815,7 +1871,7 @@ let LazyAssets = class LazyAssets {
1815
1871
  const data = yield this.collection.findOne(where);
1816
1872
  return !data
1817
1873
  ? null
1818
- : new LazyAsset(data._id, data.jobName, data.jobParams, data.jobQue, data.progressId, data.assetId, this.assets, this.progresses, this.jobMan, this.collection);
1874
+ : new LazyAsset(data._id, data, this.collection, this.assets, this.progresses, this.jobMan);
1819
1875
  });
1820
1876
  }
1821
1877
  unlink(id) {
@@ -3527,9 +3583,14 @@ class LazyAssetGenerator {
3527
3583
  process() {
3528
3584
  return __awaiter$1(this, void 0, void 0, function* () {
3529
3585
  const lazyAsset = yield this.lazyAssets.read(this.lazyId);
3530
- const progress = yield this.progresses.get(lazyAsset.progressId);
3586
+ let progress = yield this.progresses.get(lazyAsset.progressId);
3587
+ if (!progress || progress.canceled)
3588
+ return null;
3531
3589
  try {
3532
3590
  const asset = yield this.generate(progress);
3591
+ progress = yield progress.load();
3592
+ if (!progress || progress.canceled)
3593
+ return null;
3533
3594
  yield lazyAsset.writeAsset(asset);
3534
3595
  }
3535
3596
  catch (e) {