Skip to content

Commit 7f73720

Browse files
committed
remove ChunkFileFactory
1 parent dd33790 commit 7f73720

14 files changed

+40
-142
lines changed

src/Api.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,10 @@ abstract class Api implements ApiContract
3636
*/
3737
protected $config;
3838

39-
/**
40-
* @var ChunkFileFactory
41-
*/
42-
protected $chunkFileFactory;
43-
44-
public function __construct($config = [], Request $request = null, Filesystem $files = null, ChunkFileFactory $chunkFileFactory = null)
39+
public function __construct($config = [], Request $request = null, Filesystem $files = null)
4540
{
4641
$this->request = $request ?: Request::capture();
4742
$this->files = $files ?: new Filesystem();
48-
$this->chunkFileFactory = $chunkFileFactory ?: new ChunkFileFactory($this->files);
4943
$this->config = array_merge([
5044
'chunks' => sys_get_temp_dir().'/chunks',
5145
'storage' => 'storage/temp',
@@ -123,8 +117,6 @@ protected function storagePath(): string
123117

124118
protected function createChunkFile(string $name, string $uuid = null, string $mimeType = null): ChunkFile
125119
{
126-
return $this->chunkFileFactory->create(
127-
$name, $this->chunkPath(), $this->storagePath(), $uuid, $mimeType
128-
);
120+
return new ChunkFile($this->files, $name, $this->chunkPath(), $this->storagePath(), $uuid, $mimeType);
129121
}
130122
}

src/ChunkFile.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Recca0120\Upload;
44

5-
use ErrorException;
65
use Illuminate\Contracts\Filesystem\FileNotFoundException;
76
use Recca0120\Upload\Exceptions\ResourceOpenException;
87

@@ -46,30 +45,21 @@ class ChunkFile
4645
protected $tmpfilename;
4746

4847
public function __construct(
48+
Filesystem $files,
4949
string $name,
5050
string $chunkPath,
5151
string $storagePath,
5252
string $token = null,
53-
string $mimeType = null,
54-
Filesystem $files = null
53+
string $mimeType = null
5554
) {
56-
$this->files = $files ?: new Filesystem();
55+
$this->files = $files;
5756
$this->name = $name;
5857
$this->chunkPath = $chunkPath;
5958
$this->storagePath = $storagePath;
6059
$this->token = $token;
6160
$this->mimeType = $mimeType;
6261
}
6362

64-
public function getMimeType(): ?string
65-
{
66-
try {
67-
return $this->mimeType ?: $this->files->mimeType($this->name);
68-
} catch (ErrorException $e) {
69-
return null;
70-
}
71-
}
72-
7363
/**
7464
* appendStream.
7565
*
@@ -124,7 +114,7 @@ public function createUploadedFile($chunks = null, $storageFile = null)
124114
return $this->files->createUploadedFile($storageFile, $this->name, $this->files->mimeType($storageFile));
125115
}
126116

127-
protected function tmpfilename(): ?string
117+
private function tmpfilename(): ?string
128118
{
129119
if (is_null($this->tmpfilename) === true) {
130120
$this->tmpfilename = $this->files->tmpfilename($this->name, $this->token);
@@ -133,12 +123,12 @@ protected function tmpfilename(): ?string
133123
return $this->tmpfilename;
134124
}
135125

136-
protected function chunkFile(): string
126+
private function chunkFile(): string
137127
{
138128
return $this->chunkPath.$this->tmpfilename().static::TMPFILE_EXTENSION;
139129
}
140130

141-
protected function storageFile(): string
131+
private function storageFile(): string
142132
{
143133
return $this->storagePath.$this->tmpfilename();
144134
}

src/ChunkFileFactory.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Dropzone.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public function completedResponse(JsonResponse $response): JsonResponse
4141
$data = $response->getData();
4242
$data->success = true;
4343
$data->uuid = $this->request->get('dzuuid');
44-
$response->setData($data);
4544

46-
return $response;
45+
return $response->setData($data);
4746
}
4847
}

src/FileAPI.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public function receive(string $name)
2020
}
2121

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

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

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

51-
return preg_replace('/[\'"]/', '', $originalName);
52-
}
53-
54-
protected function getMimeType(string $originalName): string
55-
{
56-
$mimeType = (string) $this->request->header('content-type');
57-
if (empty($mimeType) === true) {
58-
$mimeType = $this->files->mimeType($originalName);
59-
}
60-
61-
return $mimeType;
51+
return rawurldecode(preg_replace('/[\'"]/', '', $originalName));
6252
}
6353

6454
protected function parseContentRange(): array

src/Filesystem.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ public function appendStream($output, $input, int $offset = 0): void
3939
fclose($input);
4040
}
4141

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

46-
$mimeType = $mimeType ?: $this->mimeType($path);
47-
4846
return new $class($path, $originalName, $mimeType, UPLOAD_ERR_OK, true);
4947
}
5048

src/Plupload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function receive(string $name)
3232
$chunkFile->appendStream($uploadedFile->getPathname(), $start);
3333

3434
if ($completed !== true) {
35-
throw new ChunkedResponseException('', []);
35+
throw new ChunkedResponseException(['jsonrpc' => '2.0', 'result' => false]);
3636
}
3737

3838
return $chunkFile->createUploadedFile();

tests/ChunkFileFactoryTest.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

tests/ChunkFileTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,58 @@
77
use Mockery as m;
88
use PHPUnit\Framework\TestCase;
99
use Recca0120\Upload\ChunkFile;
10-
use Recca0120\Upload\Exceptions\ChunkedResponseException;
10+
use Recca0120\Upload\Exceptions\ResourceOpenException;
1111
use Recca0120\Upload\Filesystem;
1212
use Symfony\Component\HttpFoundation\File\UploadedFile;
1313

1414
class ChunkFileTest extends TestCase
1515
{
1616
use MockeryPHPUnitIntegration;
1717

18+
/**
19+
* @throws ResourceOpenException
20+
*/
1821
public function testAppendStream(): void
1922
{
20-
$this->expectException(ChunkedResponseException::class);
21-
2223
$files = m::mock(Filesystem::class);
2324

2425
$chunkFile = new ChunkFile(
26+
$files,
2527
$name = __FILE__,
2628
$chunkPath = 'storage/chunk/',
2729
'storage/',
2830
$token = uniqid('', true),
29-
'text/plain',
30-
$files
31+
'text/plain'
3132
);
3233

3334
$source = 'php://input';
3435
$offset = 0;
3536
$files->allows('tmpfilename')->once()->with($name, $token)->andReturn($tmpfilename = 'foo.php');
3637
$files->allows('appendStream')->once()->with($chunkPath.$tmpfilename.'.part', $source, $offset);
3738
$chunkFile->appendStream($source, $offset);
38-
39-
throw new ChunkedResponseException('', []);
4039
}
4140

41+
/**
42+
* @throws ResourceOpenException
43+
*/
4244
public function testAppendFile(): void
4345
{
44-
$this->expectException(ChunkedResponseException::class);
45-
4646
$files = m::mock(Filesystem::class);
4747

4848
$chunkFile = new ChunkFile(
49+
$files,
4950
$name = __FILE__,
5051
$chunkPath = 'storage/chunk/',
5152
'storage/',
5253
$token = uniqid('', true),
53-
'text/plain',
54-
$files
54+
'text/plain'
5555
);
5656

5757
$source = 'php://input';
5858
$index = 0;
5959
$files->allows('tmpfilename')->once()->with($name, $token)->andReturn($tmpfilename = 'foo.php');
6060
$files->allows('appendStream')->once()->with($chunkPath.$tmpfilename.'.part.'.$index, $source, 0);
6161
$chunkFile->appendFile($source, $index);
62-
63-
throw new ChunkedResponseException('', []);
6462
}
6563

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

7472
$chunkFile = new ChunkFile(
73+
$files,
7574
$name = __FILE__,
7675
$chunkPath = 'storage/chunk/',
7776
$storagePath = 'storage/',
7877
$token = uniqid('', true),
79-
null,
80-
$files
78+
null
8179
);
8280

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

8888
$this->assertSame($uploadedFile, $chunkFile->createUploadedFile());

tests/DropzoneTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Contracts\Filesystem\FileNotFoundException;
66
use Illuminate\Http\JsonResponse;
7-
use Recca0120\Upload\ChunkFileFactory;
87
use Recca0120\Upload\Dropzone;
98
use Recca0120\Upload\Exceptions\ChunkedResponseException;
109
use Recca0120\Upload\Exceptions\ResourceOpenException;
@@ -15,12 +14,7 @@ protected function setUp(): void
1514
{
1615
parent::setUp();
1716

18-
$this->api = new Dropzone(
19-
$this->config,
20-
$this->request,
21-
$this->files,
22-
new ChunkFileFactory($this->files)
23-
);
17+
$this->api = new Dropzone($this->config, $this->request, $this->files);
2418
}
2519

2620
/**

0 commit comments

Comments
 (0)