@stemy/backend 2.8.3 → 2.9.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.
@@ -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
  });
@@ -1889,7 +1881,7 @@
1889
1881
  Assets.prototype.read = function (id) {
1890
1882
  return __awaiter$q(this, void 0, void 0, function () {
1891
1883
  return __generator(this, function (_b) {
1892
- return [2 /*return*/, this.find({ _id: new bson.ObjectId(id) })];
1884
+ return [2 /*return*/, !id ? null : this.find({ _id: new bson.ObjectId(id) })];
1893
1885
  });
1894
1886
  });
1895
1887
  };
@@ -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,42 @@
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, extension;
1951
+ var _this = this;
1952
+ return __generator(this, function (_b) {
1953
+ contentType = fileType.mime.trim();
1954
+ extension = (fileType.ext || "").trim();
1955
+ metadata = Object.assign({
1956
+ extension: extension,
1957
+ downloadCount: 0,
1958
+ firstDownload: null,
1959
+ lastDownload: null
1960
+ }, metadata || {});
1961
+ metadata.filename = metadata.filename || new bson.ObjectId().toHexString();
1962
+ return [2 /*return*/, new Promise((function (resolve, reject) {
1963
+ var uploaderStream = _this.bucket.openUploadStream(metadata.filename);
1964
+ stream.pipe(uploaderStream)
1965
+ .on("error", function (error) {
1966
+ reject(error.message || error);
1967
+ })
1968
+ .on("finish", function () {
1969
+ var asset = new Asset(uploaderStream.id, {
1970
+ filename: metadata.filename,
1971
+ contentType: contentType,
1972
+ metadata: metadata
1973
+ }, _this.collection, _this.bucket);
1974
+ asset.save().then(function () {
1975
+ resolve(asset);
1976
+ }, function (error) {
1977
+ reject(error.message || error);
1978
+ });
1979
+ });
1980
+ }))];
1981
+ });
1982
+ });
1983
+ };
1956
1984
  return Assets;
1957
1985
  }());
1958
1986
  exports.Assets = __decorate$t([
@@ -2046,13 +2074,13 @@
2046
2074
  this.load().then(function () {
2047
2075
  if (_this.deleted)
2048
2076
  return;
2049
- var progressPromise = !_this.progressId ? Promise.resolve(null) : _this.progresses.get(_this.progressId).then(function (p) { return p.cancel(); });
2050
- progressPromise.then(function () {
2051
- _this.startWorkingOnAsset().then(function () {
2052
- console.log("Started working on lazy asset: " + _this.id);
2053
- }).catch(function (reason) {
2054
- console.log("Can't start working on lazy asset: " + _this.id + "\nReason: " + reason);
2055
- });
2077
+ _this.progresses.get(_this.progressId).then(function (p) {
2078
+ p === null || p === void 0 ? void 0 : p.cancel();
2079
+ });
2080
+ _this.startWorkingOnAsset().then(function () {
2081
+ console.log("Started working on lazy asset: " + _this.id);
2082
+ }).catch(function (reason) {
2083
+ console.log("Can't start working on lazy asset: " + _this.id + "\nReason: " + reason);
2056
2084
  });
2057
2085
  });
2058
2086
  };
@@ -2097,19 +2125,21 @@
2097
2125
  };
2098
2126
  LazyAsset.prototype.startWorkingOnAsset = function () {
2099
2127
  return __awaiter$p(this, void 0, void 0, function () {
2100
- var id;
2101
- return __generator(this, function (_a) {
2102
- switch (_a.label) {
2103
- case 0: return [4 /*yield*/, this.progresses.create()];
2128
+ var _a;
2129
+ return __generator(this, function (_b) {
2130
+ switch (_b.label) {
2131
+ case 0:
2132
+ _a = this.data;
2133
+ return [4 /*yield*/, this.progresses.create()];
2104
2134
  case 1:
2105
- id = (_a.sent()).id;
2106
- this.data.progressId = id;
2135
+ _a.progressId = (_b.sent()).id;
2136
+ this.data.assetId = null;
2107
2137
  return [4 /*yield*/, this.save()];
2108
2138
  case 2:
2109
- _a.sent();
2139
+ _b.sent();
2110
2140
  return [4 /*yield*/, this.jobMan.enqueueWithName(this.data.jobName, Object.assign(Object.assign({}, this.data.jobParams), { lazyId: this.id }))];
2111
2141
  case 3:
2112
- _a.sent();
2142
+ _b.sent();
2113
2143
  return [2 /*return*/];
2114
2144
  }
2115
2145
  });
@@ -2873,7 +2903,7 @@
2873
2903
  Progresses.prototype.get = function (id) {
2874
2904
  return __awaiter$m(this, void 0, void 0, function () {
2875
2905
  return __generator(this, function (_a) {
2876
- return [2 /*return*/, this.find({ _id: new bson.ObjectId(id) })];
2906
+ return [2 /*return*/, !id ? null : this.find({ _id: new bson.ObjectId(id) })];
2877
2907
  });
2878
2908
  });
2879
2909
  };
@@ -3006,7 +3036,7 @@
3006
3036
  LazyAssets.prototype.read = function (id) {
3007
3037
  return __awaiter$l(this, void 0, void 0, function () {
3008
3038
  return __generator(this, function (_a) {
3009
- return [2 /*return*/, this.find({ _id: new bson.ObjectId(id) })];
3039
+ return [2 /*return*/, !id ? null : this.find({ _id: new bson.ObjectId(id) })];
3010
3040
  });
3011
3041
  });
3012
3042
  };
@@ -4592,6 +4622,9 @@
4592
4622
  if (ext) {
4593
4623
  res.header("content-disposition", "inline; filename=" + asset.filename + "." + ext);
4594
4624
  }
4625
+ if (asset.contentType) {
4626
+ res.header("content-type", asset.contentType);
4627
+ }
4595
4628
  return [2 /*return*/, asset.download()];
4596
4629
  }
4597
4630
  });