@stemy/backend 2.8.6 → 2.9.3

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.
@@ -1222,25 +1222,29 @@
1222
1222
  exports.AssetProcessor = AssetProcessor_1 = /** @class */ (function () {
1223
1223
  function AssetProcessor() {
1224
1224
  }
1225
- AssetProcessor.getMimeType = function (buffer, mimeType) {
1225
+ AssetProcessor.checkTextFileType = function (type) {
1226
+ return type.mime.indexOf("text") >= 0 || type.mime.indexOf("xml") >= 0;
1227
+ };
1228
+ AssetProcessor.fixTextFileType = function (type, buffer) {
1229
+ var text = buffer.toString("utf8");
1230
+ if (text.indexOf("<svg") >= 0) {
1231
+ return { ext: "svg", mime: "image/svg+xml" };
1232
+ }
1233
+ return type;
1234
+ };
1235
+ AssetProcessor.fileTypeFromBuffer = function (buffer) {
1236
+ var _a;
1226
1237
  return __awaiter$u(this, void 0, void 0, function () {
1227
- var e_1;
1228
- return __generator(this, function (_a) {
1229
- switch (_a.label) {
1230
- case 0:
1231
- _a.trys.push([0, 2, , 3]);
1232
- return [4 /*yield*/, fileType.fromBuffer(buffer)];
1238
+ var type;
1239
+ return __generator(this, function (_b) {
1240
+ switch (_b.label) {
1241
+ case 0: return [4 /*yield*/, fileType.fromBuffer(buffer)];
1233
1242
  case 1:
1234
- mimeType = (_a.sent()).mime;
1235
- return [3 /*break*/, 3];
1236
- case 2:
1237
- e_1 = _a.sent();
1238
- if (!mimeType) {
1239
- throw "Can't determine mime type";
1243
+ type = ((_a = _b.sent()) !== null && _a !== void 0 ? _a : { ext: "txt", mime: "text/plain" });
1244
+ if (AssetProcessor_1.checkTextFileType(type)) {
1245
+ return [2 /*return*/, AssetProcessor_1.fixTextFileType(type, buffer)];
1240
1246
  }
1241
- console.log("Can't determine mime type", e_1);
1242
- return [3 /*break*/, 3];
1243
- case 3: return [2 /*return*/, mimeType];
1247
+ return [2 /*return*/, type];
1244
1248
  }
1245
1249
  });
1246
1250
  });
@@ -1266,11 +1270,11 @@
1266
1270
  AssetProcessor.copyImageMeta = function (buffer, metadata) {
1267
1271
  return __awaiter$u(this, void 0, void 0, function () {
1268
1272
  var output;
1269
- return __generator(this, function (_a) {
1270
- switch (_a.label) {
1273
+ return __generator(this, function (_b) {
1274
+ switch (_b.label) {
1271
1275
  case 0: return [4 /*yield*/, sharp$3(buffer).rotate().toBuffer({ resolveWithObject: true })];
1272
1276
  case 1:
1273
- output = _a.sent();
1277
+ output = _b.sent();
1274
1278
  Object.assign(metadata, output.info);
1275
1279
  return [2 /*return*/, output.data];
1276
1280
  }
@@ -1287,18 +1291,18 @@
1287
1291
  metadata[prop] = font[prop];
1288
1292
  });
1289
1293
  };
1290
- AssetProcessor.prototype.process = function (buffer, metadata, contentType) {
1294
+ AssetProcessor.prototype.process = function (buffer, metadata, fileType) {
1291
1295
  return __awaiter$u(this, void 0, void 0, function () {
1292
- return __generator(this, function (_a) {
1293
- switch (_a.label) {
1296
+ return __generator(this, function (_b) {
1297
+ switch (_b.label) {
1294
1298
  case 0:
1295
- if (!AssetProcessor_1.isImage(contentType)) return [3 /*break*/, 2];
1299
+ if (!AssetProcessor_1.isImage(fileType.mime)) return [3 /*break*/, 2];
1296
1300
  return [4 /*yield*/, AssetProcessor_1.copyImageMeta(buffer, metadata)];
1297
1301
  case 1:
1298
- buffer = _a.sent();
1299
- _a.label = 2;
1302
+ buffer = _b.sent();
1303
+ _b.label = 2;
1300
1304
  case 2:
1301
- if (AssetProcessor_1.isFont(contentType)) {
1305
+ if (AssetProcessor_1.isFont(fileType.mime)) {
1302
1306
  AssetProcessor_1.copyFontMeta(buffer, metadata);
1303
1307
  }
1304
1308
  return [2 /*return*/, buffer];
@@ -1797,59 +1801,32 @@
1797
1801
  if (contentType === void 0) { contentType = null; }
1798
1802
  if (metadata === void 0) { metadata = null; }
1799
1803
  return __awaiter$q(this, void 0, void 0, function () {
1800
- var extension, fileTypeStream, uploadStream, fileType$1, e_1;
1801
- var _this = this;
1804
+ var uploadStream, buffer, fileType, e_1;
1802
1805
  return __generator(this, function (_b) {
1803
1806
  switch (_b.label) {
1804
1807
  case 0:
1805
- extension = null;
1806
- fileTypeStream = copyStream(stream);
1807
1808
  uploadStream = copyStream(stream);
1808
- _b.label = 1;
1809
+ return [4 /*yield*/, streamToBuffer(stream)];
1809
1810
  case 1:
1810
- _b.trys.push([1, 3, , 4]);
1811
- return [4 /*yield*/, fileType.fromStream(fileTypeStream)];
1811
+ buffer = _b.sent();
1812
+ fileType = { ext: "", mime: contentType };
1813
+ _b.label = 2;
1812
1814
  case 2:
1813
- fileType$1 = _b.sent();
1814
- contentType = fileType$1.mime;
1815
- extension = fileType$1.ext;
1816
- return [3 /*break*/, 4];
1815
+ _b.trys.push([2, 4, , 5]);
1816
+ return [4 /*yield*/, exports.AssetProcessor.fileTypeFromBuffer(buffer)];
1817
1817
  case 3:
1818
+ fileType = _b.sent();
1819
+ return [3 /*break*/, 5];
1820
+ case 4:
1818
1821
  e_1 = _b.sent();
1819
- if (!contentType) {
1820
- throw "Can't determine content type";
1822
+ if (!fileType.mime) {
1823
+ throw "Can't determine mime type";
1821
1824
  }
1822
- console.log("Can't determine content type", e_1);
1823
- return [3 /*break*/, 4];
1824
- case 4:
1825
- contentType = contentType.trim();
1826
- extension = (extension || "").trim();
1827
- metadata = Object.assign({
1828
- extension: extension,
1829
- downloadCount: 0,
1830
- firstDownload: null,
1831
- lastDownload: null
1832
- }, metadata || {});
1833
- metadata.filename = metadata.filename || new bson.ObjectId().toHexString();
1834
- return [2 /*return*/, new Promise((function (resolve, reject) {
1835
- var uploaderStream = _this.bucket.openUploadStream(metadata.filename);
1836
- uploadStream.pipe(uploaderStream)
1837
- .on("error", function (error) {
1838
- reject(error.message || error);
1839
- })
1840
- .on("finish", function () {
1841
- var asset = new Asset(uploaderStream.id, {
1842
- filename: metadata.filename,
1843
- contentType: contentType,
1844
- metadata: metadata
1845
- }, _this.collection, _this.bucket);
1846
- asset.save().then(function () {
1847
- resolve(asset);
1848
- }, function (error) {
1849
- reject(error.message || error);
1850
- });
1851
- });
1852
- }))];
1825
+ console.log("Can't determine mime type", e_1);
1826
+ return [3 /*break*/, 5];
1827
+ case 5:
1828
+ metadata = metadata || {};
1829
+ return [2 /*return*/, this.upload(uploadStream, fileType, metadata)];
1853
1830
  }
1854
1831
  });
1855
1832
  });
@@ -1858,16 +1835,31 @@
1858
1835
  if (metadata === void 0) { metadata = null; }
1859
1836
  if (contentType === void 0) { contentType = null; }
1860
1837
  return __awaiter$q(this, void 0, void 0, function () {
1838
+ var fileType, e_2;
1861
1839
  return __generator(this, function (_b) {
1862
1840
  switch (_b.label) {
1863
- case 0: return [4 /*yield*/, exports.AssetProcessor.getMimeType(buffer, contentType)];
1841
+ case 0:
1842
+ fileType = { ext: "", mime: contentType };
1843
+ _b.label = 1;
1864
1844
  case 1:
1865
- contentType = _b.sent();
1866
- metadata = metadata || {};
1867
- return [4 /*yield*/, this.assetProcessor.process(buffer, metadata, contentType)];
1845
+ _b.trys.push([1, 3, , 4]);
1846
+ return [4 /*yield*/, exports.AssetProcessor.fileTypeFromBuffer(buffer)];
1868
1847
  case 2:
1848
+ fileType = _b.sent();
1849
+ return [3 /*break*/, 4];
1850
+ case 3:
1851
+ e_2 = _b.sent();
1852
+ if (!fileType.mime) {
1853
+ throw "Can't determine mime type";
1854
+ }
1855
+ console.log("Can't determine mime type", e_2);
1856
+ return [3 /*break*/, 4];
1857
+ case 4:
1858
+ metadata = metadata || {};
1859
+ return [4 /*yield*/, this.assetProcessor.process(buffer, metadata, fileType)];
1860
+ case 5:
1869
1861
  buffer = _b.sent();
1870
- return [2 /*return*/, this.write(bufferToStream(buffer), contentType, metadata)];
1862
+ return [2 /*return*/, this.upload(bufferToStream(buffer), fileType, metadata)];
1871
1863
  }
1872
1864
  });
1873
1865
  });
@@ -1909,7 +1901,7 @@
1909
1901
  Assets.prototype.findMany = function (where) {
1910
1902
  return __awaiter$q(this, void 0, void 0, function () {
1911
1903
  var cursor, items, result, items_1, items_1_1, item;
1912
- var e_2, _b;
1904
+ var e_3, _b;
1913
1905
  return __generator(this, function (_c) {
1914
1906
  switch (_c.label) {
1915
1907
  case 0:
@@ -1926,12 +1918,12 @@
1926
1918
  result.push(new Asset(item._id, item, this.collection, this.bucket));
1927
1919
  }
1928
1920
  }
1929
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
1921
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
1930
1922
  finally {
1931
1923
  try {
1932
1924
  if (items_1_1 && !items_1_1.done && (_b = items_1.return)) _b.call(items_1);
1933
1925
  }
1934
- finally { if (e_2) throw e_2.error; }
1926
+ finally { if (e_3) throw e_3.error; }
1935
1927
  }
1936
1928
  return [2 /*return*/, result];
1937
1929
  }
@@ -1953,6 +1945,41 @@
1953
1945
  });
1954
1946
  });
1955
1947
  };
1948
+ Assets.prototype.upload = function (stream, fileType, metadata) {
1949
+ return __awaiter$q(this, void 0, void 0, function () {
1950
+ var contentType;
1951
+ var _this = this;
1952
+ return __generator(this, function (_b) {
1953
+ contentType = fileType.mime.trim();
1954
+ metadata = Object.assign({
1955
+ downloadCount: 0,
1956
+ firstDownload: null,
1957
+ lastDownload: null
1958
+ }, metadata || {});
1959
+ metadata.filename = metadata.filename || new bson.ObjectId().toHexString();
1960
+ metadata.extension = (fileType.ext || "").trim();
1961
+ return [2 /*return*/, new Promise((function (resolve, reject) {
1962
+ var uploaderStream = _this.bucket.openUploadStream(metadata.filename);
1963
+ stream.pipe(uploaderStream)
1964
+ .on("error", function (error) {
1965
+ reject(error.message || error);
1966
+ })
1967
+ .on("finish", function () {
1968
+ var asset = new Asset(uploaderStream.id, {
1969
+ filename: metadata.filename,
1970
+ contentType: contentType,
1971
+ metadata: metadata
1972
+ }, _this.collection, _this.bucket);
1973
+ asset.save().then(function () {
1974
+ resolve(asset);
1975
+ }, function (error) {
1976
+ reject(error.message || error);
1977
+ });
1978
+ });
1979
+ }))];
1980
+ });
1981
+ });
1982
+ };
1956
1983
  return Assets;
1957
1984
  }());
1958
1985
  exports.Assets = __decorate$t([
@@ -4546,36 +4573,42 @@
4546
4573
  });
4547
4574
  });
4548
4575
  };
4549
- AssetsController.prototype.getImageRotation = function (id, params, rotation) {
4576
+ AssetsController.prototype.getImageRotation = function (id, params, res, rotation) {
4550
4577
  if (rotation === void 0) { rotation = 0; }
4551
- var _a;
4552
4578
  return __awaiter$7(this, void 0, void 0, function () {
4553
4579
  var asset;
4554
4580
  return __generator(this, function (_c) {
4555
4581
  switch (_c.label) {
4556
- case 0: return [4 /*yield*/, this.assetResolver.resolve(id, params.lazy)];
4582
+ case 0: return [4 /*yield*/, this.getAsset("Image", id, params.lazy, res)];
4557
4583
  case 1:
4558
4584
  asset = _c.sent();
4559
- if (!asset) {
4560
- throw new routingControllers.HttpError(404, "Image with id: '" + id + "' not found.");
4561
- }
4562
- if ((_a = asset.metadata) === null || _a === void 0 ? void 0 : _a.classified) {
4563
- throw new routingControllers.HttpError(403, "Image is classified, and can be only downloaded from a custom url.");
4564
- }
4565
4585
  params.rotation = params.rotation || rotation;
4566
4586
  return [2 /*return*/, asset.downloadImage(params)];
4567
4587
  }
4568
4588
  });
4569
4589
  });
4570
4590
  };
4571
- AssetsController.prototype.getImage = function (id, params) {
4591
+ AssetsController.prototype.getImage = function (id, params, res) {
4572
4592
  return __awaiter$7(this, void 0, void 0, function () {
4573
4593
  return __generator(this, function (_c) {
4574
- return [2 /*return*/, this.getImageRotation(id, params)];
4594
+ return [2 /*return*/, this.getImageRotation(id, params, res)];
4575
4595
  });
4576
4596
  });
4577
4597
  };
4578
4598
  AssetsController.prototype.getFile = function (id, lazy, res) {
4599
+ return __awaiter$7(this, void 0, void 0, function () {
4600
+ var asset;
4601
+ return __generator(this, function (_c) {
4602
+ switch (_c.label) {
4603
+ case 0: return [4 /*yield*/, this.getAsset("Asset", id, lazy, res)];
4604
+ case 1:
4605
+ asset = _c.sent();
4606
+ return [2 /*return*/, asset.download()];
4607
+ }
4608
+ });
4609
+ });
4610
+ };
4611
+ AssetsController.prototype.getAsset = function (type, id, lazy, res) {
4579
4612
  var _a, _b;
4580
4613
  return __awaiter$7(this, void 0, void 0, function () {
4581
4614
  var asset, ext;
@@ -4585,16 +4618,19 @@
4585
4618
  case 1:
4586
4619
  asset = _c.sent();
4587
4620
  if (!asset) {
4588
- throw new routingControllers.HttpError(404, "File with id: '" + id + "' not found.");
4621
+ throw new routingControllers.HttpError(404, type + " with id: '" + id + "' not found.");
4589
4622
  }
4590
4623
  if ((_a = asset.metadata) === null || _a === void 0 ? void 0 : _a.classified) {
4591
- throw new routingControllers.HttpError(403, "Asset is classified, and can be only downloaded from a custom url.");
4624
+ throw new routingControllers.HttpError(403, type + " is classified, and can be only downloaded from a custom url.");
4592
4625
  }
4593
4626
  ext = (_b = asset.metadata) === null || _b === void 0 ? void 0 : _b.extension;
4594
4627
  if (ext) {
4595
4628
  res.header("content-disposition", "inline; filename=" + asset.filename + "." + ext);
4596
4629
  }
4597
- return [2 /*return*/, asset.download()];
4630
+ if (asset.contentType) {
4631
+ res.header("content-type", asset.contentType);
4632
+ }
4633
+ return [2 /*return*/, asset];
4598
4634
  }
4599
4635
  });
4600
4636
  });
@@ -4619,16 +4655,16 @@
4619
4655
  ], AssetsController.prototype, "uploadUrl", null);
4620
4656
  __decorate$a([
4621
4657
  routingControllers.Get("/image/:id/:rotation"),
4622
- __param$5(0, routingControllers.Param("id")), __param$5(1, routingControllers.QueryParams()), __param$5(2, routingControllers.Param("rotation")),
4658
+ __param$5(0, routingControllers.Param("id")), __param$5(1, routingControllers.QueryParams()), __param$5(2, routingControllers.Res()), __param$5(3, routingControllers.Param("rotation")),
4623
4659
  __metadata$7("design:type", Function),
4624
- __metadata$7("design:paramtypes", [String, Object, Number]),
4660
+ __metadata$7("design:paramtypes", [String, Object, Object, Number]),
4625
4661
  __metadata$7("design:returntype", Promise)
4626
4662
  ], AssetsController.prototype, "getImageRotation", null);
4627
4663
  __decorate$a([
4628
4664
  routingControllers.Get("/image/:id"),
4629
- __param$5(0, routingControllers.Param("id")), __param$5(1, routingControllers.QueryParams()),
4665
+ __param$5(0, routingControllers.Param("id")), __param$5(1, routingControllers.QueryParams()), __param$5(2, routingControllers.Res()),
4630
4666
  __metadata$7("design:type", Function),
4631
- __metadata$7("design:paramtypes", [String, Object]),
4667
+ __metadata$7("design:paramtypes", [String, Object, Object]),
4632
4668
  __metadata$7("design:returntype", Promise)
4633
4669
  ], AssetsController.prototype, "getImage", null);
4634
4670
  __decorate$a([