From 70ad6c4cd9b6c226443080d3c8aeeedd17b15382 Mon Sep 17 00:00:00 2001 From: Mohamed Alsharaf Date: Wed, 21 Aug 2024 13:38:55 +1200 Subject: [PATCH] ENH ensure file title constructed from original filename --- src/File.php | 4 ++-- src/Upload.php | 3 +++ tests/php/UploadTest.php | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/File.php b/src/File.php index 722b1ca0..b03d4248 100644 --- a/src/File.php +++ b/src/File.php @@ -728,10 +728,10 @@ protected function onBeforeWrite() // Update title if (!$title) { // Generate a readable title, dashes and underscores replaced by whitespace, - // and any file extensions removed. + // and any file extensions removed, from the file original name $this->setField( 'Title', - str_replace(['-','_'], ' ', preg_replace('/\.[^.]+$/', '', $name ?? '') ?? '') + trim(str_replace(['-','_'], ' ', (string) preg_replace('/\.[^.]+$/', '', (string) $this->getField('OriginalName')))) ); } diff --git a/src/Upload.php b/src/Upload.php index 78719d2c..a9e6c834 100644 --- a/src/Upload.php +++ b/src/Upload.php @@ -199,6 +199,9 @@ public function loadIntoFile($tmpFile, $file = null, $folderPath = false) } $filename = $this->resolveExistingFile($filename); + // Store teh actual file name before any transformation from getValidFilename + $this->file->setField('OriginalName', $tmpFile['name']); + // Save changes to underlying record (if it's a DataObject) $this->storeTempFile($tmpFile, $filename, $this->file); if ($this->file instanceof DataObject) { diff --git a/tests/php/UploadTest.php b/tests/php/UploadTest.php index 71c8b695..4a587ca5 100644 --- a/tests/php/UploadTest.php +++ b/tests/php/UploadTest.php @@ -61,7 +61,7 @@ protected function tearDown(): void public function testUpload() { // create tmp file - $tmpFileName = 'UploadTest-testUpload.txt'; + $tmpFileName = 'UploadTest-testUploãd.txt'; $this->tmpFilePath = TEMP_PATH . DIRECTORY_SEPARATOR . $tmpFileName; $tmpFileContent = $this->getTemporaryFileContent(); file_put_contents($this->tmpFilePath ?? '', $tmpFileContent); @@ -87,6 +87,7 @@ public function testUpload() 'Uploads/UploadTest-testUpload.txt', $file1->getFilename() ); + $this->assertSame('UploadTest testUploãd', $file1->Title); $this->assertEquals( ASSETS_PATH . '/UploadTest/.protected/Uploads/315ae4c3d4/UploadTest-testUpload.txt', TestAssetStore::getLocalPath($file1)