Merge pull request #262 from cogwheel0/optimize-file-id-extraction

feat(attachments): Optimize file ID extraction and image conversion
This commit is contained in:
cogwheel
2025-12-10 22:14:17 +08:00
committed by GitHub
7 changed files with 266 additions and 308 deletions

View File

@@ -30,7 +30,8 @@ import 'streaming_status_widget.dart';
// Pre-compiled regex patterns for image processing (performance optimization)
final _base64ImagePattern = RegExp(r'data:image/[^;]+;base64,[A-Za-z0-9+/]+=*');
final _fileIdPattern = RegExp(r'/api/v1/files/([^/]+)/content');
// Handle both URL formats: /api/v1/files/{id} and /api/v1/files/{id}/content
final _fileIdPattern = RegExp(r'/api/v1/files/([^/]+)(?:/content)?$');
class AssistantMessageWidget extends ConsumerStatefulWidget {
final dynamic message;
@@ -1169,10 +1170,10 @@ class _AssistantMessageWidgetState extends ConsumerState<AssistantMessageWidget>
if (fileUrl == null) return const SizedBox.shrink();
// Extract file ID from URL if it's in the format /api/v1/files/{id}/content
// Extract file ID from URL - handle both formats:
// /api/v1/files/{id} and /api/v1/files/{id}/content
String attachmentId = fileUrl;
if (fileUrl.contains('/api/v1/files/') &&
fileUrl.contains('/content')) {
if (fileUrl.contains('/api/v1/files/')) {
final fileIdMatch = _fileIdPattern.firstMatch(fileUrl);
if (fileIdMatch != null) {
attachmentId = fileIdMatch.group(1)!;

View File

@@ -14,6 +14,10 @@ import '../../../shared/services/tasks/task_queue.dart';
import '../../../shared/utils/conversation_context_menu.dart';
import '../../tools/providers/tools_providers.dart';
// Pre-compiled regex for extracting file IDs from URLs (performance optimization)
// Handles both /api/v1/files/{id} and /api/v1/files/{id}/content formats
final _fileIdPattern = RegExp(r'/api/v1/files/([^/]+)(?:/content)?$');
class UserMessageBubble extends ConsumerStatefulWidget {
final dynamic message;
final bool isUser;
@@ -377,13 +381,11 @@ class _UserMessageBubbleState extends ConsumerState<UserMessageBubble> {
if (fileUrl == null) return const SizedBox.shrink();
// Extract file ID from URL if it's in the format /api/v1/files/{id}/content
// Extract file ID from URL - handle both formats:
// /api/v1/files/{id} and /api/v1/files/{id}/content
String attachmentId = fileUrl;
if (fileUrl.contains('/api/v1/files/') &&
fileUrl.contains('/content')) {
final fileIdMatch = RegExp(
r'/api/v1/files/([^/]+)/content',
).firstMatch(fileUrl);
if (fileUrl.contains('/api/v1/files/')) {
final fileIdMatch = _fileIdPattern.firstMatch(fileUrl);
if (fileIdMatch != null) {
attachmentId = fileIdMatch.group(1)!;
}