Skip to content

Commit 0ec5878

Browse files
committed
fix chinese filename
1 parent 2f18e2a commit 0ec5878

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
processIsolation="false"
1010
stopOnError="false"
1111
stopOnFailure="false"
12-
syntaxCheck="true"
1312
verbose="true"
1413
>
1514
<testsuites>
@@ -25,7 +24,7 @@
2524
<logging>
2625
<log type="tap" target="build/report.tap"/>
2726
<log type="junit" target="build/report.junit.xml"/>
28-
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
27+
<log type="coverage-html" target="build/coverage"/>
2928
<log type="coverage-text" target="build/coverage.txt"/>
3029
<log type="coverage-clover" target="build/logs/clover.xml"/>
3130
</logging>

src/Receiver.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,14 @@ public static function factory($config = [], $class = FileAPI::class)
7777
*/
7878
protected function callback(UploadedFile $uploadedFile, $path, $domain)
7979
{
80-
$clientOriginalName = $uploadedFile->getClientOriginalName();
81-
$clientOriginalExtension = strtolower($uploadedFile->getClientOriginalExtension());
82-
$basename = pathinfo($uploadedFile->getBasename(), PATHINFO_FILENAME);
83-
$filename = md5($basename).'.'.$clientOriginalExtension;
80+
$clientPathInfo = $this->pathInfo($uploadedFile->getClientOriginalName());
81+
$basePathInfo = $this->pathInfo($uploadedFile->getBasename());
82+
$filename = md5($basePathInfo['basename']).'.'.$clientPathInfo['extension'];
8483
$mimeType = $uploadedFile->getMimeType();
8584
$size = $uploadedFile->getSize();
8685
$uploadedFile->move($path, $filename);
8786
$response = [
88-
'name' => pathinfo($clientOriginalName, PATHINFO_FILENAME).'.'.$clientOriginalExtension,
87+
'name' => $clientPathInfo['filename'].'.'.$clientPathInfo['extension'],
8988
'tmp_name' => $path.$filename,
9089
'type' => $mimeType,
9190
'size' => $size,
@@ -94,4 +93,15 @@ protected function callback(UploadedFile $uploadedFile, $path, $domain)
9493

9594
return new JsonResponse($response);
9695
}
96+
97+
private function pathInfo($path)
98+
{
99+
$parts = [];
100+
$parts['dirname'] = rtrim(substr($path, 0, strrpos($path, '/')), '/').'/';
101+
$parts['basename'] = ltrim(substr($path, strrpos($path, '/')), '/');
102+
$parts['extension'] = strtolower(substr(strrchr($path, '.'), 1));
103+
$parts['filename'] = ltrim(substr($parts['basename'], 0, strrpos($parts ['basename'], '.')), '/');
104+
105+
return $parts;
106+
}
97107
}

tests/ReceiverTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ public function testReceive()
3030
$uploadedFile->shouldReceive('getClientOriginalName')->once()->andReturn(
3131
$clientOriginalName = 'foo.PHP'
3232
);
33-
$uploadedFile->shouldReceive('getClientOriginalExtension')->once()->andReturn(
34-
$clientOriginalExtension = 'PHP'
35-
);
33+
$clientOriginalExtension = 'PHP';
3634

3735
$uploadedFile->shouldReceive('getBasename')->once()->andReturn(
3836
$basename = 'foo'

0 commit comments

Comments
 (0)