Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed May 2, 2023
1 parent 514294a commit 492afe7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 81 deletions.
16 changes: 5 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ Controller
```php

use Illuminate\Http\JsonResponse;
use Illuminate\Http\UploadedFile;
use Recca0120\Upload\UploadManager;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class UploadController extends Controller
{
Expand All @@ -61,19 +61,13 @@ class UploadController extends Controller
return $manager
->driver($driver)
->receive($inputName, function (UploadedFile $uploadedFile, $path, $domain, $api) {
$clientOriginalName = $uploadedFile->getClientOriginalName();
$clientOriginalExtension = strtolower($uploadedFile->getClientOriginalExtension());
$basename = pathinfo($uploadedFile->getBasename(), PATHINFO_FILENAME);
$filename = md5($basename).'.'.$clientOriginalExtension;
$mimeType = $uploadedFile->getMimeType();
$size = $uploadedFile->getSize();
$uploadedFile->move($path, $filename);
$filename = $uploadedFile->getBasename();

return new JsonResponse([
'name' => pathinfo($clientOriginalName, PATHINFO_FILENAME).'.'.$clientOriginalExtension,
'name' => $uploadedFile->getClientOriginalName(),
'tmp_name' => $path.$filename,
'type' => $mimeType,
'size' => $size,
'type' => $uploadedFile->getMimeType(),
'size' => $uploadedFile->getSize(),
'url' => $domain.$path.$filename,
]);
});
Expand Down
9 changes: 3 additions & 6 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function cleanDirectory(string $path): ApiContract
{
$time = time();
$maxFileAge = 3600;
$files = (array) $this->files->files($path);
$files = $this->files->files($path);
foreach ($files as $file) {
if ($this->files->isFile($file) === true &&
$this->files->lastModified($file) < ($time - $maxFileAge)
Expand All @@ -90,13 +90,10 @@ public function receive(string $name): UploadedFile
: $this->request->file($name);
}

public function deleteUploadedFile(UploadedFile $uploadedFile): ApiContract
public function clearTempDirectories(): ApiContract
{
$file = $uploadedFile->getPathname();
if ($this->files->isFile($file) === true) {
$this->files->delete($file);
}
$this->cleanDirectory($this->chunkPath());
$this->cleanDirectory($this->storagePath());

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function cleanDirectory(string $path): Api;
*/
public function receive(string $name): UploadedFile;

public function deleteUploadedFile(UploadedFile $uploadedFile): Api;
public function clearTempDirectories(): Api;

public function completedResponse(JsonResponse $response): JsonResponse;
}
28 changes: 6 additions & 22 deletions src/Receiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public function receive(string $inputName = 'file', callable $callback = null):
try {
$callback = $callback ?: [$this, 'callback'];
$response = $callback(
$uploadedFile = $this->api->receive($inputName),
$this->api->receive($inputName),
$this->api->path(),
$this->api->domain(),
$this->api
);

return $this->api->deleteUploadedFile($uploadedFile)->completedResponse($response);
return $this->api->clearTempDirectories()->completedResponse($response);
} catch (ChunkedResponseException $e) {
return $e->getResponse();
}
Expand All @@ -55,30 +55,14 @@ public static function factory(array $config = [], string $class = FileAPI::clas

protected function callback(UploadedFile $uploadedFile, $path, $domain): JsonResponse
{
$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);
$filename = $uploadedFile->getBasename();

return new JsonResponse([
'name' => $clientPathInfo['filename'].'.'.$clientPathInfo['extension'],
'name' => $uploadedFile->getClientOriginalName(),
'tmp_name' => $path.$filename,
'type' => $mimeType,
'size' => $size,
'type' => $uploadedFile->getMimeType(),
'size' => $uploadedFile->getSize(),
'url' => $domain.$path.$filename,
]);
}

private function pathInfo($path): array
{
$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;
}
}
19 changes: 0 additions & 19 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,6 @@ public function testCleanDirectory(): void
$this->assertSame($api, $api->cleanDirectory($path));
}

public function testDeleteUploadedFile(): void
{
$request = m::mock(Request::class);
$request->allows('root')->once()->andReturn('root');

$uploadedFile = m::mock(UploadedFile::class);
$uploadedFile->allows('getPathname')->once()->andReturn($file = __FILE__);

$files = m::mock(Filesystem::class);
$files->allows('isDirectory')->once()->andReturn(true);
$files->allows('isFile')->once()->with($file)->andReturn(true);
$files->allows('delete')->once()->with($file);
$files->allows('files')->once()->andReturn([]);

$api = new Api(['chunks' => 'foo/'], $request, $files);

$this->assertSame($api, $api->deleteUploadedFile($uploadedFile));
}

public function testCompletedResponse(): void
{
$request = m::mock(Request::class);
Expand Down
42 changes: 20 additions & 22 deletions tests/ReceiverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,31 @@ class ReceiverTest extends TestCase

public function testReceive(): void
{
$receiver = new Receiver($api = m::mock(Api::class));
$inputName = 'foo';
$api->allows('receive')->once()->with($inputName)->andReturn($uploadedFile = m::mock(UploadedFile::class));
$api->allows('domain')->once()->andReturn($domain = 'foo/');
$api->allows('path')->once()->andReturn($path = 'foo/');
$uploadedFile->allows('getClientOriginalName')->once()->andReturn($clientOriginalName = 'foo.PHP');
$clientOriginalExtension = 'PHP';
$path = 'temp';
$domain = 'https://foo.bar/';

$uploadedFile->allows('getBasename')->once()->andReturn($basename = 'foo');
$uploadedFile->allows('getMimeType')->once()->andReturn($mimeType = 'foo');
$uploadedFile->allows('getSize')->once()->andReturn($size = 1000);
$uploadedFile->allows('move')->once()
->with($path, $filename = md5($basename).'.'.strtolower($clientOriginalExtension));
$uploadedFile = UploadedFile::fake()->create('test.php');

$api->allows('deleteUploadedFile')->once()->with($uploadedFile)->andReturnSelf();
$api->allows('completedResponse')->once()
->with(m::type(JsonResponse::class))->andReturnUsing(function ($response) {
return $response;
});
$api = m::spy(Api::class);
$api->allows('path')->andReturn($path);
$api->allows('domain')->andReturn($domain);
$api->allows('receive')->with($inputName)->andReturn($uploadedFile)->once();
$api->allows('clearTempDirectories')->andReturnSelf();
$api->allows('completedResponse')->with(m::type(JsonResponse::class))->andReturnUsing(function ($response) {
return $response;
});

$receiver = new Receiver($api);

$response = $receiver->receive($inputName);

$this->assertSame([
'name' => pathinfo($clientOriginalName, PATHINFO_FILENAME).'.'.strtolower($clientOriginalExtension),
'tmp_name' => $path.$filename,
'type' => $mimeType,
'size' => $size,
'url' => $domain.$path.$filename,
'name' => $uploadedFile->getClientOriginalName(),
'tmp_name' => $path.$uploadedFile->getBasename(),
'type' => $uploadedFile->getMimeType(),
'size' => $uploadedFile->getSize(),
'url' => $domain.$path.$uploadedFile->getBasename(),
], (array) $response->getData());
}

Expand All @@ -59,7 +57,7 @@ public function testReceiveCustomCallback(): void
$api->allows('domain')->once()->andReturn($domain = 'foo/');
$api->allows('path')->once()->andReturn($path = 'foo/');
$response = m::mock(JsonResponse::class);
$api->allows('deleteUploadedFile')->once()->with($uploadedFile)->andReturnSelf();
$api->allows('clearTempDirectories')->once()->andReturnSelf();
$api->allows('completedResponse')->once()->with($response)->andReturn($response);

$callback = function () use ($response) {
Expand Down

0 comments on commit 492afe7

Please sign in to comment.