@stemy/backend 2.8.6 → 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.
- package/bundles/stemy-backend.umd.js +109 -78
- package/bundles/stemy-backend.umd.js.map +1 -1
- package/bundles/stemy-backend.umd.min.js +1 -1
- package/bundles/stemy-backend.umd.min.js.map +1 -1
- package/common-types.d.ts +4 -0
- package/esm2015/common-types.js +1 -1
- package/esm2015/public_api.js +1 -1
- package/esm2015/rest-controllers/assets.controller.js +4 -1
- package/esm2015/services/asset-processor.js +20 -14
- package/esm2015/services/assets.js +54 -42
- package/fesm2015/stemy-backend.js +75 -53
- package/fesm2015/stemy-backend.js.map +1 -1
- package/package.json +1 -1
- package/public_api.d.ts +1 -1
- package/services/asset-processor.d.ts +5 -3
- package/services/assets.d.ts +2 -1
- package/stemy-backend.metadata.json +1 -1
|
@@ -18,7 +18,7 @@ import { getValue as getValue$1, setValue } from 'mongoose/lib/utils';
|
|
|
18
18
|
import { PassThrough, Readable } from 'stream';
|
|
19
19
|
import { ObjectId } from 'bson';
|
|
20
20
|
import fontKit_ from 'fontkit';
|
|
21
|
-
import { fromBuffer
|
|
21
|
+
import { fromBuffer } from 'file-type';
|
|
22
22
|
import sharp_ from 'sharp';
|
|
23
23
|
import axios from 'axios';
|
|
24
24
|
import { GridFSBucket } from 'mongodb';
|
|
@@ -774,18 +774,24 @@ const fontProps = [
|
|
|
774
774
|
"xHeight", "numGlyphs", "characterSet", "availableFeatures"
|
|
775
775
|
];
|
|
776
776
|
let AssetProcessor = AssetProcessor_1 = class AssetProcessor {
|
|
777
|
-
static
|
|
777
|
+
static checkTextFileType(type) {
|
|
778
|
+
return type.mime.indexOf("text") >= 0 || type.mime.indexOf("xml") >= 0;
|
|
779
|
+
}
|
|
780
|
+
static fixTextFileType(type, buffer) {
|
|
781
|
+
const text = buffer.toString("utf8");
|
|
782
|
+
if (text.indexOf("<svg") >= 0) {
|
|
783
|
+
return { ext: "svg", mime: "image/svg+xml" };
|
|
784
|
+
}
|
|
785
|
+
return type;
|
|
786
|
+
}
|
|
787
|
+
static fileTypeFromBuffer(buffer) {
|
|
788
|
+
var _a;
|
|
778
789
|
return __awaiter$u(this, void 0, void 0, function* () {
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
catch (e) {
|
|
783
|
-
if (!mimeType) {
|
|
784
|
-
throw `Can't determine mime type`;
|
|
785
|
-
}
|
|
786
|
-
console.log(`Can't determine mime type`, e);
|
|
790
|
+
const type = ((_a = yield fromBuffer(buffer)) !== null && _a !== void 0 ? _a : { ext: "txt", mime: "text/plain" });
|
|
791
|
+
if (AssetProcessor_1.checkTextFileType(type)) {
|
|
792
|
+
return AssetProcessor_1.fixTextFileType(type, buffer);
|
|
787
793
|
}
|
|
788
|
-
return
|
|
794
|
+
return type;
|
|
789
795
|
});
|
|
790
796
|
}
|
|
791
797
|
static extractFontFormat(font) {
|
|
@@ -823,12 +829,12 @@ let AssetProcessor = AssetProcessor_1 = class AssetProcessor {
|
|
|
823
829
|
metadata[prop] = font[prop];
|
|
824
830
|
});
|
|
825
831
|
}
|
|
826
|
-
process(buffer, metadata,
|
|
832
|
+
process(buffer, metadata, fileType) {
|
|
827
833
|
return __awaiter$u(this, void 0, void 0, function* () {
|
|
828
|
-
if (AssetProcessor_1.isImage(
|
|
834
|
+
if (AssetProcessor_1.isImage(fileType.mime)) {
|
|
829
835
|
buffer = yield AssetProcessor_1.copyImageMeta(buffer, metadata);
|
|
830
836
|
}
|
|
831
|
-
if (AssetProcessor_1.isFont(
|
|
837
|
+
if (AssetProcessor_1.isFont(fileType.mime)) {
|
|
832
838
|
AssetProcessor_1.copyFontMeta(buffer, metadata);
|
|
833
839
|
}
|
|
834
840
|
return buffer;
|
|
@@ -1171,56 +1177,37 @@ let Assets = class Assets {
|
|
|
1171
1177
|
}
|
|
1172
1178
|
write(stream, contentType = null, metadata = null) {
|
|
1173
1179
|
return __awaiter$q(this, void 0, void 0, function* () {
|
|
1174
|
-
let extension = null;
|
|
1175
|
-
const fileTypeStream = copyStream(stream);
|
|
1176
1180
|
const uploadStream = copyStream(stream);
|
|
1181
|
+
const buffer = yield streamToBuffer(stream);
|
|
1182
|
+
let fileType = { ext: "", mime: contentType };
|
|
1177
1183
|
try {
|
|
1178
|
-
|
|
1179
|
-
contentType = fileType.mime;
|
|
1180
|
-
extension = fileType.ext;
|
|
1184
|
+
fileType = yield AssetProcessor.fileTypeFromBuffer(buffer);
|
|
1181
1185
|
}
|
|
1182
1186
|
catch (e) {
|
|
1183
|
-
if (!
|
|
1184
|
-
throw `Can't determine
|
|
1187
|
+
if (!fileType.mime) {
|
|
1188
|
+
throw `Can't determine mime type`;
|
|
1185
1189
|
}
|
|
1186
|
-
console.log(`Can't determine
|
|
1190
|
+
console.log(`Can't determine mime type`, e);
|
|
1187
1191
|
}
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
metadata = Object.assign({
|
|
1191
|
-
extension,
|
|
1192
|
-
downloadCount: 0,
|
|
1193
|
-
firstDownload: null,
|
|
1194
|
-
lastDownload: null
|
|
1195
|
-
}, metadata || {});
|
|
1196
|
-
metadata.filename = metadata.filename || new ObjectId().toHexString();
|
|
1197
|
-
return new Promise(((resolve, reject) => {
|
|
1198
|
-
const uploaderStream = this.bucket.openUploadStream(metadata.filename);
|
|
1199
|
-
uploadStream.pipe(uploaderStream)
|
|
1200
|
-
.on("error", error => {
|
|
1201
|
-
reject(error.message || error);
|
|
1202
|
-
})
|
|
1203
|
-
.on("finish", () => {
|
|
1204
|
-
const asset = new Asset(uploaderStream.id, {
|
|
1205
|
-
filename: metadata.filename,
|
|
1206
|
-
contentType,
|
|
1207
|
-
metadata
|
|
1208
|
-
}, this.collection, this.bucket);
|
|
1209
|
-
asset.save().then(() => {
|
|
1210
|
-
resolve(asset);
|
|
1211
|
-
}, error => {
|
|
1212
|
-
reject(error.message || error);
|
|
1213
|
-
});
|
|
1214
|
-
});
|
|
1215
|
-
}));
|
|
1192
|
+
metadata = metadata || {};
|
|
1193
|
+
return this.upload(uploadStream, fileType, metadata);
|
|
1216
1194
|
});
|
|
1217
1195
|
}
|
|
1218
1196
|
writeBuffer(buffer, metadata = null, contentType = null) {
|
|
1219
1197
|
return __awaiter$q(this, void 0, void 0, function* () {
|
|
1220
|
-
|
|
1198
|
+
let fileType = { ext: "", mime: contentType };
|
|
1199
|
+
try {
|
|
1200
|
+
fileType = yield AssetProcessor.fileTypeFromBuffer(buffer);
|
|
1201
|
+
}
|
|
1202
|
+
catch (e) {
|
|
1203
|
+
if (!fileType.mime) {
|
|
1204
|
+
throw `Can't determine mime type`;
|
|
1205
|
+
}
|
|
1206
|
+
console.log(`Can't determine mime type`, e);
|
|
1207
|
+
}
|
|
1221
1208
|
metadata = metadata || {};
|
|
1222
|
-
buffer = yield this.assetProcessor.process(buffer, metadata,
|
|
1223
|
-
return this.
|
|
1209
|
+
buffer = yield this.assetProcessor.process(buffer, metadata, fileType);
|
|
1210
|
+
return this.upload(bufferToStream(buffer), fileType, metadata);
|
|
1224
1211
|
});
|
|
1225
1212
|
}
|
|
1226
1213
|
writeUrl(url, metadata = null) {
|
|
@@ -1261,6 +1248,38 @@ let Assets = class Assets {
|
|
|
1261
1248
|
return asset.unlink();
|
|
1262
1249
|
});
|
|
1263
1250
|
}
|
|
1251
|
+
upload(stream, fileType, metadata) {
|
|
1252
|
+
return __awaiter$q(this, void 0, void 0, function* () {
|
|
1253
|
+
const contentType = fileType.mime.trim();
|
|
1254
|
+
const extension = (fileType.ext || "").trim();
|
|
1255
|
+
metadata = Object.assign({
|
|
1256
|
+
extension,
|
|
1257
|
+
downloadCount: 0,
|
|
1258
|
+
firstDownload: null,
|
|
1259
|
+
lastDownload: null
|
|
1260
|
+
}, metadata || {});
|
|
1261
|
+
metadata.filename = metadata.filename || new ObjectId().toHexString();
|
|
1262
|
+
return new Promise(((resolve, reject) => {
|
|
1263
|
+
const uploaderStream = this.bucket.openUploadStream(metadata.filename);
|
|
1264
|
+
stream.pipe(uploaderStream)
|
|
1265
|
+
.on("error", error => {
|
|
1266
|
+
reject(error.message || error);
|
|
1267
|
+
})
|
|
1268
|
+
.on("finish", () => {
|
|
1269
|
+
const asset = new Asset(uploaderStream.id, {
|
|
1270
|
+
filename: metadata.filename,
|
|
1271
|
+
contentType,
|
|
1272
|
+
metadata
|
|
1273
|
+
}, this.collection, this.bucket);
|
|
1274
|
+
asset.save().then(() => {
|
|
1275
|
+
resolve(asset);
|
|
1276
|
+
}, error => {
|
|
1277
|
+
reject(error.message || error);
|
|
1278
|
+
});
|
|
1279
|
+
});
|
|
1280
|
+
}));
|
|
1281
|
+
});
|
|
1282
|
+
}
|
|
1264
1283
|
};
|
|
1265
1284
|
Assets = __decorate$t([
|
|
1266
1285
|
injectable(),
|
|
@@ -2893,6 +2912,9 @@ let AssetsController = class AssetsController {
|
|
|
2893
2912
|
if (ext) {
|
|
2894
2913
|
res.header("content-disposition", `inline; filename=${asset.filename}.${ext}`);
|
|
2895
2914
|
}
|
|
2915
|
+
if (asset.contentType) {
|
|
2916
|
+
res.header("content-type", asset.contentType);
|
|
2917
|
+
}
|
|
2896
2918
|
return asset.download();
|
|
2897
2919
|
});
|
|
2898
2920
|
}
|