Skip to content

Commit

Permalink
fix chinese filename
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Sep 25, 2018
1 parent 2f18e2a commit 0ec5878
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
3 changes: 1 addition & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
syntaxCheck="true"
verbose="true"
>
<testsuites>
Expand All @@ -25,7 +24,7 @@
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
Expand Down
20 changes: 15 additions & 5 deletions src/Receiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,14 @@ public static function factory($config = [], $class = FileAPI::class)
*/
protected function callback(UploadedFile $uploadedFile, $path, $domain)
{
$clientOriginalName = $uploadedFile->getClientOriginalName();
$clientOriginalExtension = strtolower($uploadedFile->getClientOriginalExtension());
$basename = pathinfo($uploadedFile->getBasename(), PATHINFO_FILENAME);
$filename = md5($basename).'.'.$clientOriginalExtension;
$clientPathInfo = $this->pathInfo($uploadedFile->getClientOriginalName());
$basePathInfo = $this->pathInfo($uploadedFile->getBasename());
$filename = md5($basePathInfo['basename']).'.'.$clientPathInfo['extension'];
$mimeType = $uploadedFile->getMimeType();
$size = $uploadedFile->getSize();
$uploadedFile->move($path, $filename);
$response = [
'name' => pathinfo($clientOriginalName, PATHINFO_FILENAME).'.'.$clientOriginalExtension,
'name' => $clientPathInfo['filename'].'.'.$clientPathInfo['extension'],
'tmp_name' => $path.$filename,
'type' => $mimeType,
'size' => $size,
Expand All @@ -94,4 +93,15 @@ protected function callback(UploadedFile $uploadedFile, $path, $domain)

return new JsonResponse($response);
}

private function pathInfo($path)
{
$parts = [];
$parts['dirname'] = rtrim(substr($path, 0, strrpos($path, '/')), '/').'/';
$parts['basename'] = ltrim(substr($path, strrpos($path, '/')), '/');
$parts['extension'] = strtolower(substr(strrchr($path, '.'), 1));
$parts['filename'] = ltrim(substr($parts['basename'], 0, strrpos($parts ['basename'], '.')), '/');

return $parts;
}
}
4 changes: 1 addition & 3 deletions tests/ReceiverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ public function testReceive()
$uploadedFile->shouldReceive('getClientOriginalName')->once()->andReturn(
$clientOriginalName = 'foo.PHP'
);
$uploadedFile->shouldReceive('getClientOriginalExtension')->once()->andReturn(
$clientOriginalExtension = 'PHP'
);
$clientOriginalExtension = 'PHP';

$uploadedFile->shouldReceive('getBasename')->once()->andReturn(
$basename = 'foo'
Expand Down

0 comments on commit 0ec5878

Please sign in to comment.