@stemy/backend 2.7.3 → 2.8.2

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.
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('path'), require('body-parser'), require('jsonwebtoken'), require('tsyringe'), require('routing-controllers'), require('socket-controllers'), require('routing-controllers-openapi'), require('class-transformer/storage'), require('class-validator'), require('class-validator-jsonschema'), require('child_process'), require('crypto'), require('rxjs'), require('rxjs/internal/util/canReportError'), require('fs'), require('mongoose'), require('mongoose/lib/utils'), require('stream'), require('bson'), require('fontkit'), require('file-type'), require('sharp'), require('mongodb'), require('dotenv'), require('node-resque'), require('node-cron'), require('ioredis'), require('socket.io-client'), require('http'), require('express'), require('socket.io'), require('uuid'), require('nodemailer'), require('handlebars'), require('axios'), require('bcrypt'), require('moment')) :
3
- typeof define === 'function' && define.amd ? define('@stemy/backend', ['exports', 'path', 'body-parser', 'jsonwebtoken', 'tsyringe', 'routing-controllers', 'socket-controllers', 'routing-controllers-openapi', 'class-transformer/storage', 'class-validator', 'class-validator-jsonschema', 'child_process', 'crypto', 'rxjs', 'rxjs/internal/util/canReportError', 'fs', 'mongoose', 'mongoose/lib/utils', 'stream', 'bson', 'fontkit', 'file-type', 'sharp', 'mongodb', 'dotenv', 'node-resque', 'node-cron', 'ioredis', 'socket.io-client', 'http', 'express', 'socket.io', 'uuid', 'nodemailer', 'handlebars', 'axios', 'bcrypt', 'moment'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.stemy = global.stemy || {}, global.stemy.backend = {}), global.path, global["body-parser"], global.jsonwebtoken, global.tsyringe, global["routing-controllers"], global["socket-controllers"], global["routing-controllers-openapi"], global["class-transformer/storage"], global["class-validator"], global["class-validator-jsonschema"], global.child_process, global.crypto, global.rxjs, global.rxjs["internal/util/canReportError"], global.fs, global.mongoose, global["mongoose-lib-utils"], global.stream, global.bson, global.fontkit, global["file-type"], global.sharp, global.mongodb, global.dotenv, global["node-resque"], global["node-cron"], global.ioredis, global.socket["io-client"], global.http, global.express, global.socket.io, global.uuid, global.nodemailer, global.handlebars, global.axios, global.bcrypt, global.moment));
5
- })(this, (function (exports, path, bodyParser, jsonwebtoken, tsyringe, routingControllers, socketControllers, routingControllersOpenapi, storage, classValidator, classValidatorJsonschema, child_process, crypto, rxjs, canReportError, fs, mongoose, utils, stream, bson, fontKit_, fileType, sharp_, mongodb, dotenv, nodeResque, nodeCron, ioredis, socket_io_client, http, express_, socket_io, uuid, nodemailer, Handlebars, axios, bcrypt, moment) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('path'), require('body-parser'), require('jsonwebtoken'), require('tsyringe'), require('routing-controllers'), require('socket-controllers'), require('routing-controllers-openapi'), require('class-transformer/storage'), require('class-validator'), require('class-validator-jsonschema'), require('child_process'), require('crypto'), require('rxjs'), require('rxjs/internal/util/canReportError'), require('fs'), require('mongoose'), require('mongoose/lib/utils'), require('stream'), require('bson'), require('fontkit'), require('file-type'), require('sharp'), require('axios'), require('mongodb'), require('dotenv'), require('node-resque'), require('node-cron'), require('ioredis'), require('socket.io-client'), require('http'), require('express'), require('socket.io'), require('uuid'), require('nodemailer'), require('handlebars'), require('bcrypt'), require('moment')) :
3
+ typeof define === 'function' && define.amd ? define('@stemy/backend', ['exports', 'path', 'body-parser', 'jsonwebtoken', 'tsyringe', 'routing-controllers', 'socket-controllers', 'routing-controllers-openapi', 'class-transformer/storage', 'class-validator', 'class-validator-jsonschema', 'child_process', 'crypto', 'rxjs', 'rxjs/internal/util/canReportError', 'fs', 'mongoose', 'mongoose/lib/utils', 'stream', 'bson', 'fontkit', 'file-type', 'sharp', 'axios', 'mongodb', 'dotenv', 'node-resque', 'node-cron', 'ioredis', 'socket.io-client', 'http', 'express', 'socket.io', 'uuid', 'nodemailer', 'handlebars', 'bcrypt', 'moment'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.stemy = global.stemy || {}, global.stemy.backend = {}), global.path, global["body-parser"], global.jsonwebtoken, global.tsyringe, global["routing-controllers"], global["socket-controllers"], global["routing-controllers-openapi"], global["class-transformer/storage"], global["class-validator"], global["class-validator-jsonschema"], global.child_process, global.crypto, global.rxjs, global.rxjs["internal/util/canReportError"], global.fs, global.mongoose, global["mongoose-lib-utils"], global.stream, global.bson, global.fontkit, global["file-type"], global.sharp, global.axios, global.mongodb, global.dotenv, global["node-resque"], global["node-cron"], global.ioredis, global.socket["io-client"], global.http, global.express, global.socket.io, global.uuid, global.nodemailer, global.handlebars, global.bcrypt, global.moment));
5
+ })(this, (function (exports, path, bodyParser, jsonwebtoken, tsyringe, routingControllers, socketControllers, routingControllersOpenapi, storage, classValidator, classValidatorJsonschema, child_process, crypto, rxjs, canReportError, fs, mongoose, utils, stream, bson, fontKit_, fileType, sharp_, axios, mongodb, dotenv, nodeResque, nodeCron, ioredis, socket_io_client, http, express_, socket_io, uuid, nodemailer, Handlebars, bcrypt, moment) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
@@ -26,13 +26,13 @@
26
26
 
27
27
  var fontKit___default = /*#__PURE__*/_interopDefaultLegacy(fontKit_);
28
28
  var sharp___default = /*#__PURE__*/_interopDefaultLegacy(sharp_);
29
+ var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
29
30
  var dotenv__default = /*#__PURE__*/_interopDefaultLegacy(dotenv);
30
31
  var ioredis__default = /*#__PURE__*/_interopDefaultLegacy(ioredis);
31
32
  var socket_io_client__default = /*#__PURE__*/_interopDefaultLegacy(socket_io_client);
32
33
  var express___default = /*#__PURE__*/_interopDefaultLegacy(express_);
33
34
  var socket_io__default = /*#__PURE__*/_interopDefaultLegacy(socket_io);
34
35
  var Handlebars__namespace = /*#__PURE__*/_interopNamespace(Handlebars);
35
- var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
36
36
  var moment__default = /*#__PURE__*/_interopDefaultLegacy(moment);
37
37
 
38
38
  /*! *****************************************************************************
@@ -106,7 +106,7 @@
106
106
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
107
107
  return Reflect.metadata(metadataKey, metadataValue);
108
108
  }
109
- function __awaiter$v(thisArg, _arguments, P, generator) {
109
+ function __awaiter$w(thisArg, _arguments, P, generator) {
110
110
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
111
111
  return new (P || (P = Promise))(function (resolve, reject) {
112
112
  function fulfilled(value) { try {
@@ -353,7 +353,7 @@
353
353
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
354
354
  }
355
355
 
356
- var __awaiter$u = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
356
+ var __awaiter$v = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
357
357
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
358
358
  return new (P || (P = Promise))(function (resolve, reject) {
359
359
  function fulfilled(value) { try {
@@ -595,7 +595,7 @@
595
595
  }
596
596
  function paginateAggregations(model, aggregations, params, metaProjection) {
597
597
  if (metaProjection === void 0) { metaProjection = {}; }
598
- return __awaiter$u(this, void 0, void 0, function () {
598
+ return __awaiter$v(this, void 0, void 0, function () {
599
599
  var sortField, sortAggregation, result, pagination;
600
600
  var _a;
601
601
  return __generator(this, function (_b) {
@@ -706,7 +706,7 @@
706
706
  }
707
707
  function readAndDeleteFile(path, timeout) {
708
708
  if (timeout === void 0) { timeout = 5000; }
709
- return __awaiter$u(this, void 0, void 0, function () {
709
+ return __awaiter$v(this, void 0, void 0, function () {
710
710
  var data;
711
711
  return __generator(this, function (_a) {
712
712
  switch (_a.label) {
@@ -723,7 +723,7 @@
723
723
  });
724
724
  }
725
725
  function writeFile(path$1, data) {
726
- return __awaiter$u(this, void 0, void 0, function () {
726
+ return __awaiter$v(this, void 0, void 0, function () {
727
727
  return __generator(this, function (_a) {
728
728
  switch (_a.label) {
729
729
  case 0: return [4 /*yield*/, mkdirRecursive(path.dirname(path$1))];
@@ -811,7 +811,7 @@
811
811
  var paramName = modelName.toLowerCase();
812
812
  return routingControllers.createParamDecorator({
813
813
  required: false,
814
- value: function (action) { return __awaiter$u(_this, void 0, void 0, function () {
814
+ value: function (action) { return __awaiter$v(_this, void 0, void 0, function () {
815
815
  var req, token, id, query, doc, _a, _b, e_3;
816
816
  return __generator(this, function (_c) {
817
817
  switch (_c.label) {
@@ -1186,7 +1186,7 @@
1186
1186
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1187
1187
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1188
1188
  };
1189
- var __awaiter$t = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
1189
+ var __awaiter$u = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
1190
1190
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1191
1191
  return new (P || (P = Promise))(function (resolve, reject) {
1192
1192
  function fulfilled(value) { try {
@@ -1223,7 +1223,7 @@
1223
1223
  function AssetProcessor() {
1224
1224
  }
1225
1225
  AssetProcessor.getMimeType = function (buffer, mimeType) {
1226
- return __awaiter$t(this, void 0, void 0, function () {
1226
+ return __awaiter$u(this, void 0, void 0, function () {
1227
1227
  var e_1;
1228
1228
  return __generator(this, function (_a) {
1229
1229
  switch (_a.label) {
@@ -1264,7 +1264,7 @@
1264
1264
  return imageTypes.indexOf(contentType) >= 0;
1265
1265
  };
1266
1266
  AssetProcessor.copyImageMeta = function (buffer, metadata) {
1267
- return __awaiter$t(this, void 0, void 0, function () {
1267
+ return __awaiter$u(this, void 0, void 0, function () {
1268
1268
  var output;
1269
1269
  return __generator(this, function (_a) {
1270
1270
  switch (_a.label) {
@@ -1288,7 +1288,7 @@
1288
1288
  });
1289
1289
  };
1290
1290
  AssetProcessor.prototype.process = function (buffer, metadata, contentType) {
1291
- return __awaiter$t(this, void 0, void 0, function () {
1291
+ return __awaiter$u(this, void 0, void 0, function () {
1292
1292
  return __generator(this, function (_a) {
1293
1293
  switch (_a.label) {
1294
1294
  case 0:
@@ -1384,7 +1384,7 @@
1384
1384
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
1385
1385
  return Reflect.metadata(k, v);
1386
1386
  };
1387
- var __awaiter$s = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
1387
+ var __awaiter$t = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
1388
1388
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1389
1389
  return new (P || (P = Promise))(function (resolve, reject) {
1390
1390
  function fulfilled(value) { try {
@@ -1432,7 +1432,7 @@
1432
1432
  configurable: true
1433
1433
  });
1434
1434
  MongoConnector.prototype.connect = function () {
1435
- return __awaiter$s(this, void 0, void 0, function () {
1435
+ return __awaiter$t(this, void 0, void 0, function () {
1436
1436
  var _a;
1437
1437
  return __generator(this, function (_b) {
1438
1438
  switch (_b.label) {
@@ -1464,6 +1464,65 @@
1464
1464
  __metadata$o("design:paramtypes", [exports.Configuration])
1465
1465
  ], exports.MongoConnector);
1466
1466
 
1467
+ var __awaiter$s = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
1468
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1469
+ return new (P || (P = Promise))(function (resolve, reject) {
1470
+ function fulfilled(value) { try {
1471
+ step(generator.next(value));
1472
+ }
1473
+ catch (e) {
1474
+ reject(e);
1475
+ } }
1476
+ function rejected(value) { try {
1477
+ step(generator["throw"](value));
1478
+ }
1479
+ catch (e) {
1480
+ reject(e);
1481
+ } }
1482
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
1483
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
1484
+ });
1485
+ };
1486
+ var BaseEntity = /** @class */ (function () {
1487
+ function BaseEntity(mId, data, collection) {
1488
+ this.mId = mId;
1489
+ this.data = data;
1490
+ this.collection = collection;
1491
+ }
1492
+ Object.defineProperty(BaseEntity.prototype, "id", {
1493
+ get: function () {
1494
+ return this.mId.toHexString();
1495
+ },
1496
+ enumerable: false,
1497
+ configurable: true
1498
+ });
1499
+ BaseEntity.prototype.save = function () {
1500
+ return this.collection.updateOne({ _id: this.mId }, { $set: this.toJSON() });
1501
+ };
1502
+ BaseEntity.prototype.load = function () {
1503
+ return __awaiter$s(this, void 0, void 0, function () {
1504
+ var res;
1505
+ return __generator(this, function (_a) {
1506
+ switch (_a.label) {
1507
+ case 0: return [4 /*yield*/, this.collection.findOne({ _id: this.mId })];
1508
+ case 1:
1509
+ res = _a.sent();
1510
+ this.deleted = !res;
1511
+ this.data = res || {};
1512
+ return [2 /*return*/, this];
1513
+ }
1514
+ });
1515
+ });
1516
+ };
1517
+ BaseEntity.prototype.toJSON = function () {
1518
+ var ret = Object.assign({}, this.data);
1519
+ delete ret._id;
1520
+ ret.id = this.id;
1521
+ return ret;
1522
+ };
1523
+ return BaseEntity;
1524
+ }());
1525
+
1467
1526
  var __awaiter$r = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
1468
1527
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1469
1528
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -1490,14 +1549,12 @@
1490
1549
  w: "number",
1491
1550
  h: "number"
1492
1551
  };
1493
- var Asset = /** @class */ (function () {
1494
- function Asset(fileId, filename, contentType, metadata, bucket, collection) {
1495
- this.fileId = fileId;
1496
- this.filename = filename;
1497
- this.contentType = contentType;
1498
- this.metadata = metadata;
1499
- this.bucket = bucket;
1500
- this.collection = collection;
1552
+ var Asset = /** @class */ (function (_super) {
1553
+ __extends(Asset, _super);
1554
+ function Asset(id, data, collection, bucket) {
1555
+ var _this = _super.call(this, id, data, collection) || this;
1556
+ _this.bucket = bucket;
1557
+ return _this;
1501
1558
  }
1502
1559
  Asset.toCropRegion = function (cropInfo) {
1503
1560
  var crop = cropInfo;
@@ -1612,16 +1669,30 @@
1612
1669
  });
1613
1670
  });
1614
1671
  };
1615
- Object.defineProperty(Asset.prototype, "id", {
1672
+ Object.defineProperty(Asset.prototype, "filename", {
1616
1673
  get: function () {
1617
- return this.fileId.toHexString();
1674
+ return this.data.filename;
1675
+ },
1676
+ enumerable: false,
1677
+ configurable: true
1678
+ });
1679
+ Object.defineProperty(Asset.prototype, "contentType", {
1680
+ get: function () {
1681
+ return this.data.contentType;
1682
+ },
1683
+ enumerable: false,
1684
+ configurable: true
1685
+ });
1686
+ Object.defineProperty(Asset.prototype, "metadata", {
1687
+ get: function () {
1688
+ return this.data.metadata;
1618
1689
  },
1619
1690
  enumerable: false,
1620
1691
  configurable: true
1621
1692
  });
1622
1693
  Object.defineProperty(Asset.prototype, "stream", {
1623
1694
  get: function () {
1624
- return this.bucket.openDownloadStream(this.fileId);
1695
+ return this.bucket.openDownloadStream(this.mId);
1625
1696
  },
1626
1697
  enumerable: false,
1627
1698
  configurable: true
@@ -1629,7 +1700,7 @@
1629
1700
  Asset.prototype.unlink = function () {
1630
1701
  return __awaiter$r(this, void 0, void 0, function () {
1631
1702
  return __generator(this, function (_a) {
1632
- return [2 /*return*/, deleteFromBucket(this.bucket, this.fileId)];
1703
+ return [2 /*return*/, deleteFromBucket(this.bucket, this.mId)];
1633
1704
  });
1634
1705
  });
1635
1706
  };
@@ -1647,7 +1718,7 @@
1647
1718
  : metadata.downloadCount + 1;
1648
1719
  metadata.firstDownload = metadata.firstDownload || new Date();
1649
1720
  metadata.lastDownload = new Date();
1650
- return [4 /*yield*/, this.collection.updateOne({ _id: this.fileId }, { $set: { metadata: metadata } })];
1721
+ return [4 /*yield*/, this.collection.updateOne({ _id: this.mId }, { $set: { metadata: metadata } })];
1651
1722
  case 1:
1652
1723
  _a.sent();
1653
1724
  return [2 /*return*/, this.stream];
@@ -1676,16 +1747,8 @@
1676
1747
  });
1677
1748
  });
1678
1749
  };
1679
- Asset.prototype.toJSON = function () {
1680
- return {
1681
- id: this.id,
1682
- filename: this.filename,
1683
- contentType: this.contentType,
1684
- metadata: this.metadata
1685
- };
1686
- };
1687
1750
  return Asset;
1688
- }());
1751
+ }(BaseEntity));
1689
1752
 
1690
1753
  var __decorate$t = (this && this.__decorate) || function (decorators, target, key, desc) {
1691
1754
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -1773,8 +1836,12 @@
1773
1836
  reject(error.message || error);
1774
1837
  })
1775
1838
  .on("finish", function () {
1776
- var asset = new Asset(uploaderStream.id, metadata.filename, contentType, metadata, _this.bucket, _this.collection);
1777
- _this.collection.updateOne({ _id: uploaderStream.id }, { $set: asset.toJSON() }).then(function () {
1839
+ var asset = new Asset(uploaderStream.id, {
1840
+ filename: metadata.filename,
1841
+ contentType: contentType,
1842
+ metadata: metadata
1843
+ }, _this.collection, _this.bucket);
1844
+ asset.save().then(function () {
1778
1845
  resolve(asset);
1779
1846
  }, function (error) {
1780
1847
  reject(error.message || error);
@@ -1803,6 +1870,20 @@
1803
1870
  });
1804
1871
  });
1805
1872
  };
1873
+ Assets.prototype.writeUrl = function (url, metadata) {
1874
+ if (metadata === void 0) { metadata = null; }
1875
+ return __awaiter$q(this, void 0, void 0, function () {
1876
+ var buffer;
1877
+ return __generator(this, function (_b) {
1878
+ switch (_b.label) {
1879
+ case 0: return [4 /*yield*/, axios__default["default"]({ url: url, responseType: "arraybuffer" })];
1880
+ case 1:
1881
+ buffer = (_b.sent()).data;
1882
+ return [2 /*return*/, this.writeBuffer(buffer, metadata)];
1883
+ }
1884
+ });
1885
+ });
1886
+ };
1806
1887
  Assets.prototype.read = function (id) {
1807
1888
  return __awaiter$q(this, void 0, void 0, function () {
1808
1889
  return __generator(this, function (_b) {
@@ -1818,7 +1899,39 @@
1818
1899
  case 0: return [4 /*yield*/, this.collection.findOne(where)];
1819
1900
  case 1:
1820
1901
  data = _b.sent();
1821
- return [2 /*return*/, !data ? null : new Asset(data._id, data.filename, data.contentType, data.metadata, this.bucket, this.collection)];
1902
+ return [2 /*return*/, !data ? null : new Asset(data._id, data, this.collection, this.bucket)];
1903
+ }
1904
+ });
1905
+ });
1906
+ };
1907
+ Assets.prototype.findMany = function (where) {
1908
+ return __awaiter$q(this, void 0, void 0, function () {
1909
+ var cursor, items, result, items_1, items_1_1, item;
1910
+ var e_2, _b;
1911
+ return __generator(this, function (_c) {
1912
+ switch (_c.label) {
1913
+ case 0:
1914
+ cursor = this.collection.find(where);
1915
+ return [4 /*yield*/, cursor.toArray()];
1916
+ case 1:
1917
+ items = (_c.sent()) || [];
1918
+ result = [];
1919
+ try {
1920
+ for (items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
1921
+ item = items_1_1.value;
1922
+ if (!item)
1923
+ continue;
1924
+ result.push(new Asset(item._id, item, this.collection, this.bucket));
1925
+ }
1926
+ }
1927
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
1928
+ finally {
1929
+ try {
1930
+ if (items_1_1 && !items_1_1.done && (_b = items_1.return)) _b.call(items_1);
1931
+ }
1932
+ finally { if (e_2) throw e_2.error; }
1933
+ }
1934
+ return [2 /*return*/, result];
1822
1935
  }
1823
1936
  });
1824
1937
  });
@@ -1865,36 +1978,46 @@
1865
1978
  step((generator = generator.apply(thisArg, _arguments || [])).next());
1866
1979
  });
1867
1980
  };
1868
- var LazyAsset = /** @class */ (function () {
1869
- function LazyAsset(lazyId, jobName, jobParams, jobQue, mProgressId, mAssetId, assets, progresses, jobMan, collection) {
1870
- this.lazyId = lazyId;
1871
- this.jobName = jobName;
1872
- this.jobParams = jobParams;
1873
- this.jobQue = jobQue;
1874
- this.mProgressId = mProgressId;
1875
- this.mAssetId = mAssetId;
1876
- this.assets = assets;
1877
- this.progresses = progresses;
1878
- this.jobMan = jobMan;
1879
- this.collection = collection;
1981
+ var LazyAsset = /** @class */ (function (_super) {
1982
+ __extends(LazyAsset, _super);
1983
+ function LazyAsset(id, data, collection, assets, progresses, jobMan) {
1984
+ var _this = _super.call(this, id, data, collection) || this;
1985
+ _this.assets = assets;
1986
+ _this.progresses = progresses;
1987
+ _this.jobMan = jobMan;
1988
+ return _this;
1880
1989
  }
1881
- Object.defineProperty(LazyAsset.prototype, "id", {
1990
+ Object.defineProperty(LazyAsset.prototype, "jobName", {
1991
+ get: function () {
1992
+ return this.data.jobName;
1993
+ },
1994
+ enumerable: false,
1995
+ configurable: true
1996
+ });
1997
+ Object.defineProperty(LazyAsset.prototype, "jobParams", {
1882
1998
  get: function () {
1883
- return this.lazyId.toHexString();
1999
+ return this.data.jobParams;
2000
+ },
2001
+ enumerable: false,
2002
+ configurable: true
2003
+ });
2004
+ Object.defineProperty(LazyAsset.prototype, "jobQue", {
2005
+ get: function () {
2006
+ return this.data.jobQue;
1884
2007
  },
1885
2008
  enumerable: false,
1886
2009
  configurable: true
1887
2010
  });
1888
2011
  Object.defineProperty(LazyAsset.prototype, "progressId", {
1889
2012
  get: function () {
1890
- return this.mProgressId;
2013
+ return this.data.progressId;
1891
2014
  },
1892
2015
  enumerable: false,
1893
2016
  configurable: true
1894
2017
  });
1895
2018
  Object.defineProperty(LazyAsset.prototype, "assetId", {
1896
2019
  get: function () {
1897
- return this.mAssetId;
2020
+ return this.data.assetId;
1898
2021
  },
1899
2022
  enumerable: false,
1900
2023
  configurable: true
@@ -1903,41 +2026,50 @@
1903
2026
  return __awaiter$p(this, void 0, void 0, function () {
1904
2027
  return __generator(this, function (_a) {
1905
2028
  switch (_a.label) {
1906
- case 0: return [4 /*yield*/, this.collection.deleteOne({ _id: this.lazyId })];
2029
+ case 0: return [4 /*yield*/, this.load()];
1907
2030
  case 1:
1908
2031
  _a.sent();
1909
- return [2 /*return*/, deleteFromBucket(this.assets.bucket, new bson.ObjectId(this.mAssetId))];
2032
+ if (!!this.progressId) return [3 /*break*/, 3];
2033
+ return [4 /*yield*/, this.collection.deleteOne({ _id: this.mId })];
2034
+ case 2:
2035
+ _a.sent();
2036
+ _a.label = 3;
2037
+ case 3: return [2 /*return*/, deleteFromBucket(this.assets.bucket, new bson.ObjectId(this.assetId))];
1910
2038
  }
1911
2039
  });
1912
2040
  });
1913
2041
  };
1914
2042
  LazyAsset.prototype.startWorking = function () {
1915
2043
  var _this = this;
1916
- if (this.mProgressId)
1917
- return;
1918
- this.startWorkingOnAsset().then(function () {
1919
- console.log("Started working on lazy asset: " + _this.id);
1920
- }).catch(function (reason) {
1921
- console.log("Can't start working on lazy asset: " + _this.id + "\nReason: " + reason);
2044
+ this.load().then(function () {
2045
+ if (_this.deleted)
2046
+ return;
2047
+ var progressPromise = !_this.progressId ? Promise.resolve(null) : _this.progresses.get(_this.progressId).then(function (p) { return p.cancel(); });
2048
+ progressPromise.then(function () {
2049
+ _this.startWorkingOnAsset().then(function () {
2050
+ console.log("Started working on lazy asset: " + _this.id);
2051
+ }).catch(function (reason) {
2052
+ console.log("Can't start working on lazy asset: " + _this.id + "\nReason: " + reason);
2053
+ });
2054
+ });
1922
2055
  });
1923
2056
  };
1924
2057
  LazyAsset.prototype.loadAsset = function () {
1925
2058
  return __awaiter$p(this, void 0, void 0, function () {
1926
- var data;
1927
2059
  return __generator(this, function (_a) {
1928
2060
  switch (_a.label) {
1929
- case 0:
1930
- if (this.mAssetId) {
1931
- return [2 /*return*/, this.assets.read(this.mAssetId)];
1932
- }
1933
- if (!this.mProgressId) return [3 /*break*/, 3];
1934
- return [4 /*yield*/, this.progresses.waitToFinish(this.mProgressId)];
2061
+ case 0: return [4 /*yield*/, this.load()];
1935
2062
  case 1:
1936
2063
  _a.sent();
1937
- return [4 /*yield*/, this.collection.findOne({ _id: this.lazyId })];
2064
+ if (this.deleted)
2065
+ return [2 /*return*/, null];
2066
+ if (this.assetId) {
2067
+ return [2 /*return*/, this.assets.read(this.assetId)];
2068
+ }
2069
+ if (!this.progressId) return [3 /*break*/, 3];
2070
+ return [4 /*yield*/, this.progresses.waitToFinish(this.progressId)];
1938
2071
  case 2:
1939
- data = _a.sent();
1940
- this.mAssetId = data.assetId;
2072
+ _a.sent();
1941
2073
  return [2 /*return*/, this.loadAsset()];
1942
2074
  case 3: return [4 /*yield*/, this.startWorkingOnAsset()];
1943
2075
  case 4:
@@ -1952,7 +2084,7 @@
1952
2084
  return __generator(this, function (_a) {
1953
2085
  switch (_a.label) {
1954
2086
  case 0:
1955
- this.mAssetId = asset.id;
2087
+ this.data.assetId = asset.id;
1956
2088
  return [4 /*yield*/, this.save()];
1957
2089
  case 1:
1958
2090
  _a.sent();
@@ -1961,32 +2093,19 @@
1961
2093
  });
1962
2094
  });
1963
2095
  };
1964
- LazyAsset.prototype.save = function () {
1965
- return this.collection.updateOne({ _id: this.lazyId }, { $set: this.toJSON() });
1966
- };
1967
- LazyAsset.prototype.toJSON = function () {
1968
- return {
1969
- id: this.id,
1970
- jobName: this.jobName,
1971
- jobParams: this.jobParams,
1972
- jobQue: this.jobQue,
1973
- progressId: this.progressId,
1974
- assetId: this.assetId,
1975
- };
1976
- };
1977
2096
  LazyAsset.prototype.startWorkingOnAsset = function () {
1978
2097
  return __awaiter$p(this, void 0, void 0, function () {
1979
- var progress;
2098
+ var id;
1980
2099
  return __generator(this, function (_a) {
1981
2100
  switch (_a.label) {
1982
2101
  case 0: return [4 /*yield*/, this.progresses.create()];
1983
2102
  case 1:
1984
- progress = _a.sent();
1985
- this.mProgressId = progress.id;
2103
+ id = (_a.sent()).id;
2104
+ this.data.progressId = id;
1986
2105
  return [4 /*yield*/, this.save()];
1987
2106
  case 2:
1988
2107
  _a.sent();
1989
- return [4 /*yield*/, this.jobMan.enqueueWithName(this.jobName, Object.assign(Object.assign({}, this.jobParams), { lazyId: this.id }))];
2108
+ return [4 /*yield*/, this.jobMan.enqueueWithName(this.data.jobName, Object.assign(Object.assign({}, this.data.jobParams), { lazyId: this.id }))];
1990
2109
  case 3:
1991
2110
  _a.sent();
1992
2111
  return [2 /*return*/];
@@ -1995,7 +2114,7 @@
1995
2114
  });
1996
2115
  };
1997
2116
  return LazyAsset;
1998
- }());
2117
+ }(BaseEntity));
1999
2118
 
2000
2119
  var __decorate$s = (this && this.__decorate) || function (decorators, target, key, desc) {
2001
2120
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -2306,61 +2425,58 @@
2306
2425
  step((generator = generator.apply(thisArg, _arguments || [])).next());
2307
2426
  });
2308
2427
  };
2309
- var Progress = /** @class */ (function () {
2310
- function Progress(progressId, mCurrent, mMax, mMessage, mError, client, collection) {
2311
- this.progressId = progressId;
2312
- this.mCurrent = mCurrent;
2313
- this.mMax = mMax;
2314
- this.mMessage = mMessage;
2315
- this.mError = mError;
2316
- this.client = client;
2317
- this.collection = collection;
2428
+ var Progress = /** @class */ (function (_super) {
2429
+ __extends(Progress, _super);
2430
+ function Progress(id, data, collection, client) {
2431
+ var _this = _super.call(this, id, data, collection) || this;
2432
+ _this.client = client;
2433
+ return _this;
2318
2434
  }
2319
- Object.defineProperty(Progress.prototype, "id", {
2435
+ Object.defineProperty(Progress.prototype, "current", {
2320
2436
  get: function () {
2321
- return this.progressId.toHexString();
2437
+ return this.data.current;
2322
2438
  },
2323
2439
  enumerable: false,
2324
2440
  configurable: true
2325
2441
  });
2326
- Object.defineProperty(Progress.prototype, "current", {
2442
+ Object.defineProperty(Progress.prototype, "max", {
2327
2443
  get: function () {
2328
- return this.mCurrent;
2444
+ return this.data.max;
2329
2445
  },
2330
2446
  enumerable: false,
2331
2447
  configurable: true
2332
2448
  });
2333
- Object.defineProperty(Progress.prototype, "max", {
2449
+ Object.defineProperty(Progress.prototype, "message", {
2334
2450
  get: function () {
2335
- return this.mMax;
2451
+ return this.data.message;
2336
2452
  },
2337
2453
  enumerable: false,
2338
2454
  configurable: true
2339
2455
  });
2340
- Object.defineProperty(Progress.prototype, "message", {
2456
+ Object.defineProperty(Progress.prototype, "error", {
2341
2457
  get: function () {
2342
- return this.mMessage;
2458
+ return this.data.error;
2343
2459
  },
2344
2460
  enumerable: false,
2345
2461
  configurable: true
2346
2462
  });
2347
- Object.defineProperty(Progress.prototype, "error", {
2463
+ Object.defineProperty(Progress.prototype, "canceled", {
2348
2464
  get: function () {
2349
- return this.mError;
2465
+ return this.data.canceled;
2350
2466
  },
2351
2467
  enumerable: false,
2352
2468
  configurable: true
2353
2469
  });
2354
2470
  Object.defineProperty(Progress.prototype, "percent", {
2355
2471
  get: function () {
2356
- return this.mMax > 0 ? Math.round(this.mCurrent / this.mMax * 100) : 0;
2472
+ return this.max > 0 ? Math.round(this.current / this.max * 100) : 0;
2357
2473
  },
2358
2474
  enumerable: false,
2359
2475
  configurable: true
2360
2476
  });
2361
2477
  Object.defineProperty(Progress.prototype, "remaining", {
2362
2478
  get: function () {
2363
- return this.mMax > 0 ? this.mMax - this.mCurrent : 0;
2479
+ return this.max > 0 ? this.max - this.current : 0;
2364
2480
  },
2365
2481
  enumerable: false,
2366
2482
  configurable: true
@@ -2377,12 +2493,12 @@
2377
2493
  _a.label = 2;
2378
2494
  case 2:
2379
2495
  if (!(message !== null)) return [3 /*break*/, 4];
2380
- this.mMessage = message;
2496
+ this.data.message = message;
2381
2497
  return [4 /*yield*/, this.save()];
2382
2498
  case 3:
2383
2499
  _a.sent();
2384
2500
  _a.label = 4;
2385
- case 4: return [2 /*return*/, new SubProgress(this, this.mCurrent, progressValue, Math.max(max, 1))];
2501
+ case 4: return [2 /*return*/, new SubProgress(this, this.current, progressValue, Math.max(max, 1))];
2386
2502
  }
2387
2503
  });
2388
2504
  });
@@ -2395,7 +2511,21 @@
2395
2511
  if (isNaN(max) || max <= 0) {
2396
2512
  throw "Max progress value must be bigger than zero";
2397
2513
  }
2398
- this.mMax = max;
2514
+ this.data.max = max;
2515
+ return [4 /*yield*/, this.save()];
2516
+ case 1:
2517
+ _a.sent();
2518
+ return [2 /*return*/];
2519
+ }
2520
+ });
2521
+ });
2522
+ };
2523
+ Progress.prototype.setMessage = function (message) {
2524
+ return __awaiter$n(this, void 0, void 0, function () {
2525
+ return __generator(this, function (_a) {
2526
+ switch (_a.label) {
2527
+ case 0:
2528
+ this.data.message = message;
2399
2529
  return [4 /*yield*/, this.save()];
2400
2530
  case 1:
2401
2531
  _a.sent();
@@ -2409,7 +2539,7 @@
2409
2539
  return __generator(this, function (_a) {
2410
2540
  switch (_a.label) {
2411
2541
  case 0:
2412
- this.mError = error;
2542
+ this.data.error = error;
2413
2543
  return [4 /*yield*/, this.save()];
2414
2544
  case 1:
2415
2545
  _a.sent();
@@ -2427,9 +2557,14 @@
2427
2557
  if (isNaN(value) || value <= 0) {
2428
2558
  throw "Advance value must be bigger than zero";
2429
2559
  }
2430
- this.mCurrent = Math.min(this.mMax, this.mCurrent + value);
2431
- return [4 /*yield*/, this.save()];
2560
+ return [4 /*yield*/, this.load()];
2432
2561
  case 1:
2562
+ _a.sent();
2563
+ if (this.deleted || this.canceled)
2564
+ return [2 /*return*/, null];
2565
+ this.data.current = Math.min(this.max, this.current + value);
2566
+ return [4 /*yield*/, this.save()];
2567
+ case 2:
2433
2568
  _a.sent();
2434
2569
  if (!this.client)
2435
2570
  return [2 /*return*/];
@@ -2439,34 +2574,36 @@
2439
2574
  });
2440
2575
  });
2441
2576
  };
2442
- Progress.prototype.toJSON = function () {
2443
- return {
2444
- id: this.id,
2445
- current: this.current,
2446
- max: this.max,
2447
- message: this.message,
2448
- error: this.error
2449
- };
2450
- };
2451
- Progress.prototype.save = function () {
2452
- return this.collection.updateOne({ _id: this.progressId }, { $set: this.toJSON() });
2577
+ Progress.prototype.cancel = function () {
2578
+ return __awaiter$n(this, void 0, void 0, function () {
2579
+ return __generator(this, function (_a) {
2580
+ switch (_a.label) {
2581
+ case 0:
2582
+ this.data.canceled = true;
2583
+ return [4 /*yield*/, this.save()];
2584
+ case 1:
2585
+ _a.sent();
2586
+ return [2 /*return*/];
2587
+ }
2588
+ });
2589
+ });
2453
2590
  };
2454
2591
  return Progress;
2455
- }());
2592
+ }(BaseEntity));
2456
2593
  var SubProgress = /** @class */ (function () {
2457
- function SubProgress(parent, progressFrom, progressValue, max) {
2458
- if (max === void 0) { max = 100; }
2594
+ function SubProgress(parent, progressFrom, progressValue, mMax) {
2595
+ if (mMax === void 0) { mMax = 100; }
2459
2596
  this.parent = parent;
2460
2597
  this.progressFrom = progressFrom;
2461
2598
  this.progressValue = progressValue;
2462
- this.max = max;
2599
+ this.mMax = mMax;
2463
2600
  if (progressFrom < 0) {
2464
2601
  throw "Progress from must be bigger than or zero";
2465
2602
  }
2466
2603
  if (progressValue <= 0) {
2467
2604
  throw "Progress value must be bigger than zero";
2468
2605
  }
2469
- this.currentValue = 0;
2606
+ this.mCurrent = 0;
2470
2607
  }
2471
2608
  Object.defineProperty(SubProgress.prototype, "id", {
2472
2609
  get: function () {
@@ -2475,13 +2612,24 @@
2475
2612
  enumerable: false,
2476
2613
  configurable: true
2477
2614
  });
2615
+ Object.defineProperty(SubProgress.prototype, "current", {
2616
+ get: function () {
2617
+ return this.mCurrent;
2618
+ },
2619
+ enumerable: false,
2620
+ configurable: true
2621
+ });
2622
+ Object.defineProperty(SubProgress.prototype, "max", {
2623
+ get: function () {
2624
+ return this.mMax;
2625
+ },
2626
+ enumerable: false,
2627
+ configurable: true
2628
+ });
2478
2629
  Object.defineProperty(SubProgress.prototype, "message", {
2479
2630
  get: function () {
2480
2631
  return this.parent.message;
2481
2632
  },
2482
- set: function (value) {
2483
- this.parent.message = value;
2484
- },
2485
2633
  enumerable: false,
2486
2634
  configurable: true
2487
2635
  });
@@ -2489,9 +2637,6 @@
2489
2637
  get: function () {
2490
2638
  return this.parent.error;
2491
2639
  },
2492
- set: function (value) {
2493
- this.parent.error = value;
2494
- },
2495
2640
  enumerable: false,
2496
2641
  configurable: true
2497
2642
  });
@@ -2502,16 +2647,16 @@
2502
2647
  enumerable: false,
2503
2648
  configurable: true
2504
2649
  });
2505
- Object.defineProperty(SubProgress.prototype, "current", {
2650
+ Object.defineProperty(SubProgress.prototype, "remaining", {
2506
2651
  get: function () {
2507
- return this.currentValue;
2652
+ return this.max - this.mCurrent;
2508
2653
  },
2509
2654
  enumerable: false,
2510
2655
  configurable: true
2511
2656
  });
2512
- Object.defineProperty(SubProgress.prototype, "remaining", {
2657
+ Object.defineProperty(SubProgress.prototype, "canceled", {
2513
2658
  get: function () {
2514
- return this.max - this.currentValue;
2659
+ return !this.parent || this.parent.canceled;
2515
2660
  },
2516
2661
  enumerable: false,
2517
2662
  configurable: true
@@ -2528,8 +2673,7 @@
2528
2673
  _a.label = 2;
2529
2674
  case 2:
2530
2675
  if (!(message !== null)) return [3 /*break*/, 4];
2531
- this.message = message;
2532
- return [4 /*yield*/, this.parent.save()];
2676
+ return [4 /*yield*/, this.setMessage(message)];
2533
2677
  case 3:
2534
2678
  _a.sent();
2535
2679
  _a.label = 4;
@@ -2546,7 +2690,7 @@
2546
2690
  if (isNaN(max) || max <= 0) {
2547
2691
  throw "Max progress value must be bigger than zero";
2548
2692
  }
2549
- this.max = max;
2693
+ this.mMax = max;
2550
2694
  return [4 /*yield*/, this.save()];
2551
2695
  case 1:
2552
2696
  _a.sent();
@@ -2555,13 +2699,29 @@
2555
2699
  });
2556
2700
  });
2557
2701
  };
2702
+ SubProgress.prototype.setMessage = function (message) {
2703
+ return __awaiter$n(this, void 0, void 0, function () {
2704
+ return __generator(this, function (_a) {
2705
+ switch (_a.label) {
2706
+ case 0:
2707
+ if (!this.parent)
2708
+ return [2 /*return*/, null];
2709
+ return [4 /*yield*/, this.parent.setMessage(message)];
2710
+ case 1:
2711
+ _a.sent();
2712
+ return [2 /*return*/];
2713
+ }
2714
+ });
2715
+ });
2716
+ };
2558
2717
  SubProgress.prototype.setError = function (error) {
2559
2718
  return __awaiter$n(this, void 0, void 0, function () {
2560
2719
  return __generator(this, function (_a) {
2561
2720
  switch (_a.label) {
2562
2721
  case 0:
2563
- this.error = error || null;
2564
- return [4 /*yield*/, this.save()];
2722
+ if (!this.parent)
2723
+ return [2 /*return*/, null];
2724
+ return [4 /*yield*/, this.parent.setError(error)];
2565
2725
  case 1:
2566
2726
  _a.sent();
2567
2727
  return [2 /*return*/];
@@ -2578,7 +2738,7 @@
2578
2738
  if (isNaN(value) || value <= 0) {
2579
2739
  throw "Advance value must be bigger than zero";
2580
2740
  }
2581
- this.currentValue = Math.min(this.max, this.currentValue + value);
2741
+ this.mCurrent = Math.min(this.max, this.mCurrent + value);
2582
2742
  return [4 /*yield*/, this.save()];
2583
2743
  case 1:
2584
2744
  _a.sent();
@@ -2587,13 +2747,28 @@
2587
2747
  });
2588
2748
  });
2589
2749
  };
2750
+ SubProgress.prototype.cancel = function () {
2751
+ return __awaiter$n(this, void 0, void 0, function () {
2752
+ return __generator(this, function (_a) {
2753
+ switch (_a.label) {
2754
+ case 0:
2755
+ if (!this.parent)
2756
+ return [2 /*return*/, null];
2757
+ return [4 /*yield*/, this.parent.cancel()];
2758
+ case 1:
2759
+ _a.sent();
2760
+ return [2 /*return*/];
2761
+ }
2762
+ });
2763
+ });
2764
+ };
2590
2765
  SubProgress.prototype.save = function () {
2591
2766
  return __awaiter$n(this, void 0, void 0, function () {
2592
2767
  var ratio, newProgress, current;
2593
2768
  return __generator(this, function (_a) {
2594
2769
  switch (_a.label) {
2595
2770
  case 0:
2596
- ratio = this.max > 0 ? this.currentValue / this.max : 0;
2771
+ ratio = this.max > 0 ? this.mCurrent / this.max : 0;
2597
2772
  newProgress = this.progressFrom + Math.round(this.progressValue * ratio);
2598
2773
  current = this.parent.current;
2599
2774
  if (newProgress <= current)
@@ -2606,6 +2781,13 @@
2606
2781
  });
2607
2782
  });
2608
2783
  };
2784
+ SubProgress.prototype.load = function () {
2785
+ return __awaiter$n(this, void 0, void 0, function () {
2786
+ return __generator(this, function (_a) {
2787
+ return [2 /*return*/, null];
2788
+ });
2789
+ });
2790
+ };
2609
2791
  SubProgress.prototype.toJSON = function () {
2610
2792
  return this.parent.toJSON();
2611
2793
  };
@@ -2701,7 +2883,7 @@
2701
2883
  case 0: return [4 /*yield*/, this.collection.findOne(where)];
2702
2884
  case 1:
2703
2885
  data = _a.sent();
2704
- return [2 /*return*/, !data ? null : new Progress(data._id, data.current, data.max, data.message, data.error, this.client, this.collection)];
2886
+ return [2 /*return*/, !data ? null : new Progress(data._id, data, this.collection, this.client)];
2705
2887
  }
2706
2888
  });
2707
2889
  });
@@ -2709,20 +2891,24 @@
2709
2891
  Progresses.prototype.create = function (max) {
2710
2892
  if (max === void 0) { max = 100; }
2711
2893
  return __awaiter$m(this, void 0, void 0, function () {
2712
- var res;
2894
+ var data, res;
2713
2895
  return __generator(this, function (_a) {
2714
2896
  switch (_a.label) {
2715
2897
  case 0:
2716
2898
  if (isNaN(max) || max <= 0) {
2717
2899
  throw "Max progress value must be bigger than zero";
2718
2900
  }
2719
- return [4 /*yield*/, this.collection.insertOne({
2720
- current: 0,
2721
- max: max
2722
- })];
2901
+ data = {
2902
+ current: 0,
2903
+ max: max,
2904
+ message: "",
2905
+ error: "",
2906
+ canceled: false
2907
+ };
2908
+ return [4 /*yield*/, this.collection.insertOne(data)];
2723
2909
  case 1:
2724
2910
  res = _a.sent();
2725
- return [2 /*return*/, new Progress(res.insertedId, 0, max, "", "", this.client, this.collection)];
2911
+ return [2 /*return*/, new Progress(res.insertedId, data, this.collection, this.client)];
2726
2912
  }
2727
2913
  });
2728
2914
  });
@@ -2792,19 +2978,25 @@
2792
2978
  if (jobParams === void 0) { jobParams = {}; }
2793
2979
  if (jobQue === void 0) { jobQue = "main"; }
2794
2980
  return __awaiter$l(this, void 0, void 0, function () {
2795
- var jobName, res;
2981
+ var jobName, data, existingAsset, res;
2796
2982
  return __generator(this, function (_a) {
2797
2983
  switch (_a.label) {
2798
2984
  case 0:
2799
2985
  jobName = this.jobMan.tryResolve(jobType, Object.assign(Object.assign({}, jobParams), { lazyId: "" }));
2800
- return [4 /*yield*/, this.collection.insertOne({
2801
- jobName: jobName,
2802
- jobParams: jobParams,
2803
- jobQue: jobQue
2804
- })];
2986
+ data = {
2987
+ jobName: jobName,
2988
+ jobParams: jobParams,
2989
+ jobQue: jobQue
2990
+ };
2991
+ return [4 /*yield*/, this.find(data)];
2805
2992
  case 1:
2993
+ existingAsset = _a.sent();
2994
+ if (existingAsset)
2995
+ return [2 /*return*/, existingAsset];
2996
+ return [4 /*yield*/, this.collection.insertOne(data)];
2997
+ case 2:
2806
2998
  res = _a.sent();
2807
- return [2 /*return*/, new LazyAsset(res.insertedId, jobName, jobParams, jobQue, null, null, this.assets, this.progresses, this.jobMan, this.collection)];
2999
+ return [2 /*return*/, new LazyAsset(res.insertedId, data, this.collection, this.assets, this.progresses, this.jobMan)];
2808
3000
  }
2809
3001
  });
2810
3002
  });
@@ -2826,7 +3018,7 @@
2826
3018
  data = _a.sent();
2827
3019
  return [2 /*return*/, !data
2828
3020
  ? null
2829
- : new LazyAsset(data._id, data.jobName, data.jobParams, data.jobQue, data.progressId, data.assetId, this.assets, this.progresses, this.jobMan, this.collection)];
3021
+ : new LazyAsset(data._id, data, this.collection, this.assets, this.progresses, this.jobMan)];
2830
3022
  }
2831
3023
  });
2832
3024
  });
@@ -4330,6 +4522,26 @@
4330
4522
  });
4331
4523
  });
4332
4524
  };
4525
+ AssetsController.prototype.uploadUrl = function (body) {
4526
+ return __awaiter$7(this, void 0, void 0, function () {
4527
+ var asset, e_2, msg;
4528
+ return __generator(this, function (_c) {
4529
+ switch (_c.label) {
4530
+ case 0:
4531
+ _c.trys.push([0, 2, , 3]);
4532
+ return [4 /*yield*/, this.assets.writeUrl(body.url, body)];
4533
+ case 1:
4534
+ asset = _c.sent();
4535
+ return [2 /*return*/, asset.toJSON()];
4536
+ case 2:
4537
+ e_2 = _c.sent();
4538
+ msg = (e_2 === null || e_2 === void 0 ? void 0 : e_2.message) || e_2 || "Unknown error";
4539
+ throw new routingControllers.HttpError(400, "Asset can't be uploaded.\n" + msg);
4540
+ case 3: return [2 /*return*/];
4541
+ }
4542
+ });
4543
+ });
4544
+ };
4333
4545
  AssetsController.prototype.getImageRotation = function (id, params, rotation) {
4334
4546
  if (rotation === void 0) { rotation = 0; }
4335
4547
  var _a;
@@ -4393,6 +4605,14 @@
4393
4605
  __metadata$7("design:paramtypes", [Object]),
4394
4606
  __metadata$7("design:returntype", Promise)
4395
4607
  ], AssetsController.prototype, "upload", null);
4608
+ __decorate$a([
4609
+ routingControllers.Authorized(),
4610
+ routingControllers.Post("url"),
4611
+ __param$5(0, routingControllers.Body()),
4612
+ __metadata$7("design:type", Function),
4613
+ __metadata$7("design:paramtypes", [Object]),
4614
+ __metadata$7("design:returntype", Promise)
4615
+ ], AssetsController.prototype, "uploadUrl", null);
4396
4616
  __decorate$a([
4397
4617
  routingControllers.Get("/image/:id/:rotation"),
4398
4618
  __param$5(0, routingControllers.Param("id")), __param$5(1, routingControllers.QueryParams()), __param$5(2, routingControllers.Param("rotation")),
@@ -5003,8 +5223,9 @@
5003
5223
  ], CompressionMiddleware);
5004
5224
 
5005
5225
  var Tree = /** @class */ (function () {
5006
- function Tree(container, path) {
5226
+ function Tree(container, exists, path) {
5007
5227
  this.container = container;
5228
+ this.exists = exists;
5008
5229
  this.path = path;
5009
5230
  this.map = new Map();
5010
5231
  }
@@ -5016,7 +5237,7 @@
5016
5237
  configurable: true
5017
5238
  });
5018
5239
  Tree.prototype.resolveService = function () {
5019
- return this.container.resolve(this.path);
5240
+ return !this.exists ? null : this.container.resolve(this.path);
5020
5241
  };
5021
5242
  Tree.prototype.resolveLeaves = function () {
5022
5243
  var map;
@@ -5058,7 +5279,7 @@
5058
5279
  parentTree = parentTree.resolveAncestor(path);
5059
5280
  }
5060
5281
  catch (e) {
5061
- parentTree = new Tree(this.container, "");
5282
+ parentTree = new Tree(this.container, false, "");
5062
5283
  }
5063
5284
  var pathParts = path.split(".");
5064
5285
  var tree = this;
@@ -5092,7 +5313,7 @@
5092
5313
  Tree.prototype.resolvePath = function (path, throwError) {
5093
5314
  if (throwError === void 0) { throwError = true; }
5094
5315
  var absolutePath = !this.path ? path : this.path + "." + path;
5095
- var tree = new Tree(this.container, absolutePath);
5316
+ var tree = new Tree(this.container, false, absolutePath);
5096
5317
  try {
5097
5318
  tree = this.resolveAncestor(path);
5098
5319
  }
@@ -5111,29 +5332,21 @@
5111
5332
  return tree;
5112
5333
  };
5113
5334
  Tree.prototype.addPath = function (path) {
5114
- var e_2, _a;
5115
5335
  if (!isString(path) || path.length == 0) {
5116
5336
  return this;
5117
5337
  }
5118
5338
  var pathParts = path.split(".");
5339
+ var maxIx = pathParts.length - 1;
5119
5340
  var tree = this;
5120
5341
  path = this.path;
5121
- try {
5122
- for (var pathParts_2 = __values(pathParts), pathParts_2_1 = pathParts_2.next(); !pathParts_2_1.done; pathParts_2_1 = pathParts_2.next()) {
5123
- var part = pathParts_2_1.value;
5124
- if (!tree.map.has(part)) {
5125
- tree.map.set(part, new Tree(this.container, !path ? part : path + "." + part));
5126
- }
5127
- tree = tree.map.get(part);
5128
- path = tree.path;
5129
- }
5130
- }
5131
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
5132
- finally {
5133
- try {
5134
- if (pathParts_2_1 && !pathParts_2_1.done && (_a = pathParts_2.return)) _a.call(pathParts_2);
5342
+ for (var ix = 0; ix <= maxIx; ix++) {
5343
+ var part = pathParts[ix];
5344
+ if (!tree.map.has(part)) {
5345
+ tree.map.set(part, new Tree(this.container, false, !path ? part : path + "." + part));
5135
5346
  }
5136
- finally { if (e_2) throw e_2.error; }
5347
+ tree = tree.map.get(part);
5348
+ tree.exists = tree.exists || ix == maxIx;
5349
+ path = tree.path;
5137
5350
  }
5138
5351
  return this;
5139
5352
  };
@@ -5148,7 +5361,7 @@
5148
5361
  container["wrapperContainer"] = this;
5149
5362
  this.tokens = [];
5150
5363
  this.tokenSet = new Set();
5151
- this.myTree = new Tree(this, "");
5364
+ this.root = new Tree(this, false, "");
5152
5365
  }
5153
5366
  Object.defineProperty(DiContainer.prototype, "registeredTokens", {
5154
5367
  get: function () {
@@ -5160,7 +5373,7 @@
5160
5373
  });
5161
5374
  Object.defineProperty(DiContainer.prototype, "tree", {
5162
5375
  get: function () {
5163
- return this.myTree;
5376
+ return this.root;
5164
5377
  },
5165
5378
  enumerable: false,
5166
5379
  configurable: true
@@ -5224,7 +5437,7 @@
5224
5437
  this.tokenSet.add(token);
5225
5438
  this.tokens.push(token);
5226
5439
  if (isString(token)) {
5227
- this.myTree.addPath(token);
5440
+ this.root.addPath(token);
5228
5441
  }
5229
5442
  return this;
5230
5443
  };
@@ -5328,29 +5541,36 @@
5328
5541
  return [4 /*yield*/, this.progresses.get(lazyAsset.progressId)];
5329
5542
  case 2:
5330
5543
  progress = _a.sent();
5544
+ if (!progress || progress.canceled)
5545
+ return [2 /*return*/, null];
5331
5546
  _a.label = 3;
5332
5547
  case 3:
5333
- _a.trys.push([3, 6, , 8]);
5548
+ _a.trys.push([3, 7, , 9]);
5334
5549
  return [4 /*yield*/, this.generate(progress)];
5335
5550
  case 4:
5336
5551
  asset = _a.sent();
5337
- return [4 /*yield*/, lazyAsset.writeAsset(asset)];
5552
+ return [4 /*yield*/, progress.load()];
5338
5553
  case 5:
5339
- _a.sent();
5340
- return [3 /*break*/, 8];
5554
+ progress = _a.sent();
5555
+ if (!progress || progress.canceled)
5556
+ return [2 /*return*/, null];
5557
+ return [4 /*yield*/, lazyAsset.writeAsset(asset)];
5341
5558
  case 6:
5559
+ _a.sent();
5560
+ return [3 /*break*/, 9];
5561
+ case 7:
5342
5562
  e_1 = _a.sent();
5343
5563
  return [4 /*yield*/, progress.setError(e_1.message || e_1)];
5344
- case 7:
5564
+ case 8:
5345
5565
  _a.sent();
5346
5566
  throw e_1;
5347
- case 8:
5348
- if (!(progress.remaining > 0)) return [3 /*break*/, 10];
5349
- return [4 /*yield*/, progress.advance(progress.remaining)];
5350
5567
  case 9:
5568
+ if (!(progress.remaining > 0)) return [3 /*break*/, 11];
5569
+ return [4 /*yield*/, progress.advance(progress.remaining)];
5570
+ case 10:
5351
5571
  _a.sent();
5352
- _a.label = 10;
5353
- case 10: return [2 /*return*/];
5572
+ _a.label = 11;
5573
+ case 11: return [2 /*return*/];
5354
5574
  }
5355
5575
  });
5356
5576
  });