Skip to content

Commit

Permalink
Merge pull request #633 from satrun77/pull/non-english-filename
Browse files Browse the repository at this point in the history
ENH ensure file title constructed from original filename
  • Loading branch information
GuySartorelli authored Aug 21, 2024
2 parents 2dfcd3f + 9584c31 commit 6b6fcbc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,12 +742,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
Expand All @@ -756,6 +751,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
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions tests/php/FileTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion tests/php/UploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down

0 comments on commit 6b6fcbc

Please sign in to comment.