From 3ea294b64bbc46a6424895a21d49aa9ea517fd53 Mon Sep 17 00:00:00 2001 From: codex Date: Sun, 1 Nov 2026 22:55:31 +0800 Subject: [PATCH] fix: preserve ticket attachment filenames --- web/app/Services/TicketAttachmentService.php | 3 ++- web/tests/TicketReplyTransactionTest.php | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/web/app/Services/TicketAttachmentService.php b/web/app/Services/TicketAttachmentService.php index 1bb5c806..a18f2cc9 100644 --- a/web/app/Services/TicketAttachmentService.php +++ b/web/app/Services/TicketAttachmentService.php @@ -218,7 +218,8 @@ final class TicketAttachmentService if (!isset(self::MIME_EXTENSIONS[$mime]) || @getimagesize($tmp) === false) { throw new \InvalidArgumentException('仅支持 JPG、PNG、GIF 或 WebP 图片'); } - $safeName = trim(preg_replace('/[\x00-\x1F\x7F\\\/]+/u', '', $name) ?: 'image'); + $nameWithoutSeparators = str_replace(['\\', '/'], '', $name); + $safeName = trim(preg_replace('/[\x00-\x1F\x7F]+/u', '', $nameWithoutSeparators) ?: 'image'); if ($safeName === '') { $safeName = 'image'; } diff --git a/web/tests/TicketReplyTransactionTest.php b/web/tests/TicketReplyTransactionTest.php index 6b2f78a9..7129026e 100644 --- a/web/tests/TicketReplyTransactionTest.php +++ b/web/tests/TicketReplyTransactionTest.php @@ -129,6 +129,8 @@ $assert(Capsule::table('ticket')->where('rootid', 1)->count() === 1, $assert(Capsule::table('ticket_attachments')->count() === 1, 'Successful attachment row is missing.'); $attachment = Capsule::table('ticket_attachments')->first(); +$assert((string) $attachment->original_name === 'ok.png', + 'Successful attachment did not preserve its sanitized original name.'); $assert(is_file(BASE_PATH . '/storage/ticket-attachments/' . $attachment->storage_key), 'Successful attachment file is missing.');