Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH ensure file title constructed from original filename #633

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
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
Loading