Skip to content

Commit

Permalink
remove ChunkFileFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Apr 26, 2023
1 parent dd33790 commit 7f73720
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 142 deletions.
12 changes: 2 additions & 10 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,10 @@ abstract class Api implements ApiContract
*/
protected $config;

/**
* @var ChunkFileFactory
*/
protected $chunkFileFactory;

public function __construct($config = [], Request $request = null, Filesystem $files = null, ChunkFileFactory $chunkFileFactory = null)
public function __construct($config = [], Request $request = null, Filesystem $files = null)
{
$this->request = $request ?: Request::capture();
$this->files = $files ?: new Filesystem();
$this->chunkFileFactory = $chunkFileFactory ?: new ChunkFileFactory($this->files);
$this->config = array_merge([
'chunks' => sys_get_temp_dir().'/chunks',
'storage' => 'storage/temp',
Expand Down Expand Up @@ -123,8 +117,6 @@ protected function storagePath(): string

protected function createChunkFile(string $name, string $uuid = null, string $mimeType = null): ChunkFile
{
return $this->chunkFileFactory->create(
$name, $this->chunkPath(), $this->storagePath(), $uuid, $mimeType
);
return new ChunkFile($this->files, $name, $this->chunkPath(), $this->storagePath(), $uuid, $mimeType);
}
}
22 changes: 6 additions & 16 deletions src/ChunkFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Recca0120\Upload;

use ErrorException;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Recca0120\Upload\Exceptions\ResourceOpenException;

Expand Down Expand Up @@ -46,30 +45,21 @@ class ChunkFile
protected $tmpfilename;

public function __construct(
Filesystem $files,
string $name,
string $chunkPath,
string $storagePath,
string $token = null,
string $mimeType = null,
Filesystem $files = null
string $mimeType = null
) {
$this->files = $files ?: new Filesystem();
$this->files = $files;
$this->name = $name;
$this->chunkPath = $chunkPath;
$this->storagePath = $storagePath;
$this->token = $token;
$this->mimeType = $mimeType;
}

public function getMimeType(): ?string
{
try {
return $this->mimeType ?: $this->files->mimeType($this->name);
} catch (ErrorException $e) {
return null;
}
}

/**
* appendStream.
*
Expand Down Expand Up @@ -124,7 +114,7 @@ public function createUploadedFile($chunks = null, $storageFile = null)
return $this->files->createUploadedFile($storageFile, $this->name, $this->files->mimeType($storageFile));
}

protected function tmpfilename(): ?string
private function tmpfilename(): ?string
{
if (is_null($this->tmpfilename) === true) {
$this->tmpfilename = $this->files->tmpfilename($this->name, $this->token);
Expand All @@ -133,12 +123,12 @@ protected function tmpfilename(): ?string
return $this->tmpfilename;
}

protected function chunkFile(): string
private function chunkFile(): string
{
return $this->chunkPath.$this->tmpfilename().static::TMPFILE_EXTENSION;
}

protected function storageFile(): string
private function storageFile(): string
{
return $this->storagePath.$this->tmpfilename();
}
Expand Down
21 changes: 0 additions & 21 deletions src/ChunkFileFactory.php

This file was deleted.

3 changes: 1 addition & 2 deletions src/Dropzone.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public function completedResponse(JsonResponse $response): JsonResponse
$data = $response->getData();
$data->success = true;
$data->uuid = $this->request->get('dzuuid');
$response->setData($data);

return $response;
return $response->setData($data);
}
}
18 changes: 4 additions & 14 deletions src/FileAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function receive(string $name)
}

[$start, $end, $total] = $this->parseContentRange();
$originalName = $this->getOriginalName($contentDisposition);
$mimeType = $this->getMimeType($originalName);
$originalName = $this->parseOriginalName($contentDisposition);
$mimeType = $this->request->header('content-type');
$uuid = $this->request->get('token');
$completed = $end >= $total - 1;

Expand All @@ -41,24 +41,14 @@ public function receive(string $name)
return $chunkFile->createUploadedFile();
}

protected function getOriginalName(string $contentDisposition): string
protected function parseOriginalName(string $contentDisposition): string
{
$originalName = (string) $this->request->get('name');
if (empty($originalName) === true) {
[$originalName] = sscanf($contentDisposition, 'attachment; filename=%s');
}

return preg_replace('/[\'"]/', '', $originalName);
}

protected function getMimeType(string $originalName): string
{
$mimeType = (string) $this->request->header('content-type');
if (empty($mimeType) === true) {
$mimeType = $this->files->mimeType($originalName);
}

return $mimeType;
return rawurldecode(preg_replace('/[\'"]/', '', $originalName));
}

protected function parseContentRange(): array
Expand Down
4 changes: 1 addition & 3 deletions src/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ public function appendStream($output, $input, int $offset = 0): void
fclose($input);
}

public function createUploadedFile(string $path, string $originalName, string $mimeType = null, int $size = null)
public function createUploadedFile(string $path, string $originalName, string $mimeType)
{
$class = class_exists(UploadedFile::class) === true ? UploadedFile::class : SymfonyUploadedFile::class;

$mimeType = $mimeType ?: $this->mimeType($path);

return new $class($path, $originalName, $mimeType, UPLOAD_ERR_OK, true);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Plupload.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function receive(string $name)
$chunkFile->appendStream($uploadedFile->getPathname(), $start);

if ($completed !== true) {
throw new ChunkedResponseException('', []);
throw new ChunkedResponseException(['jsonrpc' => '2.0', 'result' => false]);
}

return $chunkFile->createUploadedFile();
Expand Down
29 changes: 0 additions & 29 deletions tests/ChunkFileFactoryTest.php

This file was deleted.

32 changes: 16 additions & 16 deletions tests/ChunkFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,58 @@
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Recca0120\Upload\ChunkFile;
use Recca0120\Upload\Exceptions\ChunkedResponseException;
use Recca0120\Upload\Exceptions\ResourceOpenException;
use Recca0120\Upload\Filesystem;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class ChunkFileTest extends TestCase
{
use MockeryPHPUnitIntegration;

/**
* @throws ResourceOpenException
*/
public function testAppendStream(): void
{
$this->expectException(ChunkedResponseException::class);

$files = m::mock(Filesystem::class);

$chunkFile = new ChunkFile(
$files,
$name = __FILE__,
$chunkPath = 'storage/chunk/',
'storage/',
$token = uniqid('', true),
'text/plain',
$files
'text/plain'
);

$source = 'php://input';
$offset = 0;
$files->allows('tmpfilename')->once()->with($name, $token)->andReturn($tmpfilename = 'foo.php');
$files->allows('appendStream')->once()->with($chunkPath.$tmpfilename.'.part', $source, $offset);
$chunkFile->appendStream($source, $offset);

throw new ChunkedResponseException('', []);
}

/**
* @throws ResourceOpenException
*/
public function testAppendFile(): void
{
$this->expectException(ChunkedResponseException::class);

$files = m::mock(Filesystem::class);

$chunkFile = new ChunkFile(
$files,
$name = __FILE__,
$chunkPath = 'storage/chunk/',
'storage/',
$token = uniqid('', true),
'text/plain',
$files
'text/plain'
);

$source = 'php://input';
$index = 0;
$files->allows('tmpfilename')->once()->with($name, $token)->andReturn($tmpfilename = 'foo.php');
$files->allows('appendStream')->once()->with($chunkPath.$tmpfilename.'.part.'.$index, $source, 0);
$chunkFile->appendFile($source, $index);

throw new ChunkedResponseException('', []);
}

/**
Expand All @@ -72,17 +70,19 @@ public function testCreateUploadedFile(): void
$files->allows('mimeType')->once()->andReturn($mimeType = 'text/plain');

$chunkFile = new ChunkFile(
$files,
$name = __FILE__,
$chunkPath = 'storage/chunk/',
$storagePath = 'storage/',
$token = uniqid('', true),
null,
$files
null
);

$files->allows('tmpfilename')->once()->with($name, $token)->andReturn($tmpfilename = 'foo.php');
$files->allows('move')->once()->with($chunkPath.$tmpfilename.'.part', $storagePath.$tmpfilename);
$files->allows('createUploadedFile')->once()->with($storagePath.$tmpfilename, $name, $mimeType)
$files->allows('createUploadedFile')
->once()
->with($storagePath.$tmpfilename, $name, $mimeType)
->andReturn($uploadedFile = m::mock(UploadedFile::class));

$this->assertSame($uploadedFile, $chunkFile->createUploadedFile());
Expand Down
8 changes: 1 addition & 7 deletions tests/DropzoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Http\JsonResponse;
use Recca0120\Upload\ChunkFileFactory;
use Recca0120\Upload\Dropzone;
use Recca0120\Upload\Exceptions\ChunkedResponseException;
use Recca0120\Upload\Exceptions\ResourceOpenException;
Expand All @@ -15,12 +14,7 @@ protected function setUp(): void
{
parent::setUp();

$this->api = new Dropzone(
$this->config,
$this->request,
$this->files,
new ChunkFileFactory($this->files)
);
$this->api = new Dropzone($this->config, $this->request, $this->files);
}

/**
Expand Down
10 changes: 3 additions & 7 deletions tests/FileAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Http\JsonResponse;
use Recca0120\Upload\ChunkFileFactory;
use Recca0120\Upload\Exceptions\ChunkedResponseException;
use Recca0120\Upload\Exceptions\ResourceOpenException;
use Recca0120\Upload\FileAPI;
Expand All @@ -15,12 +14,7 @@ protected function setUp(): void
{
parent::setUp();

$this->api = new FileAPI(
$this->config,
$this->request,
$this->files,
new ChunkFileFactory($this->files)
);
$this->api = new FileAPI($this->config, $this->request, $this->files);
}

/**
Expand Down Expand Up @@ -49,6 +43,7 @@ public function testReceiveChunkedFile(): void
$this->request->headers->replace([
'content-disposition' => 'attachment; filename="'.($this->uploadedFile->getClientOriginalName()).'"',
'content-range' => "bytes {$start}-{$end}/${total}",
'content-type' => 'image/png',
]);

self::assertTrue($this->api->receive('foo')->isValid());
Expand All @@ -63,6 +58,7 @@ public function testReceiveChunkedFileWithoutContentRange(): void
$this->request->headers->replace([
'content-disposition' => 'attachment; filename="'.($this->uploadedFile->getClientOriginalName()).'"',
'content-length' => $this->uploadedFile->getSize(),
'content-type' => 'image/png',
]);

self::assertTrue($this->api->receive('foo')->isValid());
Expand Down
2 changes: 1 addition & 1 deletion tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testCreateUploadedFile(): void

$this->assertInstanceOf(
UploadedFile::class,
$files->createUploadedFile($file->url(), basename($file->url()))
$files->createUploadedFile($file->url(), basename($file->url()), $files->mimeType($file->url()))
);
}
}
Loading

0 comments on commit 7f73720

Please sign in to comment.