@lacrous/omnicloud 0.2.13 → 0.2.14

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/dist/index.cjs CHANGED
@@ -1542,6 +1542,7 @@ var FileService = class {
1542
1542
  const record = await this.performUpload(userId, input, {
1543
1543
  onStored: async (stored) => {
1544
1544
  await operations.update(op.id, { telegramMessageId: Number(stored.messageId) });
1545
+ await operations.claim(op.id, "UPLOADING", "COMMITTING");
1545
1546
  }
1546
1547
  });
1547
1548
  await operations.update(op.id, { status: "COMPLETED", fileId: record.id });
@@ -1582,13 +1583,27 @@ var FileService = class {
1582
1583
  const mimeType = mimeFromFilename(name);
1583
1584
  const size = op.size ?? input.spooled?.size ?? input.data?.byteLength ?? 0;
1584
1585
  const sha256 = op.sha256 ?? input.spooled?.sha256 ?? sha256Hex(input.data ?? Buffer.alloc(0));
1585
- const record = await this.commitRecord(userId, input, {
1586
- name,
1587
- mimeType,
1588
- size,
1589
- sha256,
1590
- telegramMessageId: op.telegramMessageId
1591
- });
1586
+ const claimed = await operations.claim(op.id, "UPLOADING", "COMMITTING");
1587
+ if (!claimed) {
1588
+ const current = await operations.findByOperationId(userId, op.operationId);
1589
+ if (current?.status === "COMPLETED" && current.fileId) {
1590
+ return this.get(userId, current.fileId);
1591
+ }
1592
+ throw new ConflictError("This upload is being committed by another request. Retry shortly.");
1593
+ }
1594
+ let record;
1595
+ try {
1596
+ record = await this.commitRecord(userId, input, {
1597
+ name,
1598
+ mimeType,
1599
+ size,
1600
+ sha256,
1601
+ telegramMessageId: op.telegramMessageId
1602
+ });
1603
+ } catch (error) {
1604
+ await operations.claim(op.id, "COMMITTING", "UPLOADING");
1605
+ throw error;
1606
+ }
1592
1607
  await operations.update(op.id, { status: "COMPLETED", fileId: record.id });
1593
1608
  return record;
1594
1609
  }