更新
This commit is contained in:
@@ -7,6 +7,8 @@ namespace app\adminapi\logic\tcm;
|
||||
use app\common\service\mobilerecording\MobileRecordingStorage as Storage;
|
||||
use app\common\service\mobilerecording\MobileRecordingStream;
|
||||
use app\common\service\mobilerecording\MobileRecordingResyncException;
|
||||
use app\common\service\mobilerecording\MobileRecordingLocalUnavailableException;
|
||||
use app\common\service\mobilerecording\MobileRecordingObjects as Objects;
|
||||
use DomainException;
|
||||
use think\facade\Db;
|
||||
|
||||
@@ -45,8 +47,8 @@ final class MobileRecordingMediaLogic
|
||||
self::deleteUploads($obsolete, $garbage);
|
||||
if ($existing) {
|
||||
if ($existing['state'] === 'complete') {
|
||||
try { $file = Storage::open($existing); fclose($file); }
|
||||
catch (DomainException $e) {
|
||||
try { Objects::verifyAvailable($existing); }
|
||||
catch (MobileRecordingLocalUnavailableException $e) {
|
||||
Db::name('mobile_recording_asset')->where('upload_id', $existing['upload_id'])->delete();
|
||||
$existing['state'] = 'uploading'; $existing['received_bytes'] = 0;
|
||||
Db::name('mobile_recording_upload')->where('upload_id', $existing['upload_id'])
|
||||
@@ -76,6 +78,14 @@ final class MobileRecordingMediaLogic
|
||||
if (!hash_equals($sha, hash('sha256', $bytes))) { throw new DomainException('分片摘要校验失败'); }
|
||||
return self::mutate(static function (array &$garbage) use ($uploadId, $diagnosis, $offset, $bytes, $length, $actor): array {
|
||||
$row = self::session($uploadId, $diagnosis, $actor);
|
||||
if ($row['state'] === 'complete') {
|
||||
if ($offset + $length > (int) $row['size_bytes']) { throw new DomainException('分片超出声明的文件大小'); }
|
||||
$source = Objects::source($row);
|
||||
try { $existing = is_callable($source) ? $source($offset, $length) : Storage::bytes($source, $offset, $length); }
|
||||
finally { if (is_resource($source)) { fclose($source); } }
|
||||
if (!hash_equals($bytes, $existing)) { throw new MobileRecordingResyncException('分片与已接收内容冲突,请查询上传状态'); }
|
||||
return self::dto($row);
|
||||
}
|
||||
return Storage::locked($uploadId, static function ($file) use ($row, $offset, $bytes, $length): array {
|
||||
$received = Storage::reconcile($file, (int) $row['received_bytes']);
|
||||
if ($offset + $length > (int) $row['size_bytes']) { throw new DomainException('分片超出声明的文件大小'); }
|
||||
@@ -99,14 +109,16 @@ final class MobileRecordingMediaLogic
|
||||
MobileRecordingLogic::only($p, ['upload_id', 'diagnosis_id']);
|
||||
$uploadId = self::uploadId($p['upload_id'] ?? null);
|
||||
$diagnosis = self::diagnosisId($p);
|
||||
return self::mutate(static function (array &$garbage) use ($uploadId, $diagnosis, $actor): array {
|
||||
$row = self::synchronize(self::session($uploadId, $diagnosis, $actor));
|
||||
// Persist the object location and encryption key before the first COS call.
|
||||
$prepared = self::mutate(static function (array &$garbage) use ($uploadId, $diagnosis, $actor): array {
|
||||
$row = self::session($uploadId, $diagnosis, $actor);
|
||||
if ($row['state'] === 'complete') { return self::dto($row); }
|
||||
if (Objects::ready($row)) { return ['prepared' => true]; }
|
||||
$row = self::synchronize($row);
|
||||
if ((int) $row['received_bytes'] !== (int) $row['size_bytes']) { throw new MobileRecordingResyncException('文件尚未上传完整'); }
|
||||
$error = Storage::locked($uploadId, static function ($file) use ($row): ?string {
|
||||
try { Storage::validate($file, $row); return null; }
|
||||
catch (DomainException $e) {
|
||||
// Persist a reset before returning a validation error, so same-hash retry is recoverable.
|
||||
catch (\DomainException $e) {
|
||||
if (!ftruncate($file, 0) || !fflush($file)) { throw new \RuntimeException('Upload reset failed'); }
|
||||
return $e->getMessage();
|
||||
}
|
||||
@@ -115,15 +127,61 @@ final class MobileRecordingMediaLogic
|
||||
Db::name('mobile_recording_upload')->where('upload_id', $uploadId)->update(['received_bytes' => 0, 'updated_at' => time()]);
|
||||
return ['validation_error' => $error];
|
||||
}
|
||||
Db::name('mobile_recording_upload')->where('upload_id', $uploadId)->update(['state' => 'complete', 'updated_at' => time()]);
|
||||
$asset = ['binding_id' => $row['binding_id'], 'kind' => $row['kind'], 'upload_id' => $uploadId, 'updated_at' => time()];
|
||||
$query = Db::name('mobile_recording_asset')->where('binding_id', $row['binding_id'])->where('kind', $row['kind']);
|
||||
if ((clone $query)->find()) { $query->update($asset); } else { Db::name('mobile_recording_asset')->insert($asset); }
|
||||
$oldIds = Db::name('mobile_recording_upload')->where('binding_id', $row['binding_id'])->where('kind', $row['kind'])
|
||||
->where('upload_id', '<>', $uploadId)->column('upload_id');
|
||||
self::deleteUploads($oldIds, $garbage);
|
||||
return self::dto(array_replace($row, ['state' => 'complete']));
|
||||
Objects::ensure($row);
|
||||
return ['prepared' => true];
|
||||
});
|
||||
if (!isset($prepared['prepared'])) {
|
||||
if (($prepared['state'] ?? '') === 'complete') { self::tryCleanupStaging($uploadId); }
|
||||
return $prepared;
|
||||
}
|
||||
Objects::advance($uploadId, static fn (): array => self::session($uploadId, $diagnosis, $actor));
|
||||
$result = self::mutate(static function (array &$garbage) use ($uploadId, $diagnosis, $actor): array {
|
||||
$row = self::session($uploadId, $diagnosis, $actor);
|
||||
if (!Objects::ready($row)) { throw new \RuntimeException('Private cloud confirmation pending'); }
|
||||
if ($row['state'] !== 'complete') { self::publish($row, $garbage); }
|
||||
return self::dto(array_replace($row, ['state' => 'complete', 'received_bytes' => (int) $row['size_bytes']]));
|
||||
});
|
||||
self::tryCleanupStaging($uploadId);
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function tryCleanupStaging(string $id): void
|
||||
{
|
||||
// Publication is already durable; failure must not unpublish it. The worker retries this task.
|
||||
try { Objects::cleanupStaging($id); } catch (\Throwable $ignored) {}
|
||||
}
|
||||
|
||||
private static function publish(array $row, array &$garbage): void
|
||||
{
|
||||
$uploadId = $row['upload_id'];
|
||||
Db::name('mobile_recording_upload')->where('upload_id', $uploadId)
|
||||
->update(['state' => 'complete', 'received_bytes' => $row['size_bytes'], 'updated_at' => time()]);
|
||||
$asset = ['binding_id' => $row['binding_id'], 'kind' => $row['kind'], 'upload_id' => $uploadId, 'updated_at' => time()];
|
||||
$query = Db::name('mobile_recording_asset')->where('binding_id', $row['binding_id'])->where('kind', $row['kind']);
|
||||
if ((clone $query)->find()) { $query->update($asset); } else { Db::name('mobile_recording_asset')->insert($asset); }
|
||||
$oldIds = Db::name('mobile_recording_upload')->where('binding_id', $row['binding_id'])->where('kind', $row['kind'])
|
||||
->where('upload_id', '<>', $uploadId)->column('upload_id');
|
||||
self::deleteUploads($oldIds, $garbage);
|
||||
}
|
||||
|
||||
/** Privileged maintenance only, never routed by the HTTP controller. */
|
||||
public static function migrateLocal(string $id): void
|
||||
{
|
||||
$guard = static function () use ($id): array {
|
||||
$peek = Db::name('mobile_recording_upload')->where('upload_id', $id)->find();
|
||||
$binding = $peek ? Db::name('mobile_recording_binding')->where('id', $peek['binding_id'])->lock(true)->find() : null;
|
||||
$row = Db::name('mobile_recording_upload')->where('upload_id', $id)->where('state', 'complete')->lock(true)->find();
|
||||
if (!$binding || !$row) { throw new \DomainException('Legacy media reference no longer active'); }
|
||||
return $row;
|
||||
};
|
||||
Db::transaction(static function () use ($id, $guard): void {
|
||||
$row = $guard();
|
||||
if (Objects::ready($row)) { return; }
|
||||
Storage::locked($id, static fn ($file) => Storage::validate($file, $row));
|
||||
Objects::ensure($row);
|
||||
});
|
||||
Objects::advance($id, $guard);
|
||||
Objects::cleanupStaging($id);
|
||||
}
|
||||
|
||||
public static function status(array $p, int $actor): array
|
||||
@@ -149,16 +207,17 @@ final class MobileRecordingMediaLogic
|
||||
public static function audio(array $p, int $actor, string $range = ''): MobileRecordingStream
|
||||
{
|
||||
return self::read($p, $actor, 'audio', static function (array $row) use ($range): MobileRecordingStream {
|
||||
return new MobileRecordingStream(Storage::open($row), (int) $row['size_bytes'], $row['media_type'], $range);
|
||||
return new MobileRecordingStream(Objects::source($row), (int) $row['size_bytes'], $row['media_type'], $range);
|
||||
});
|
||||
}
|
||||
|
||||
public static function transcript(array $p, int $actor): array
|
||||
{
|
||||
return self::read($p, $actor, 'transcript', static function (array $row): array {
|
||||
$file = Storage::open($row);
|
||||
try { $text = Storage::bytes($file, 0, (int) $row['size_bytes']); }
|
||||
finally { fclose($file); }
|
||||
$source = Objects::source($row);
|
||||
try { $text = is_callable($source) ? $source(0, (int) $row['size_bytes']) : Storage::bytes($source, 0, (int) $row['size_bytes']); }
|
||||
finally { if (is_resource($source)) { fclose($source); } }
|
||||
if (!hash_equals($row['sha256'], hash('sha256', $text))) { throw new \RuntimeException('Transcript integrity mismatch'); }
|
||||
return ['text' => $text, 'sha256' => $row['sha256'], 'size_bytes' => (int) $row['size_bytes']];
|
||||
});
|
||||
}
|
||||
@@ -187,6 +246,7 @@ final class MobileRecordingMediaLogic
|
||||
public static function bindingMoved(int $id, int $diagnosisId): array
|
||||
{
|
||||
$garbage = Db::name('mobile_recording_upload')->where('binding_id', $id)->where('state', 'uploading')->column('upload_id');
|
||||
Objects::queueDelete($garbage);
|
||||
if ($garbage) { Db::name('mobile_recording_upload')->whereIn('upload_id', $garbage)->delete(); }
|
||||
Db::name('mobile_recording_upload')->where('binding_id', $id)->where('state', 'complete')
|
||||
->update(['diagnosis_id' => $diagnosisId, 'updated_at' => time()]);
|
||||
@@ -197,6 +257,7 @@ final class MobileRecordingMediaLogic
|
||||
public static function bindingRemoved(int $id): array
|
||||
{
|
||||
$garbage = Db::name('mobile_recording_upload')->where('binding_id', $id)->column('upload_id');
|
||||
Objects::queueDelete($garbage);
|
||||
Db::name('mobile_recording_asset')->where('binding_id', $id)->delete();
|
||||
Db::name('mobile_recording_upload')->where('binding_id', $id)->delete();
|
||||
return $garbage;
|
||||
@@ -247,6 +308,7 @@ final class MobileRecordingMediaLogic
|
||||
|
||||
private static function synchronize(array $row): array
|
||||
{
|
||||
if ($row['state'] === 'uploading' && Objects::ready($row)) { return $row; }
|
||||
if ($row['state'] === 'uploading') {
|
||||
$received = Storage::locked($row['upload_id'], static fn ($file): int => Storage::reconcile($file, (int) $row['received_bytes']));
|
||||
if ($received !== (int) $row['received_bytes']) {
|
||||
@@ -269,6 +331,7 @@ final class MobileRecordingMediaLogic
|
||||
private static function deleteUploads(array $ids, array &$garbage): void
|
||||
{
|
||||
if (!$ids) { return; }
|
||||
Objects::queueDelete($ids);
|
||||
Db::name('mobile_recording_upload')->whereIn('upload_id', $ids)->delete();
|
||||
$garbage = array_merge($garbage, $ids);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user