Skip to content

Commit

Permalink
fixed processing filenames with special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Dec 18, 2015
1 parent 0a6a407 commit dc7d3ea
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All Notable changes to `laravel-medialibrary` will be documented in this file

##3.11.2
- Fixed the processing a file name with special characters

##3.11.1
- Remove adding .gitignore files

Expand Down
30 changes: 26 additions & 4 deletions src/FileAdder/FileAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,23 @@ public function setFile($file)

if (is_string($file)) {
$this->pathToFile = $file;
$this->fileName = pathinfo($file, PATHINFO_BASENAME);
$this->setFileName(pathinfo($file, PATHINFO_BASENAME));
$this->mediaName = pathinfo($file, PATHINFO_FILENAME);

return $this;
}

if ($file instanceof UploadedFile) {
$this->pathToFile = $file->getPath().'/'.$file->getFilename();
$this->fileName = $file->getClientOriginalName();
$this->setFileName($file->getClientOriginalName());
$this->mediaName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);

return $this;
}

if ($file instanceof File) {
$this->pathToFile = $file->getPath().'/'.$file->getFilename();
$this->fileName = pathinfo($file->getFilename(), PATHINFO_BASENAME);
$this->setFileName(pathinfo($file->getFilename(), PATHINFO_BASENAME));
$this->mediaName = pathinfo($file->getFilename(), PATHINFO_FILENAME);

return $this;
Expand Down Expand Up @@ -191,7 +191,7 @@ public function usingFileName($fileName)
*/
public function setFileName($fileName)
{
$this->fileName = $fileName;
$this->fileName = $this->sanitizeFileName($fileName);

return $this;
}
Expand Down Expand Up @@ -287,6 +287,16 @@ public function toCollection($collectionName = 'default', $diskName = '')
return $this->toCollectionOnDisk($collectionName, $diskName);
}

/**
* @param string $collectionName
* @param string $diskName
*
* @return \Spatie\MediaLibrary\Media
*
* @throws \Spatie\MediaLibrary\Exceptions\FileDoesNotExist
* @throws \Spatie\MediaLibrary\Exceptions\FileTooBig
* @throws \Spatie\MediaLibrary\Exceptions\FilesystemDoesNotExist
*/
public function toCollectionOnDisk($collectionName = 'default', $diskName = '')
{
if (!is_file($this->pathToFile)) {
Expand Down Expand Up @@ -344,4 +354,16 @@ protected function determineDiskName($diskName)

return $diskName;
}

/**
* Sanitize the given file name.
*
* @param $fileName
*
* @return string
*/
protected function sanitizeFileName($fileName)
{
return str_replace(['#', '/', '\\'], '-', $fileName);
}
}
14 changes: 14 additions & 0 deletions tests/FileAdder/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ public function it_can_rename_the_file_before_it_gets_added()
$this->assertFileExists($this->getMediaDirectory($media->id.'/othertest.jpg'));
}

/**
* @test
*/
public function it_will_sanitize_the_file_name()
{
$media = $this->testModel
->addMedia($this->getTestJpg())
->usingFileName('other#test.jpg')
->toMediaLibrary();

$this->assertEquals('test', $media->name);
$this->assertFileExists($this->getMediaDirectory($media->id.'/other-test.jpg'));
}

/**
* @test
*/
Expand Down

0 comments on commit dc7d3ea

Please sign in to comment.