@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.d.cts CHANGED
@@ -809,11 +809,13 @@ interface StorageStats {
809
809
  *
810
810
  * PENDING accepted, Telegram write not started
811
811
  * UPLOADING Telegram write in progress or its outcome unknown
812
+ * COMMITTING the stored object is being turned into a file record by exactly one
813
+ * request, which holds this state as an exclusive claim
812
814
  * COMPLETED Telegram object stored and file record committed; result is final
813
815
  * FAILED Telegram write definitively failed; the operation may be retried
814
816
  * under a new id
815
817
  */
816
- type UploadOperationStatus = "PENDING" | "UPLOADING" | "COMPLETED" | "FAILED";
818
+ type UploadOperationStatus = "PENDING" | "UPLOADING" | "COMMITTING" | "COMPLETED" | "FAILED";
817
819
 
818
820
  /**
819
821
  * Repository interfaces. The application depends on these, not on a specific
package/dist/index.d.ts CHANGED
@@ -809,11 +809,13 @@ interface StorageStats {
809
809
  *
810
810
  * PENDING accepted, Telegram write not started
811
811
  * UPLOADING Telegram write in progress or its outcome unknown
812
+ * COMMITTING the stored object is being turned into a file record by exactly one
813
+ * request, which holds this state as an exclusive claim
812
814
  * COMPLETED Telegram object stored and file record committed; result is final
813
815
  * FAILED Telegram write definitively failed; the operation may be retried
814
816
  * under a new id
815
817
  */
816
- type UploadOperationStatus = "PENDING" | "UPLOADING" | "COMPLETED" | "FAILED";
818
+ type UploadOperationStatus = "PENDING" | "UPLOADING" | "COMMITTING" | "COMPLETED" | "FAILED";
817
819
 
818
820
  /**
819
821
  * Repository interfaces. The application depends on these, not on a specific
package/dist/index.js CHANGED
@@ -1479,6 +1479,7 @@ var FileService = class {
1479
1479
  const record = await this.performUpload(userId, input, {
1480
1480
  onStored: async (stored) => {
1481
1481
  await operations.update(op.id, { telegramMessageId: Number(stored.messageId) });
1482
+ await operations.claim(op.id, "UPLOADING", "COMMITTING");
1482
1483
  }
1483
1484
  });
1484
1485
  await operations.update(op.id, { status: "COMPLETED", fileId: record.id });
@@ -1519,13 +1520,27 @@ var FileService = class {
1519
1520
  const mimeType = mimeFromFilename(name);
1520
1521
  const size = op.size ?? input.spooled?.size ?? input.data?.byteLength ?? 0;
1521
1522
  const sha256 = op.sha256 ?? input.spooled?.sha256 ?? sha256Hex(input.data ?? Buffer.alloc(0));
1522
- const record = await this.commitRecord(userId, input, {
1523
- name,
1524
- mimeType,
1525
- size,
1526
- sha256,
1527
- telegramMessageId: op.telegramMessageId
1528
- });
1523
+ const claimed = await operations.claim(op.id, "UPLOADING", "COMMITTING");
1524
+ if (!claimed) {
1525
+ const current = await operations.findByOperationId(userId, op.operationId);
1526
+ if (current?.status === "COMPLETED" && current.fileId) {
1527
+ return this.get(userId, current.fileId);
1528
+ }
1529
+ throw new ConflictError("This upload is being committed by another request. Retry shortly.");
1530
+ }
1531
+ let record;
1532
+ try {
1533
+ record = await this.commitRecord(userId, input, {
1534
+ name,
1535
+ mimeType,
1536
+ size,
1537
+ sha256,
1538
+ telegramMessageId: op.telegramMessageId
1539
+ });
1540
+ } catch (error) {
1541
+ await operations.claim(op.id, "COMMITTING", "UPLOADING");
1542
+ throw error;
1543
+ }
1529
1544
  await operations.update(op.id, { status: "COMPLETED", fileId: record.id });
1530
1545
  return record;
1531
1546
  }