Skip to content

Commit

Permalink
ENH ensure file title constructed from original filename
Browse files Browse the repository at this point in the history
  • Loading branch information
satrun77 committed Aug 21, 2024
1 parent e447930 commit 70ad6c4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))))
);
}

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('OriginalName', $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
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 70ad6c4

Please sign in to comment.