@did-space/core 0.3.9 → 0.3.11

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.
@@ -8,17 +8,50 @@ export interface Object {
8
8
  * @memberof Object
9
9
  */
10
10
  key: string;
11
+ /**
12
+ * @description 文件名
13
+ * @example index.html
14
+ * @type {string}
15
+ * @memberof Object
16
+ */
11
17
  name: string;
18
+ /**
19
+ * @description
20
+ * @type {boolean}
21
+ * @memberof Object
22
+ */
12
23
  isDir: boolean;
24
+ /**
25
+ * @description 文件的大小
26
+ * @type {number}
27
+ * @memberof Object
28
+ */
13
29
  size: number;
30
+ /**
31
+ * @description
32
+ * @type {number}
33
+ * @memberof Object
34
+ */
14
35
  lastModified: number;
36
+ /**
37
+ * @description
38
+ * @type {boolean}
39
+ * @memberof Object
40
+ */
15
41
  editable: boolean;
16
42
  /**
17
43
  * @description 如果是一个文件夹,则为 null
44
+ * @example video/mp4
18
45
  * @type {string}
19
46
  * @memberof Object
20
47
  */
21
48
  mimeType: string;
49
+ /**
50
+ * @description
51
+ * @type {string}
52
+ * @memberof Object
53
+ */
54
+ absolutePath: string;
22
55
  /**
23
56
  * @description 对象的 hash 值(ipfs CID v1)
24
57
  * @type {string}
@@ -64,21 +64,40 @@ class GlobalSpace {
64
64
  raw: true,
65
65
  });
66
66
  debug('add.$objectRecord', JSON.stringify(objectRecord));
67
- if (objectRecord) {
67
+ // @note: 运行时的纠错机制,我们发现 size 有时候保存出错(修复后将不可能出现),必须 hash 和 size 都相同,才不需要修改底层存储
68
+ if (objectRecord && objectRecord.size === options.size) {
68
69
  return;
69
70
  }
70
71
  // 存储新对象到全局存储区
71
72
  const newKey = (0, utils_1.getHashPath)(options.hash);
72
73
  yield this.driver.writeAsOwner(newKey, data, options);
73
74
  // 添加 object 记录
74
- yield this.objectRepository.create({
75
- id: options.hash,
76
- size: options.size,
77
- meta: {
78
- mimeType: mime_types_1.default.lookup(key) || null,
79
- key,
80
- },
81
- });
75
+ if (objectRecord) {
76
+ // @note: 运行时的纠错机制, 我们发现 size 有时候保存出错(修复后将不可能出现),这个是一个兜底方案,但是这是一个稳健的做法
77
+ yield this.objectRepository.update({
78
+ size: options.size,
79
+ meta: {
80
+ mimeType: mime_types_1.default.lookup(key) || null,
81
+ key,
82
+ },
83
+ }, {
84
+ where: {
85
+ id: options.hash,
86
+ },
87
+ });
88
+ }
89
+ else {
90
+ yield this.objectRepository.create({
91
+ id: options.hash,
92
+ size: options.size,
93
+ meta: {
94
+ mimeType: mime_types_1.default.lookup(key) || null,
95
+ key,
96
+ },
97
+ }, {
98
+ ignoreDuplicates: true,
99
+ });
100
+ }
82
101
  debug('add.after', JSON.stringify({ objectRecord }));
83
102
  });
84
103
  }
@@ -221,6 +221,7 @@ class ObjectSpace {
221
221
  include: this.objectRepository,
222
222
  });
223
223
  return Promise.all(trees.map((x) => __awaiter(this, void 0, void 0, function* () {
224
+ const absolutePath = (0, path_1.join)(x.spaceDid, x.key);
224
225
  if (x.type === model_1.TreeModelType.FOLDER) {
225
226
  const { size, lastModified } = yield this.getStatusAsOwner(x.key);
226
227
  return {
@@ -231,6 +232,7 @@ class ObjectSpace {
231
232
  lastModified: lastModified || new Date(x.updatedAt).getTime(),
232
233
  editable: true,
233
234
  mimeType: null,
235
+ absolutePath,
234
236
  };
235
237
  }
236
238
  return {
@@ -242,6 +244,7 @@ class ObjectSpace {
242
244
  editable: true,
243
245
  mimeType: x.Object.meta.mimeType,
244
246
  hash: x.objectId,
247
+ absolutePath,
245
248
  };
246
249
  })));
247
250
  });
@@ -270,6 +273,7 @@ class ObjectSpace {
270
273
  editable: true,
271
274
  mimeType: x.Object.meta.mimeType,
272
275
  hash: x.objectId,
276
+ absolutePath: (0, path_1.join)(x.spaceDid, x.key),
273
277
  };
274
278
  });
275
279
  });
@@ -324,9 +328,9 @@ class ObjectSpace {
324
328
  isDir: x.type === model_1.TreeModelType.FOLDER,
325
329
  size: x.type === model_1.TreeModelType.FOLDER ? 0 : x.Object.size,
326
330
  lastModified: new Date(x.updatedAt).getTime(),
327
- // FIXME: 需要后续处理一下
328
331
  editable: true,
329
332
  mimeType: x.type === model_1.TreeModelType.FOLDER ? null : x.Object.meta.mimeType,
333
+ absolutePath: (0, path_1.join)(x.spaceDid, x.key),
330
334
  };
331
335
  });
332
336
  }
@@ -343,10 +347,11 @@ class ObjectSpace {
343
347
  debug('_updateAsOwner.before', JSON.stringify({ key, options: (0, lodash_1.omit)(options, 'data'), where }));
344
348
  const oldTree = yield this.treeRepository.findOne({
345
349
  where,
346
- raw: true,
350
+ include: this.objectRepository,
347
351
  });
348
352
  debug('_updateAsOwner.$oldTree', JSON.stringify(oldTree));
349
- if (oldTree.objectId === options.hash) {
353
+ // @note: 运行时的纠错机制,我们发现 size 有时候保存出错(修复后将不可能出现),必须 hash 和 size 都相同,才不需要修改底层存储
354
+ if (oldTree.objectId === options.hash && oldTree.Object.size === options.size) {
350
355
  // 内容不变,不需要再次存储对象,只需要更新对象的事件即可
351
356
  yield this.treeRepository.update({
352
357
  updatedAt: new Date().toISOString(),
@@ -373,7 +378,7 @@ class ObjectSpace {
373
378
  }
374
379
  _createAsOwner(key, data, options) {
375
380
  return __awaiter(this, void 0, void 0, function* () {
376
- debug('_createAsOwner.before', JSON.stringify({ key, options }));
381
+ debug('_createAsOwner.before', JSON.stringify({ key, options: (0, lodash_1.omit)(options, 'data') }));
377
382
  const isDir = key.endsWith('/');
378
383
  if (isDir) {
379
384
  // 添加 tree 记录
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@did-space/core",
3
- "version": "0.3.9",
3
+ "version": "0.3.11",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -36,7 +36,7 @@
36
36
  ]
37
37
  },
38
38
  "dependencies": {
39
- "@arcblock/validator": "^1.18.91",
39
+ "@arcblock/validator": "^1.18.92",
40
40
  "debug": "^4.3.4",
41
41
  "hasha": "^5.2.2",
42
42
  "ipfs-only-hash": "^4.0.0",
@@ -60,5 +60,5 @@
60
60
  "ts-jest": "^28.0.6",
61
61
  "typescript": "^4.9.5"
62
62
  },
63
- "gitHead": "82db55693ead83d0185ae410b0660a53f996a6e9"
63
+ "gitHead": "c6d855ee4ae889b24a3ba7575059f03c348353f9"
64
64
  }