更新
This commit is contained in:
@@ -16,6 +16,9 @@ final class DifyRequiredAttachmentUpload
|
||||
{
|
||||
private const DEFAULT_MAX_BYTES = 15 * 1024 * 1024;
|
||||
private const MAX_RESPONSE_BYTES = 262144;
|
||||
private const OCR_MODEL_RESERVE_SECONDS = 8.0;
|
||||
private const OCR_PROCESS_CLEANUP_SECONDS = 0.8;
|
||||
private const OCR_MIN_SECONDS = 1.0;
|
||||
private const VIDEO_EXTENSIONS = ['mp4', 'mov', 'm4v', 'avi', 'mkv', 'webm', 'flv', 'wmv', 'mpeg', 'mpg', '3gp', 'ts'];
|
||||
|
||||
/**
|
||||
@@ -29,6 +32,8 @@ final class DifyRequiredAttachmentUpload
|
||||
* HTTP 404/410 downloads are skipped. Other source/transport errors still fail.
|
||||
* Successful files and uploaded_indices have matching order; skipped metadata
|
||||
* contains only the original one-based input index, attachment kind and reason.
|
||||
* Optional ocr_context shares ocr_remaining_seconds across batches. Its optional
|
||||
* absolute deadline can further constrain OCR, never extend the upload deadline.
|
||||
*
|
||||
* @return array{ok:bool,files?:array,uploaded_indices?:array,skipped?:array,error_code?:string,error?:string}
|
||||
*/
|
||||
@@ -87,6 +92,8 @@ final class DifyRequiredAttachmentUpload
|
||||
$prepared = [];
|
||||
$uploadedIndices = [];
|
||||
$cache = [];
|
||||
$imagePaths = [];
|
||||
$imageSources = [];
|
||||
foreach ($plan as $item) {
|
||||
$index = $item['index'];
|
||||
self::check($deadline, $shouldAbort);
|
||||
@@ -120,6 +127,7 @@ final class DifyRequiredAttachmentUpload
|
||||
}
|
||||
$cache[$cacheKey] = self::upload($endpoint, $apiKey, $user, $path, $mime,
|
||||
'attachment-' . $index . '.' . $extension, $deadline, $shouldAbort, $options['ca_file'], $multi);
|
||||
if ($item['type'] === 'image') $imagePaths[$cacheKey] = $path;
|
||||
}
|
||||
}
|
||||
if ($cache[$cacheKey] === null) {
|
||||
@@ -132,11 +140,17 @@ final class DifyRequiredAttachmentUpload
|
||||
}
|
||||
$prepared[] = ['type' => $item['type'], 'transfer_method' => 'local_file', 'upload_file_id' => $cache[$cacheKey]];
|
||||
$uploadedIndices[] = $index;
|
||||
if (isset($imagePaths[$cacheKey])) $imageSources[] = ['index' => $index, 'path' => $imagePaths[$cacheKey]];
|
||||
self::notify($options, $index, $total, 'uploaded');
|
||||
}
|
||||
self::check($deadline, $shouldAbort);
|
||||
$textCheck = ['status' => 'disabled', 'images' => []];
|
||||
if ($imageSources !== [] && !empty($options['ocr']['enabled'])) {
|
||||
$textCheck = self::optionalImageText($imageSources, $tempRoot, $deadline, $shouldAbort, $options);
|
||||
}
|
||||
self::check($deadline, $shouldAbort);
|
||||
return ['ok' => true, 'files' => $prepared, 'uploaded_indices' => $uploadedIndices,
|
||||
'skipped' => $skipped, 'attachment_transport' => 'uploaded_files'];
|
||||
'skipped' => $skipped, 'attachment_transport' => 'uploaded_files', 'image_text_check' => $textCheck];
|
||||
} catch (\Throwable $error) {
|
||||
// Never expose cURL response bodies, URLs, local paths, credentials or exception text.
|
||||
$code = $error instanceof \RuntimeException ? $error->getMessage() : 'REQUIRED_FILES_PREPARE_FAILED';
|
||||
@@ -165,6 +179,40 @@ final class DifyRequiredAttachmentUpload
|
||||
}
|
||||
}
|
||||
|
||||
/** Spend only optional OCR time; the original image/model deadline is unchanged. */
|
||||
private static function optionalImageText(array $images, string $root, float $deadline, ?callable $shouldAbort, array $options): array
|
||||
{
|
||||
$unavailable = ['status' => 'unavailable', 'images' => []];
|
||||
$settings = $options['ocr'];
|
||||
$localLimit = (float) ($settings['timeout_seconds'] ?? 10);
|
||||
$context = $options['ocr_context'] ?? null;
|
||||
$sharedLimit = is_object($context) ? (float) ($context->ocr_remaining_seconds ?? 0) : $localLimit;
|
||||
if (!is_finite($localLimit) || !is_finite($sharedLimit) || $localLimit <= 0 || $sharedLimit <= 0) return $unavailable;
|
||||
$now = microtime(true);
|
||||
$globalEnd = $deadline;
|
||||
if (is_object($context) && isset($context->deadline) && is_numeric($context->deadline)
|
||||
&& is_finite((float) $context->deadline)) $globalEnd = min($globalEnd, (float) $context->deadline);
|
||||
$sharedWork = is_object($context) ? $sharedLimit - self::OCR_PROCESS_CLEANUP_SECONDS : $sharedLimit;
|
||||
$budget = min(60.0, $localLimit, $sharedWork,
|
||||
$globalEnd - $now - self::OCR_MODEL_RESERVE_SECONDS - self::OCR_PROCESS_CLEANUP_SECONDS);
|
||||
if ($budget < self::OCR_MIN_SECONDS) return $unavailable;
|
||||
$ocrDeadline = $now + $budget;
|
||||
$started = microtime(true);
|
||||
try {
|
||||
return ClinicalImageTextService::read($images, $root, $ocrDeadline, $shouldAbort, $settings);
|
||||
} catch (\Throwable $ignored) {
|
||||
// OCR is optional; cancellation itself is rechecked by prepare before
|
||||
// it can return an upload receipt or permit the model to start.
|
||||
return $unavailable;
|
||||
} finally {
|
||||
if (is_object($context)) {
|
||||
$spent = max(0.0, microtime(true) - $started);
|
||||
try { $context->ocr_remaining_seconds = max(0.0, $sharedLimit - $spent); }
|
||||
catch (\Throwable $ignored) { /* Invalid server-owned context cannot block original images. */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function options(array $options): array
|
||||
{
|
||||
$configured = [];
|
||||
|
||||
Reference in New Issue
Block a user