@stemy/backend 2.7.1 → 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.
@@ -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;
@@ -1520,27 +1577,28 @@
1520
1577
  };
1521
1578
  Asset.toImage = function (stream, meta, params) {
1522
1579
  return __awaiter$r(this, void 0, void 0, function () {
1523
- var buffer, crop, cropBefore, cropAfter, imgMeta, width, height, e_1;
1580
+ var crop, buffer, cropBefore, cropAfter, imgMeta, width, height, e_1;
1524
1581
  return __generator(this, function (_a) {
1525
1582
  switch (_a.label) {
1526
1583
  case 0:
1527
1584
  params = params || {};
1528
- if (Object.keys(params).length == 0)
1585
+ crop = Asset.toCropRegion(meta.crop);
1586
+ // Return back the stream if there is no params and no default crop exists
1587
+ if (Object.keys(params).length == 0 && !crop)
1529
1588
  return [2 /*return*/, stream];
1589
+ // Parse params
1590
+ params.rotation = isNaN(params.rotation) ? 0 : Math.round(params.rotation / 90) * 90;
1591
+ params.canvasScaleX = isNaN(params.canvasScaleX) ? 1 : Number(params.canvasScaleX);
1592
+ params.canvasScaleY = isNaN(params.canvasScaleY) ? 1 : Number(params.canvasScaleY);
1593
+ params.scaleX = isNaN(params.scaleX) ? 1 : Number(params.scaleX);
1594
+ params.scaleY = isNaN(params.scaleY) ? 1 : Number(params.scaleY);
1595
+ params.crop = isBoolean(params.crop) ? params.crop : params.crop == "true";
1530
1596
  return [4 /*yield*/, streamToBuffer(stream)];
1531
1597
  case 1:
1532
1598
  buffer = _a.sent();
1533
- // Parse params
1534
- params.rotation = isNaN(params.rotation) ? 0 : Math.round(params.rotation / 90) * 90;
1535
- params.canvasScaleX = isNaN(params.canvasScaleX) ? 1 : params.canvasScaleX;
1536
- params.canvasScaleY = isNaN(params.canvasScaleY) ? 1 : params.canvasScaleY;
1537
- params.scaleX = isNaN(params.scaleX) ? 1 : params.scaleX;
1538
- params.scaleY = isNaN(params.scaleY) ? 1 : params.scaleY;
1539
- params.crop = Boolean(params.crop);
1540
1599
  _a.label = 2;
1541
1600
  case 2:
1542
1601
  _a.trys.push([2, 16, , 17]);
1543
- crop = Asset.toCropRegion(meta.crop);
1544
1602
  cropBefore = Asset.toCropRegion(params.cropBefore || (params.crop ? meta.cropBefore : null));
1545
1603
  cropAfter = Asset.toCropRegion(params.cropAfter || (params.crop ? meta.cropAfter : null));
1546
1604
  return [4 /*yield*/, sharp$2(buffer).metadata()];
@@ -1554,6 +1612,8 @@
1554
1612
  .toBuffer()];
1555
1613
  case 4:
1556
1614
  buffer = _a.sent();
1615
+ width = cropBefore.width;
1616
+ height = cropBefore.height;
1557
1617
  return [3 /*break*/, 7];
1558
1618
  case 5:
1559
1619
  if (!crop) return [3 /*break*/, 7];
@@ -1562,6 +1622,8 @@
1562
1622
  .toBuffer()];
1563
1623
  case 6:
1564
1624
  buffer = _a.sent();
1625
+ width = crop.width;
1626
+ height = crop.height;
1565
1627
  _a.label = 7;
1566
1628
  case 7:
1567
1629
  if (!(params.canvasScaleX !== 1 || params.canvasScaleY !== 1)) return [3 /*break*/, 9];
@@ -1607,16 +1669,30 @@
1607
1669
  });
1608
1670
  });
1609
1671
  };
1610
- Object.defineProperty(Asset.prototype, "id", {
1672
+ Object.defineProperty(Asset.prototype, "filename", {
1673
+ get: function () {
1674
+ return this.data.filename;
1675
+ },
1676
+ enumerable: false,
1677
+ configurable: true
1678
+ });
1679
+ Object.defineProperty(Asset.prototype, "contentType", {
1611
1680
  get: function () {
1612
- return this.fileId.toHexString();
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;
1613
1689
  },
1614
1690
  enumerable: false,
1615
1691
  configurable: true
1616
1692
  });
1617
1693
  Object.defineProperty(Asset.prototype, "stream", {
1618
1694
  get: function () {
1619
- return this.bucket.openDownloadStream(this.fileId);
1695
+ return this.bucket.openDownloadStream(this.mId);
1620
1696
  },
1621
1697
  enumerable: false,
1622
1698
  configurable: true
@@ -1624,7 +1700,7 @@
1624
1700
  Asset.prototype.unlink = function () {
1625
1701
  return __awaiter$r(this, void 0, void 0, function () {
1626
1702
  return __generator(this, function (_a) {
1627
- return [2 /*return*/, deleteFromBucket(this.bucket, this.fileId)];
1703
+ return [2 /*return*/, deleteFromBucket(this.bucket, this.mId)];
1628
1704
  });
1629
1705
  });
1630
1706
  };
@@ -1642,7 +1718,7 @@
1642
1718
  : metadata.downloadCount + 1;
1643
1719
  metadata.firstDownload = metadata.firstDownload || new Date();
1644
1720
  metadata.lastDownload = new Date();
1645
- 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 } })];
1646
1722
  case 1:
1647
1723
  _a.sent();
1648
1724
  return [2 /*return*/, this.stream];
@@ -1671,16 +1747,8 @@
1671
1747
  });
1672
1748
  });
1673
1749
  };
1674
- Asset.prototype.toJSON = function () {
1675
- return {
1676
- id: this.id,
1677
- filename: this.filename,
1678
- contentType: this.contentType,
1679
- metadata: this.metadata
1680
- };
1681
- };
1682
1750
  return Asset;
1683
- }());
1751
+ }(BaseEntity));
1684
1752
 
1685
1753
  var __decorate$t = (this && this.__decorate) || function (decorators, target, key, desc) {
1686
1754
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -1768,8 +1836,12 @@
1768
1836
  reject(error.message || error);
1769
1837
  })
1770
1838
  .on("finish", function () {
1771
- var asset = new Asset(uploaderStream.id, metadata.filename, contentType, metadata, _this.bucket, _this.collection);
1772
- _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 () {
1773
1845
  resolve(asset);
1774
1846
  }, function (error) {
1775
1847
  reject(error.message || error);
@@ -1798,6 +1870,20 @@
1798
1870
  });
1799
1871
  });
1800
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
+ };
1801
1887
  Assets.prototype.read = function (id) {
1802
1888
  return __awaiter$q(this, void 0, void 0, function () {
1803
1889
  return __generator(this, function (_b) {
@@ -1813,7 +1899,7 @@
1813
1899
  case 0: return [4 /*yield*/, this.collection.findOne(where)];
1814
1900
  case 1:
1815
1901
  data = _b.sent();
1816
- 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)];
1817
1903
  }
1818
1904
  });
1819
1905
  });
@@ -1860,36 +1946,46 @@
1860
1946
  step((generator = generator.apply(thisArg, _arguments || [])).next());
1861
1947
  });
1862
1948
  };
1863
- var LazyAsset = /** @class */ (function () {
1864
- function LazyAsset(lazyId, jobName, jobParams, jobQue, mProgressId, mAssetId, assets, progresses, jobMan, collection) {
1865
- this.lazyId = lazyId;
1866
- this.jobName = jobName;
1867
- this.jobParams = jobParams;
1868
- this.jobQue = jobQue;
1869
- this.mProgressId = mProgressId;
1870
- this.mAssetId = mAssetId;
1871
- this.assets = assets;
1872
- this.progresses = progresses;
1873
- this.jobMan = jobMan;
1874
- this.collection = collection;
1949
+ var LazyAsset = /** @class */ (function (_super) {
1950
+ __extends(LazyAsset, _super);
1951
+ function LazyAsset(id, data, collection, assets, progresses, jobMan) {
1952
+ var _this = _super.call(this, id, data, collection) || this;
1953
+ _this.assets = assets;
1954
+ _this.progresses = progresses;
1955
+ _this.jobMan = jobMan;
1956
+ return _this;
1875
1957
  }
1876
- Object.defineProperty(LazyAsset.prototype, "id", {
1958
+ Object.defineProperty(LazyAsset.prototype, "jobName", {
1959
+ get: function () {
1960
+ return this.data.jobName;
1961
+ },
1962
+ enumerable: false,
1963
+ configurable: true
1964
+ });
1965
+ Object.defineProperty(LazyAsset.prototype, "jobParams", {
1877
1966
  get: function () {
1878
- return this.lazyId.toHexString();
1967
+ return this.data.jobParams;
1968
+ },
1969
+ enumerable: false,
1970
+ configurable: true
1971
+ });
1972
+ Object.defineProperty(LazyAsset.prototype, "jobQue", {
1973
+ get: function () {
1974
+ return this.data.jobQue;
1879
1975
  },
1880
1976
  enumerable: false,
1881
1977
  configurable: true
1882
1978
  });
1883
1979
  Object.defineProperty(LazyAsset.prototype, "progressId", {
1884
1980
  get: function () {
1885
- return this.mProgressId;
1981
+ return this.data.progressId;
1886
1982
  },
1887
1983
  enumerable: false,
1888
1984
  configurable: true
1889
1985
  });
1890
1986
  Object.defineProperty(LazyAsset.prototype, "assetId", {
1891
1987
  get: function () {
1892
- return this.mAssetId;
1988
+ return this.data.assetId;
1893
1989
  },
1894
1990
  enumerable: false,
1895
1991
  configurable: true
@@ -1898,41 +1994,50 @@
1898
1994
  return __awaiter$p(this, void 0, void 0, function () {
1899
1995
  return __generator(this, function (_a) {
1900
1996
  switch (_a.label) {
1901
- case 0: return [4 /*yield*/, this.collection.deleteOne({ _id: this.lazyId })];
1997
+ case 0: return [4 /*yield*/, this.load()];
1902
1998
  case 1:
1903
1999
  _a.sent();
1904
- return [2 /*return*/, deleteFromBucket(this.assets.bucket, new bson.ObjectId(this.mAssetId))];
2000
+ if (!!this.progressId) return [3 /*break*/, 3];
2001
+ return [4 /*yield*/, this.collection.deleteOne({ _id: this.mId })];
2002
+ case 2:
2003
+ _a.sent();
2004
+ _a.label = 3;
2005
+ case 3: return [2 /*return*/, deleteFromBucket(this.assets.bucket, new bson.ObjectId(this.assetId))];
1905
2006
  }
1906
2007
  });
1907
2008
  });
1908
2009
  };
1909
2010
  LazyAsset.prototype.startWorking = function () {
1910
2011
  var _this = this;
1911
- if (this.mProgressId)
1912
- return;
1913
- this.startWorkingOnAsset().then(function () {
1914
- console.log("Started working on lazy asset: " + _this.id);
1915
- }).catch(function (reason) {
1916
- console.log("Can't start working on lazy asset: " + _this.id + "\nReason: " + reason);
2012
+ this.load().then(function () {
2013
+ if (_this.deleted)
2014
+ return;
2015
+ var progressPromise = !_this.progressId ? Promise.resolve(null) : _this.progresses.get(_this.progressId).then(function (p) { return p.cancel(); });
2016
+ progressPromise.then(function () {
2017
+ _this.startWorkingOnAsset().then(function () {
2018
+ console.log("Started working on lazy asset: " + _this.id);
2019
+ }).catch(function (reason) {
2020
+ console.log("Can't start working on lazy asset: " + _this.id + "\nReason: " + reason);
2021
+ });
2022
+ });
1917
2023
  });
1918
2024
  };
1919
2025
  LazyAsset.prototype.loadAsset = function () {
1920
2026
  return __awaiter$p(this, void 0, void 0, function () {
1921
- var data;
1922
2027
  return __generator(this, function (_a) {
1923
2028
  switch (_a.label) {
1924
- case 0:
1925
- if (this.mAssetId) {
1926
- return [2 /*return*/, this.assets.read(this.mAssetId)];
1927
- }
1928
- if (!this.mProgressId) return [3 /*break*/, 3];
1929
- return [4 /*yield*/, this.progresses.waitToFinish(this.mProgressId)];
2029
+ case 0: return [4 /*yield*/, this.load()];
1930
2030
  case 1:
1931
2031
  _a.sent();
1932
- return [4 /*yield*/, this.collection.findOne({ _id: this.lazyId })];
2032
+ if (this.deleted)
2033
+ return [2 /*return*/, null];
2034
+ if (this.assetId) {
2035
+ return [2 /*return*/, this.assets.read(this.assetId)];
2036
+ }
2037
+ if (!this.progressId) return [3 /*break*/, 3];
2038
+ return [4 /*yield*/, this.progresses.waitToFinish(this.progressId)];
1933
2039
  case 2:
1934
- data = _a.sent();
1935
- this.mAssetId = data.assetId;
2040
+ _a.sent();
1936
2041
  return [2 /*return*/, this.loadAsset()];
1937
2042
  case 3: return [4 /*yield*/, this.startWorkingOnAsset()];
1938
2043
  case 4:
@@ -1947,7 +2052,7 @@
1947
2052
  return __generator(this, function (_a) {
1948
2053
  switch (_a.label) {
1949
2054
  case 0:
1950
- this.mAssetId = asset.id;
2055
+ this.data.assetId = asset.id;
1951
2056
  return [4 /*yield*/, this.save()];
1952
2057
  case 1:
1953
2058
  _a.sent();
@@ -1956,32 +2061,19 @@
1956
2061
  });
1957
2062
  });
1958
2063
  };
1959
- LazyAsset.prototype.save = function () {
1960
- return this.collection.updateOne({ _id: this.lazyId }, { $set: this.toJSON() });
1961
- };
1962
- LazyAsset.prototype.toJSON = function () {
1963
- return {
1964
- id: this.id,
1965
- jobName: this.jobName,
1966
- jobParams: this.jobParams,
1967
- jobQue: this.jobQue,
1968
- progressId: this.progressId,
1969
- assetId: this.assetId,
1970
- };
1971
- };
1972
2064
  LazyAsset.prototype.startWorkingOnAsset = function () {
1973
2065
  return __awaiter$p(this, void 0, void 0, function () {
1974
- var progress;
2066
+ var id;
1975
2067
  return __generator(this, function (_a) {
1976
2068
  switch (_a.label) {
1977
2069
  case 0: return [4 /*yield*/, this.progresses.create()];
1978
2070
  case 1:
1979
- progress = _a.sent();
1980
- this.mProgressId = progress.id;
2071
+ id = (_a.sent()).id;
2072
+ this.data.progressId = id;
1981
2073
  return [4 /*yield*/, this.save()];
1982
2074
  case 2:
1983
2075
  _a.sent();
1984
- return [4 /*yield*/, this.jobMan.enqueueWithName(this.jobName, Object.assign(Object.assign({}, this.jobParams), { lazyId: this.id }))];
2076
+ return [4 /*yield*/, this.jobMan.enqueueWithName(this.data.jobName, Object.assign(Object.assign({}, this.data.jobParams), { lazyId: this.id }))];
1985
2077
  case 3:
1986
2078
  _a.sent();
1987
2079
  return [2 /*return*/];
@@ -1990,7 +2082,7 @@
1990
2082
  });
1991
2083
  };
1992
2084
  return LazyAsset;
1993
- }());
2085
+ }(BaseEntity));
1994
2086
 
1995
2087
  var __decorate$s = (this && this.__decorate) || function (decorators, target, key, desc) {
1996
2088
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -2301,61 +2393,58 @@
2301
2393
  step((generator = generator.apply(thisArg, _arguments || [])).next());
2302
2394
  });
2303
2395
  };
2304
- var Progress = /** @class */ (function () {
2305
- function Progress(progressId, mCurrent, mMax, mMessage, mError, client, collection) {
2306
- this.progressId = progressId;
2307
- this.mCurrent = mCurrent;
2308
- this.mMax = mMax;
2309
- this.mMessage = mMessage;
2310
- this.mError = mError;
2311
- this.client = client;
2312
- this.collection = collection;
2396
+ var Progress = /** @class */ (function (_super) {
2397
+ __extends(Progress, _super);
2398
+ function Progress(id, data, collection, client) {
2399
+ var _this = _super.call(this, id, data, collection) || this;
2400
+ _this.client = client;
2401
+ return _this;
2313
2402
  }
2314
- Object.defineProperty(Progress.prototype, "id", {
2403
+ Object.defineProperty(Progress.prototype, "current", {
2315
2404
  get: function () {
2316
- return this.progressId.toHexString();
2405
+ return this.data.current;
2317
2406
  },
2318
2407
  enumerable: false,
2319
2408
  configurable: true
2320
2409
  });
2321
- Object.defineProperty(Progress.prototype, "current", {
2410
+ Object.defineProperty(Progress.prototype, "max", {
2322
2411
  get: function () {
2323
- return this.mCurrent;
2412
+ return this.data.max;
2324
2413
  },
2325
2414
  enumerable: false,
2326
2415
  configurable: true
2327
2416
  });
2328
- Object.defineProperty(Progress.prototype, "max", {
2417
+ Object.defineProperty(Progress.prototype, "message", {
2329
2418
  get: function () {
2330
- return this.mMax;
2419
+ return this.data.message;
2331
2420
  },
2332
2421
  enumerable: false,
2333
2422
  configurable: true
2334
2423
  });
2335
- Object.defineProperty(Progress.prototype, "message", {
2424
+ Object.defineProperty(Progress.prototype, "error", {
2336
2425
  get: function () {
2337
- return this.mMessage;
2426
+ return this.data.error;
2338
2427
  },
2339
2428
  enumerable: false,
2340
2429
  configurable: true
2341
2430
  });
2342
- Object.defineProperty(Progress.prototype, "error", {
2431
+ Object.defineProperty(Progress.prototype, "canceled", {
2343
2432
  get: function () {
2344
- return this.mError;
2433
+ return this.data.canceled;
2345
2434
  },
2346
2435
  enumerable: false,
2347
2436
  configurable: true
2348
2437
  });
2349
2438
  Object.defineProperty(Progress.prototype, "percent", {
2350
2439
  get: function () {
2351
- return this.mMax > 0 ? Math.round(this.mCurrent / this.mMax * 100) : 0;
2440
+ return this.max > 0 ? Math.round(this.current / this.max * 100) : 0;
2352
2441
  },
2353
2442
  enumerable: false,
2354
2443
  configurable: true
2355
2444
  });
2356
2445
  Object.defineProperty(Progress.prototype, "remaining", {
2357
2446
  get: function () {
2358
- return this.mMax > 0 ? this.mMax - this.mCurrent : 0;
2447
+ return this.max > 0 ? this.max - this.current : 0;
2359
2448
  },
2360
2449
  enumerable: false,
2361
2450
  configurable: true
@@ -2372,12 +2461,12 @@
2372
2461
  _a.label = 2;
2373
2462
  case 2:
2374
2463
  if (!(message !== null)) return [3 /*break*/, 4];
2375
- this.mMessage = message;
2464
+ this.data.message = message;
2376
2465
  return [4 /*yield*/, this.save()];
2377
2466
  case 3:
2378
2467
  _a.sent();
2379
2468
  _a.label = 4;
2380
- case 4: return [2 /*return*/, new SubProgress(this, this.mCurrent, progressValue, Math.max(max, 1))];
2469
+ case 4: return [2 /*return*/, new SubProgress(this, this.current, progressValue, Math.max(max, 1))];
2381
2470
  }
2382
2471
  });
2383
2472
  });
@@ -2390,7 +2479,21 @@
2390
2479
  if (isNaN(max) || max <= 0) {
2391
2480
  throw "Max progress value must be bigger than zero";
2392
2481
  }
2393
- this.mMax = max;
2482
+ this.data.max = max;
2483
+ return [4 /*yield*/, this.save()];
2484
+ case 1:
2485
+ _a.sent();
2486
+ return [2 /*return*/];
2487
+ }
2488
+ });
2489
+ });
2490
+ };
2491
+ Progress.prototype.setMessage = function (message) {
2492
+ return __awaiter$n(this, void 0, void 0, function () {
2493
+ return __generator(this, function (_a) {
2494
+ switch (_a.label) {
2495
+ case 0:
2496
+ this.data.message = message;
2394
2497
  return [4 /*yield*/, this.save()];
2395
2498
  case 1:
2396
2499
  _a.sent();
@@ -2404,7 +2507,7 @@
2404
2507
  return __generator(this, function (_a) {
2405
2508
  switch (_a.label) {
2406
2509
  case 0:
2407
- this.mError = error;
2510
+ this.data.error = error;
2408
2511
  return [4 /*yield*/, this.save()];
2409
2512
  case 1:
2410
2513
  _a.sent();
@@ -2422,9 +2525,14 @@
2422
2525
  if (isNaN(value) || value <= 0) {
2423
2526
  throw "Advance value must be bigger than zero";
2424
2527
  }
2425
- this.mCurrent = Math.min(this.mMax, this.mCurrent + value);
2426
- return [4 /*yield*/, this.save()];
2528
+ return [4 /*yield*/, this.load()];
2427
2529
  case 1:
2530
+ _a.sent();
2531
+ if (this.deleted || this.canceled)
2532
+ return [2 /*return*/, null];
2533
+ this.data.current = Math.min(this.max, this.current + value);
2534
+ return [4 /*yield*/, this.save()];
2535
+ case 2:
2428
2536
  _a.sent();
2429
2537
  if (!this.client)
2430
2538
  return [2 /*return*/];
@@ -2434,34 +2542,36 @@
2434
2542
  });
2435
2543
  });
2436
2544
  };
2437
- Progress.prototype.toJSON = function () {
2438
- return {
2439
- id: this.id,
2440
- current: this.current,
2441
- max: this.max,
2442
- message: this.message,
2443
- error: this.error
2444
- };
2445
- };
2446
- Progress.prototype.save = function () {
2447
- return this.collection.updateOne({ _id: this.progressId }, { $set: this.toJSON() });
2545
+ Progress.prototype.cancel = function () {
2546
+ return __awaiter$n(this, void 0, void 0, function () {
2547
+ return __generator(this, function (_a) {
2548
+ switch (_a.label) {
2549
+ case 0:
2550
+ this.data.canceled = true;
2551
+ return [4 /*yield*/, this.save()];
2552
+ case 1:
2553
+ _a.sent();
2554
+ return [2 /*return*/];
2555
+ }
2556
+ });
2557
+ });
2448
2558
  };
2449
2559
  return Progress;
2450
- }());
2560
+ }(BaseEntity));
2451
2561
  var SubProgress = /** @class */ (function () {
2452
- function SubProgress(parent, progressFrom, progressValue, max) {
2453
- if (max === void 0) { max = 100; }
2562
+ function SubProgress(parent, progressFrom, progressValue, mMax) {
2563
+ if (mMax === void 0) { mMax = 100; }
2454
2564
  this.parent = parent;
2455
2565
  this.progressFrom = progressFrom;
2456
2566
  this.progressValue = progressValue;
2457
- this.max = max;
2567
+ this.mMax = mMax;
2458
2568
  if (progressFrom < 0) {
2459
2569
  throw "Progress from must be bigger than or zero";
2460
2570
  }
2461
2571
  if (progressValue <= 0) {
2462
2572
  throw "Progress value must be bigger than zero";
2463
2573
  }
2464
- this.currentValue = 0;
2574
+ this.mCurrent = 0;
2465
2575
  }
2466
2576
  Object.defineProperty(SubProgress.prototype, "id", {
2467
2577
  get: function () {
@@ -2470,13 +2580,24 @@
2470
2580
  enumerable: false,
2471
2581
  configurable: true
2472
2582
  });
2583
+ Object.defineProperty(SubProgress.prototype, "current", {
2584
+ get: function () {
2585
+ return this.mCurrent;
2586
+ },
2587
+ enumerable: false,
2588
+ configurable: true
2589
+ });
2590
+ Object.defineProperty(SubProgress.prototype, "max", {
2591
+ get: function () {
2592
+ return this.mMax;
2593
+ },
2594
+ enumerable: false,
2595
+ configurable: true
2596
+ });
2473
2597
  Object.defineProperty(SubProgress.prototype, "message", {
2474
2598
  get: function () {
2475
2599
  return this.parent.message;
2476
2600
  },
2477
- set: function (value) {
2478
- this.parent.message = value;
2479
- },
2480
2601
  enumerable: false,
2481
2602
  configurable: true
2482
2603
  });
@@ -2484,9 +2605,6 @@
2484
2605
  get: function () {
2485
2606
  return this.parent.error;
2486
2607
  },
2487
- set: function (value) {
2488
- this.parent.error = value;
2489
- },
2490
2608
  enumerable: false,
2491
2609
  configurable: true
2492
2610
  });
@@ -2497,16 +2615,16 @@
2497
2615
  enumerable: false,
2498
2616
  configurable: true
2499
2617
  });
2500
- Object.defineProperty(SubProgress.prototype, "current", {
2618
+ Object.defineProperty(SubProgress.prototype, "remaining", {
2501
2619
  get: function () {
2502
- return this.currentValue;
2620
+ return this.max - this.mCurrent;
2503
2621
  },
2504
2622
  enumerable: false,
2505
2623
  configurable: true
2506
2624
  });
2507
- Object.defineProperty(SubProgress.prototype, "remaining", {
2625
+ Object.defineProperty(SubProgress.prototype, "canceled", {
2508
2626
  get: function () {
2509
- return this.max - this.currentValue;
2627
+ return !this.parent || this.parent.canceled;
2510
2628
  },
2511
2629
  enumerable: false,
2512
2630
  configurable: true
@@ -2523,8 +2641,7 @@
2523
2641
  _a.label = 2;
2524
2642
  case 2:
2525
2643
  if (!(message !== null)) return [3 /*break*/, 4];
2526
- this.message = message;
2527
- return [4 /*yield*/, this.parent.save()];
2644
+ return [4 /*yield*/, this.setMessage(message)];
2528
2645
  case 3:
2529
2646
  _a.sent();
2530
2647
  _a.label = 4;
@@ -2541,7 +2658,7 @@
2541
2658
  if (isNaN(max) || max <= 0) {
2542
2659
  throw "Max progress value must be bigger than zero";
2543
2660
  }
2544
- this.max = max;
2661
+ this.mMax = max;
2545
2662
  return [4 /*yield*/, this.save()];
2546
2663
  case 1:
2547
2664
  _a.sent();
@@ -2550,13 +2667,29 @@
2550
2667
  });
2551
2668
  });
2552
2669
  };
2670
+ SubProgress.prototype.setMessage = function (message) {
2671
+ return __awaiter$n(this, void 0, void 0, function () {
2672
+ return __generator(this, function (_a) {
2673
+ switch (_a.label) {
2674
+ case 0:
2675
+ if (!this.parent)
2676
+ return [2 /*return*/, null];
2677
+ return [4 /*yield*/, this.parent.setMessage(message)];
2678
+ case 1:
2679
+ _a.sent();
2680
+ return [2 /*return*/];
2681
+ }
2682
+ });
2683
+ });
2684
+ };
2553
2685
  SubProgress.prototype.setError = function (error) {
2554
2686
  return __awaiter$n(this, void 0, void 0, function () {
2555
2687
  return __generator(this, function (_a) {
2556
2688
  switch (_a.label) {
2557
2689
  case 0:
2558
- this.error = error || null;
2559
- return [4 /*yield*/, this.save()];
2690
+ if (!this.parent)
2691
+ return [2 /*return*/, null];
2692
+ return [4 /*yield*/, this.parent.setError(error)];
2560
2693
  case 1:
2561
2694
  _a.sent();
2562
2695
  return [2 /*return*/];
@@ -2573,7 +2706,7 @@
2573
2706
  if (isNaN(value) || value <= 0) {
2574
2707
  throw "Advance value must be bigger than zero";
2575
2708
  }
2576
- this.currentValue = Math.min(this.max, this.currentValue + value);
2709
+ this.mCurrent = Math.min(this.max, this.mCurrent + value);
2577
2710
  return [4 /*yield*/, this.save()];
2578
2711
  case 1:
2579
2712
  _a.sent();
@@ -2582,13 +2715,28 @@
2582
2715
  });
2583
2716
  });
2584
2717
  };
2718
+ SubProgress.prototype.cancel = function () {
2719
+ return __awaiter$n(this, void 0, void 0, function () {
2720
+ return __generator(this, function (_a) {
2721
+ switch (_a.label) {
2722
+ case 0:
2723
+ if (!this.parent)
2724
+ return [2 /*return*/, null];
2725
+ return [4 /*yield*/, this.parent.cancel()];
2726
+ case 1:
2727
+ _a.sent();
2728
+ return [2 /*return*/];
2729
+ }
2730
+ });
2731
+ });
2732
+ };
2585
2733
  SubProgress.prototype.save = function () {
2586
2734
  return __awaiter$n(this, void 0, void 0, function () {
2587
2735
  var ratio, newProgress, current;
2588
2736
  return __generator(this, function (_a) {
2589
2737
  switch (_a.label) {
2590
2738
  case 0:
2591
- ratio = this.max > 0 ? this.currentValue / this.max : 0;
2739
+ ratio = this.max > 0 ? this.mCurrent / this.max : 0;
2592
2740
  newProgress = this.progressFrom + Math.round(this.progressValue * ratio);
2593
2741
  current = this.parent.current;
2594
2742
  if (newProgress <= current)
@@ -2601,6 +2749,13 @@
2601
2749
  });
2602
2750
  });
2603
2751
  };
2752
+ SubProgress.prototype.load = function () {
2753
+ return __awaiter$n(this, void 0, void 0, function () {
2754
+ return __generator(this, function (_a) {
2755
+ return [2 /*return*/, null];
2756
+ });
2757
+ });
2758
+ };
2604
2759
  SubProgress.prototype.toJSON = function () {
2605
2760
  return this.parent.toJSON();
2606
2761
  };
@@ -2696,7 +2851,7 @@
2696
2851
  case 0: return [4 /*yield*/, this.collection.findOne(where)];
2697
2852
  case 1:
2698
2853
  data = _a.sent();
2699
- return [2 /*return*/, !data ? null : new Progress(data._id, data.current, data.max, data.message, data.error, this.client, this.collection)];
2854
+ return [2 /*return*/, !data ? null : new Progress(data._id, data, this.collection, this.client)];
2700
2855
  }
2701
2856
  });
2702
2857
  });
@@ -2704,20 +2859,24 @@
2704
2859
  Progresses.prototype.create = function (max) {
2705
2860
  if (max === void 0) { max = 100; }
2706
2861
  return __awaiter$m(this, void 0, void 0, function () {
2707
- var res;
2862
+ var data, res;
2708
2863
  return __generator(this, function (_a) {
2709
2864
  switch (_a.label) {
2710
2865
  case 0:
2711
2866
  if (isNaN(max) || max <= 0) {
2712
2867
  throw "Max progress value must be bigger than zero";
2713
2868
  }
2714
- return [4 /*yield*/, this.collection.insertOne({
2715
- current: 0,
2716
- max: max
2717
- })];
2869
+ data = {
2870
+ current: 0,
2871
+ max: max,
2872
+ message: "",
2873
+ error: "",
2874
+ canceled: false
2875
+ };
2876
+ return [4 /*yield*/, this.collection.insertOne(data)];
2718
2877
  case 1:
2719
2878
  res = _a.sent();
2720
- return [2 /*return*/, new Progress(res.insertedId, 0, max, "", "", this.client, this.collection)];
2879
+ return [2 /*return*/, new Progress(res.insertedId, data, this.collection, this.client)];
2721
2880
  }
2722
2881
  });
2723
2882
  });
@@ -2787,19 +2946,25 @@
2787
2946
  if (jobParams === void 0) { jobParams = {}; }
2788
2947
  if (jobQue === void 0) { jobQue = "main"; }
2789
2948
  return __awaiter$l(this, void 0, void 0, function () {
2790
- var jobName, res;
2949
+ var jobName, data, existingAsset, res;
2791
2950
  return __generator(this, function (_a) {
2792
2951
  switch (_a.label) {
2793
2952
  case 0:
2794
2953
  jobName = this.jobMan.tryResolve(jobType, Object.assign(Object.assign({}, jobParams), { lazyId: "" }));
2795
- return [4 /*yield*/, this.collection.insertOne({
2796
- jobName: jobName,
2797
- jobParams: jobParams,
2798
- jobQue: jobQue
2799
- })];
2954
+ data = {
2955
+ jobName: jobName,
2956
+ jobParams: jobParams,
2957
+ jobQue: jobQue
2958
+ };
2959
+ return [4 /*yield*/, this.find(data)];
2800
2960
  case 1:
2961
+ existingAsset = _a.sent();
2962
+ if (existingAsset)
2963
+ return [2 /*return*/, existingAsset];
2964
+ return [4 /*yield*/, this.collection.insertOne(data)];
2965
+ case 2:
2801
2966
  res = _a.sent();
2802
- return [2 /*return*/, new LazyAsset(res.insertedId, jobName, jobParams, jobQue, null, null, this.assets, this.progresses, this.jobMan, this.collection)];
2967
+ return [2 /*return*/, new LazyAsset(res.insertedId, data, this.collection, this.assets, this.progresses, this.jobMan)];
2803
2968
  }
2804
2969
  });
2805
2970
  });
@@ -2821,7 +2986,7 @@
2821
2986
  data = _a.sent();
2822
2987
  return [2 /*return*/, !data
2823
2988
  ? null
2824
- : new LazyAsset(data._id, data.jobName, data.jobParams, data.jobQue, data.progressId, data.assetId, this.assets, this.progresses, this.jobMan, this.collection)];
2989
+ : new LazyAsset(data._id, data, this.collection, this.assets, this.progresses, this.jobMan)];
2825
2990
  }
2826
2991
  });
2827
2992
  });
@@ -4325,6 +4490,26 @@
4325
4490
  });
4326
4491
  });
4327
4492
  };
4493
+ AssetsController.prototype.uploadUrl = function (body) {
4494
+ return __awaiter$7(this, void 0, void 0, function () {
4495
+ var asset, e_2, msg;
4496
+ return __generator(this, function (_c) {
4497
+ switch (_c.label) {
4498
+ case 0:
4499
+ _c.trys.push([0, 2, , 3]);
4500
+ return [4 /*yield*/, this.assets.writeUrl(body.url, body)];
4501
+ case 1:
4502
+ asset = _c.sent();
4503
+ return [2 /*return*/, asset.toJSON()];
4504
+ case 2:
4505
+ e_2 = _c.sent();
4506
+ msg = (e_2 === null || e_2 === void 0 ? void 0 : e_2.message) || e_2 || "Unknown error";
4507
+ throw new routingControllers.HttpError(400, "Asset can't be uploaded.\n" + msg);
4508
+ case 3: return [2 /*return*/];
4509
+ }
4510
+ });
4511
+ });
4512
+ };
4328
4513
  AssetsController.prototype.getImageRotation = function (id, params, rotation) {
4329
4514
  if (rotation === void 0) { rotation = 0; }
4330
4515
  var _a;
@@ -4388,6 +4573,14 @@
4388
4573
  __metadata$7("design:paramtypes", [Object]),
4389
4574
  __metadata$7("design:returntype", Promise)
4390
4575
  ], AssetsController.prototype, "upload", null);
4576
+ __decorate$a([
4577
+ routingControllers.Authorized(),
4578
+ routingControllers.Post("url"),
4579
+ __param$5(0, routingControllers.Body()),
4580
+ __metadata$7("design:type", Function),
4581
+ __metadata$7("design:paramtypes", [Object]),
4582
+ __metadata$7("design:returntype", Promise)
4583
+ ], AssetsController.prototype, "uploadUrl", null);
4391
4584
  __decorate$a([
4392
4585
  routingControllers.Get("/image/:id/:rotation"),
4393
4586
  __param$5(0, routingControllers.Param("id")), __param$5(1, routingControllers.QueryParams()), __param$5(2, routingControllers.Param("rotation")),
@@ -5323,29 +5516,36 @@
5323
5516
  return [4 /*yield*/, this.progresses.get(lazyAsset.progressId)];
5324
5517
  case 2:
5325
5518
  progress = _a.sent();
5519
+ if (!progress || progress.canceled)
5520
+ return [2 /*return*/, null];
5326
5521
  _a.label = 3;
5327
5522
  case 3:
5328
- _a.trys.push([3, 6, , 8]);
5523
+ _a.trys.push([3, 7, , 9]);
5329
5524
  return [4 /*yield*/, this.generate(progress)];
5330
5525
  case 4:
5331
5526
  asset = _a.sent();
5332
- return [4 /*yield*/, lazyAsset.writeAsset(asset)];
5527
+ return [4 /*yield*/, progress.load()];
5333
5528
  case 5:
5334
- _a.sent();
5335
- return [3 /*break*/, 8];
5529
+ progress = _a.sent();
5530
+ if (!progress || progress.canceled)
5531
+ return [2 /*return*/, null];
5532
+ return [4 /*yield*/, lazyAsset.writeAsset(asset)];
5336
5533
  case 6:
5534
+ _a.sent();
5535
+ return [3 /*break*/, 9];
5536
+ case 7:
5337
5537
  e_1 = _a.sent();
5338
5538
  return [4 /*yield*/, progress.setError(e_1.message || e_1)];
5339
- case 7:
5539
+ case 8:
5340
5540
  _a.sent();
5341
5541
  throw e_1;
5342
- case 8:
5343
- if (!(progress.remaining > 0)) return [3 /*break*/, 10];
5344
- return [4 /*yield*/, progress.advance(progress.remaining)];
5345
5542
  case 9:
5543
+ if (!(progress.remaining > 0)) return [3 /*break*/, 11];
5544
+ return [4 /*yield*/, progress.advance(progress.remaining)];
5545
+ case 10:
5346
5546
  _a.sent();
5347
- _a.label = 10;
5348
- case 10: return [2 /*return*/];
5547
+ _a.label = 11;
5548
+ case 11: return [2 /*return*/];
5349
5549
  }
5350
5550
  });
5351
5551
  });