@stemy/backend 5.0.10 → 5.1.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.
@@ -614,8 +614,14 @@ function gunzipPromised(data, opts) {
614
614
  });
615
615
  });
616
616
  }
617
- function deleteFromBucket(bucket, fileId) {
617
+ function deleteFromBucket(bucket, id) {
618
+ const fileId = id instanceof ObjectId ? id : new ObjectId(id);
618
619
  return new Promise(((resolve, reject) => {
620
+ if (!id) {
621
+ // We don't care about empty id
622
+ resolve(null);
623
+ return;
624
+ }
619
625
  bucket.delete(fileId, error => {
620
626
  let err = error;
621
627
  if (error) {
@@ -999,6 +1005,10 @@ class Asset extends BaseEntity {
999
1005
  async unlink() {
1000
1006
  return deleteFromBucket(this.bucket, this.mId);
1001
1007
  }
1008
+ async setMeta(metadata) {
1009
+ metadata = Object.assign(this.metadata, metadata || {});
1010
+ await this.collection.updateOne({ _id: this.mId }, { $set: { metadata } });
1011
+ }
1002
1012
  getBuffer() {
1003
1013
  return streamToBuffer(this.stream);
1004
1014
  }
@@ -1034,6 +1044,9 @@ class TempAsset {
1034
1044
  async unlink() {
1035
1045
  throw new Error(`Temp asset '${this.id}' can not be removed!`);
1036
1046
  }
1047
+ async setMeta(meta) {
1048
+ Object.assign(this.metadata, meta || {});
1049
+ }
1037
1050
  async getBuffer() {
1038
1051
  return this.buffer;
1039
1052
  }
@@ -1212,6 +1225,12 @@ class LazyAsset extends BaseEntity {
1212
1225
  get jobQue() {
1213
1226
  return this.data.jobQue;
1214
1227
  }
1228
+ get createdAt() {
1229
+ return this.data.createdAt;
1230
+ }
1231
+ get updatedAt() {
1232
+ return this.data.updatedAt;
1233
+ }
1215
1234
  get progressId() {
1216
1235
  return this.data.progressId;
1217
1236
  }
@@ -1254,14 +1273,19 @@ class LazyAsset extends BaseEntity {
1254
1273
  return this.loadAsset();
1255
1274
  }
1256
1275
  async writeAsset(asset) {
1276
+ this.data.updatedAt = new Date();
1257
1277
  this.data.assetId = asset.id;
1278
+ await asset.setMeta({ lazyId: this.id });
1258
1279
  await this.save();
1259
1280
  return asset;
1260
1281
  }
1261
1282
  async startWorkingOnAsset(fromLoad) {
1283
+ const oldAsset = this.data.assetId;
1284
+ this.data.updatedAt = new Date();
1262
1285
  this.data.progressId = (await this.progresses.create()).id;
1263
1286
  this.data.assetId = null;
1264
1287
  await this.save();
1288
+ await deleteFromBucket(this.assets.bucket, oldAsset);
1265
1289
  const jobParams = JSON.parse(await gunzipPromised(this.data.jobParams));
1266
1290
  await this.progresses.jobMan.enqueueWithName(this.data.jobName, { ...jobParams, lazyId: this.id, fromLoad });
1267
1291
  }
@@ -1731,11 +1755,13 @@ let LazyAssets = class LazyAssets {
1731
1755
  const data = {
1732
1756
  jobName,
1733
1757
  jobParams,
1734
- jobQue
1758
+ jobQue,
1735
1759
  };
1736
1760
  const existingAsset = await this.find(data);
1737
1761
  if (existingAsset)
1738
1762
  return existingAsset;
1763
+ data.createdAt = new Date();
1764
+ data.updatedAt = data.createdAt;
1739
1765
  const res = await this.collection.insertOne(data);
1740
1766
  return new LazyAsset(res.insertedId, data, this.collection, this.logger, this.assets, this.progresses);
1741
1767
  }