diff --git a/src/File.php b/src/File.php index 722b1ca0..20357c4f 100644 --- a/src/File.php +++ b/src/File.php @@ -727,12 +727,7 @@ protected function onBeforeWrite() // Update title if (!$title) { - // Generate a readable title, dashes and underscores replaced by whitespace, - // and any file extensions removed. - $this->setField( - 'Title', - str_replace(['-','_'], ' ', preg_replace('/\.[^.]+$/', '', $name ?? '') ?? '') - ); + $this->setField('Title', File::getNormalisedFileName($name)); } // Propagate changes to the AssetStore and update the DBFile field @@ -741,6 +736,14 @@ protected function onBeforeWrite() parent::onBeforeWrite(); } + /** + * Generate a readable title, dashes and underscores replaced by whitespace, and any file extensions removed. + */ + public static function getNormalisedFileName(string $name): string + { + return trim(str_replace(['-','_'], ' ', (string) preg_replace('/\.[^.]+$/', '', $name))); + } + /** * Update link tracking on delete */ diff --git a/src/Upload.php b/src/Upload.php index 78719d2c..5da1e7ba 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('Title', File::getNormalisedFileName((string) $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/FileTest.yml b/tests/php/FileTest.yml index 9584aef1..2ed58f45 100644 --- a/tests/php/FileTest.yml +++ b/tests/php/FileTest.yml @@ -170,3 +170,7 @@ SilverStripe\Assets\Image: FileFilename: FileTest.png FileHash: 55b443b60176235ef09801153cca4e6da7494a0c Name: FileTest.png + setfromname-non-english: + FileFilename: FileTest-ملف.png + FileHash: 55b443b60176235ef09801153cca4e6da7494a0c + Name: FileTest.png 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)