Skip to content

Commit 2f18e2a

Browse files
committed
fix mimeType error
1 parent 88af5cc commit 2f18e2a

10 files changed

+63
-30
lines changed

src/Api.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ protected function storagePath()
183183
* @param string $uuid
184184
* @return \Recca0120\Upload\ChunkFile
185185
*/
186-
protected function createChunkFile($name, $uuid = null)
186+
protected function createChunkFile($name, $uuid = null, $mimeType = null)
187187
{
188188
return $this->chunkFileFactory->create(
189-
$name, $this->chunkPath(), $this->storagePath(), $uuid
189+
$name, $this->chunkPath(), $this->storagePath(), $uuid, $mimeType
190190
);
191191
}
192192
}

src/ChunkFile.php

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

33
namespace Recca0120\Upload;
44

5+
use ErrorException;
56
use Recca0120\Upload\Exceptions\ChunkedResponseException;
67

78
class ChunkFile
@@ -68,17 +69,18 @@ class ChunkFile
6869
* @param string $name
6970
* @param string $chunkPath
7071
* @param string $storagePath
72+
* @param string $mimeType
7173
* @param string $token
7274
* @param \Recca0120\Upload\Filesystem $files
7375
*/
74-
public function __construct($name, $chunkPath, $storagePath, $token = null, Filesystem $files = null)
76+
public function __construct($name, $chunkPath, $storagePath, $token = null, $mimeType = null, Filesystem $files = null)
7577
{
7678
$this->files = $files ?: new Filesystem();
7779
$this->name = $name;
7880
$this->chunkPath = $chunkPath;
7981
$this->storagePath = $storagePath;
8082
$this->token = $token;
81-
$this->mimeType = $this->files->mimeType($this->name);
83+
$this->mimeType = $mimeType;
8284
}
8385

8486
/**
@@ -88,7 +90,11 @@ public function __construct($name, $chunkPath, $storagePath, $token = null, File
8890
*/
8991
public function getMimeType()
9092
{
91-
return $this->mimeType;
93+
try {
94+
return $this->mimeType ?: $this->files->mimeType($this->name);
95+
} catch (ErrorException $e) {
96+
return;
97+
}
9298
}
9399

94100
/**
@@ -157,7 +163,7 @@ public function createUploadedFile($chunks = null, $storageFile = null)
157163
}
158164

159165
return $this->files->createUploadedFile(
160-
$storageFile, $this->name, $this->mimeType
166+
$storageFile, $this->name, $this->getMimeType()
161167
);
162168
}
163169

src/ChunkFileFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public function __construct(Filesystem $files = null)
1919
*
2020
* @return \Recca0120\Upload\ChunkFile
2121
*/
22-
public function create($name, $chunksPath, $storagePath, $token = null)
22+
public function create($name, $chunksPath, $storagePath, $token = null, $mimeType = null)
2323
{
24-
return new ChunkFile($name, $chunksPath, $storagePath, $token, $this->files);
24+
return new ChunkFile($name, $chunksPath, $storagePath, $token, $mimeType, $this->files);
2525
}
2626
}

src/FileAPI.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ public function receive($name)
2121

2222
list($start, $end, $total) = $this->parseContentRange();
2323
$originalName = $this->getOriginalName($contentDisposition);
24+
$mimeType = $this->getMimeType($originalName);
2425
$uuid = $this->request->get('token');
2526
$completed = $end >= $total - 1;
2627

27-
$chunkFile = $this->createChunkFile($originalName, $uuid);
28+
$chunkFile = $this->createChunkFile($originalName, $mimeType, $uuid);
2829
$chunkFile->appendStream('php://input', $start);
2930

3031
return $completed === true
@@ -57,6 +58,22 @@ protected function getOriginalName($contentDisposition)
5758
return preg_replace('/[\'"]/', '', $originalName);
5859
}
5960

61+
/**
62+
* getMimeType.
63+
*
64+
* @param string $originalName
65+
* @return string
66+
*/
67+
protected function getMimeType($originalName)
68+
{
69+
$mimeType = (string) $this->request->header('content-type');
70+
if (empty($mimeType) === true) {
71+
$mimeType = $this->files->mimeType($originalName);
72+
}
73+
74+
return $mimeType;
75+
}
76+
6077
/**
6178
* parseContentRange.
6279
*

tests/ChunkFileFactoryTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ public function testCreate()
2020
$files = m::mock('Recca0120\Upload\Filesystem')
2121
);
2222

23-
$files->shouldReceive('mimeType')->once()->andReturn('text/plain');
24-
25-
$this->assertInstanceOf('Recca0120\Upload\ChunkFile', $chunkFileFactory->create('foo.php', 'foo.chunksPath', 'foo.storagePath', 'foo.token'));
23+
$chunkFile = $chunkFileFactory->create(
24+
$name = 'foo.php',
25+
$cunksPath = 'foo.chunksPath',
26+
$storagePath = 'foo.storagePath',
27+
$token = 'foo.token'.
28+
$mimeType = 'foo.mimeType'
29+
);
30+
$this->assertInstanceOf('Recca0120\Upload\ChunkFile', $chunkFile);
2631
}
2732
}

tests/ChunkFileTest.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ protected function tearDown()
1919
public function testAppendStream()
2020
{
2121
$files = m::mock('Recca0120\Upload\Filesystem');
22-
$files->shouldReceive('mimeType')->once()->andReturn(
23-
$mimeType = 'text/plain'
24-
);
2522

2623
$chunkFile = new ChunkFile(
2724
$name = __FILE__,
2825
$chunkPath = 'storage/chunk/',
2926
$storagePath = 'storage/',
3027
$token = uniqid(),
28+
$mimeType = 'text/plain',
3129
$files
3230
);
3331

@@ -48,15 +46,13 @@ public function testAppendStream()
4846
public function testAppendFile()
4947
{
5048
$files = m::mock('Recca0120\Upload\Filesystem');
51-
$files->shouldReceive('mimeType')->once()->andReturn(
52-
$mimeType = 'text/plain'
53-
);
5449

5550
$chunkFile = new ChunkFile(
5651
$name = __FILE__,
5752
$chunkPath = 'storage/chunk/',
5853
$storagePath = 'storage/',
5954
$token = uniqid(),
55+
$mimeType = 'text/plain',
6056
$files
6157
);
6258

@@ -86,6 +82,7 @@ public function testCreateUploadedFile()
8682
$chunkPath = 'storage/chunk/',
8783
$storagePath = 'storage/',
8884
$token = uniqid(),
85+
null,
8986
$files
9087
);
9188

@@ -108,15 +105,13 @@ public function testCreateUploadedFile()
108105
public function testThrowException()
109106
{
110107
$files = m::mock('Recca0120\Upload\Filesystem');
111-
$files->shouldReceive('mimeType')->once()->andReturn(
112-
$mimeType = 'text/plain'
113-
);
114108

115109
$chunkFile = new ChunkFile(
116110
$name = __FILE__,
117111
$chunkPath = 'storage/chunk/',
118112
$storagePath = 'storage/',
119113
$token = uniqid(),
114+
$mimeType = 'text/plain',
120115
$files
121116
);
122117

tests/DropzoneTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function testReceiveChunkedFile()
6565
$realPath = 'foo.realpath'
6666
);
6767

68-
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $uuid)->andReturn(
68+
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $uuid, null)->andReturn(
6969
$chunkFile = m::mock('Recca0120\Upload\ChunkFile')
7070
);
7171

@@ -109,7 +109,7 @@ public function testReceiveChunkedFileWithParts()
109109
$realPath = 'foo.realpath'
110110
);
111111

112-
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $uuid)->andReturn(
112+
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $uuid, null)->andReturn(
113113
$chunkFile = m::mock('Recca0120\Upload\ChunkFile')
114114
);
115115

tests/FileAPITest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ public function testReceiveChunkedFile()
5454
$request->shouldReceive('header')->once()->with('content-range')->andReturn(
5555
$contentRange = 'bytes '.$start.'-'.$end.'/'.$total
5656
);
57+
$request->shouldReceive('header')->once()->with('content-type')->andReturn(
58+
$mimeType = 'foo'
59+
);
5760

5861
$request->shouldReceive('get')->once()->with('token')->andReturn($token = 'foo');
5962

60-
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $token)->andReturn(
63+
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $mimeType, $token)->andReturn(
6164
$chunkFile = m::mock('Recca0120\Upload\ChunkFile')
6265
);
6366

@@ -87,10 +90,13 @@ public function testReceiveChunkedFileWithoutContentRange()
8790
$request->shouldReceive('header')->once()->with('content-disposition')->andReturn(
8891
'attachment; filename="'.($originalName = 'foo.php').'"'
8992
);
93+
$request->shouldReceive('header')->once()->with('content-type')->andReturn(
94+
$mimeType = 'foo'
95+
);
9096

9197
$request->shouldReceive('get')->once()->with('token')->andReturn($token = 'foo');
9298

93-
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $token)->andReturn(
99+
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $mimeType, $token)->andReturn(
94100
$chunkFile = m::mock('Recca0120\Upload\ChunkFile')
95101
);
96102

@@ -124,9 +130,13 @@ public function testReceiveChunkedFileAndThrowChunkedResponseException()
124130
$request->shouldReceive('header')->once()->with('content-disposition')->andReturn(
125131
'attachment; filename='.($originalName = 'foo.php')
126132
);
133+
$request->shouldReceive('header')->once()->with('content-type')->andReturn(
134+
$mimeType = 'foo'
135+
);
136+
127137
$request->shouldReceive('get')->once()->with('token')->andReturn($token = 'foo');
128138

129-
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $token)->andReturn(
139+
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $mimeType, $token)->andReturn(
130140
$chunkFile = m::mock('Recca0120\Upload\ChunkFile')
131141
);
132142

tests/FineUploaderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testReceiveChunkedFile()
6060
$uuid = 'foo.qquuid'
6161
);
6262

63-
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $uuid)->andReturn(
63+
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $uuid, null)->andReturn(
6464
$chunkFile = m::mock('Recca0120\Upload\ChunkFile')
6565
);
6666

@@ -103,7 +103,7 @@ public function testReceiveChunkedFileWithParts()
103103
$realPath = 'foo.realpath'
104104
);
105105

106-
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $uuid)->andReturn(
106+
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $uuid, null)->andReturn(
107107
$chunkFile = m::mock('Recca0120\Upload\ChunkFile')
108108
);
109109

tests/PluploadTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testReceiveChunkedFile()
5656

5757
$request->shouldReceive('get')->once()->with('token')->andReturn($token = 'foo');
5858

59-
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $token)->andReturn(
59+
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $token, null)->andReturn(
6060
$chunkFile = m::mock('Recca0120\Upload\ChunkFile')
6161
);
6262

@@ -92,7 +92,7 @@ public function testReceiveChunkedFileAndThrowChunkedResponseException()
9292

9393
$request->shouldReceive('get')->once()->with('token')->andReturn($token = 'foo');
9494

95-
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $token)->andReturn(
95+
$chunkFileFactory->shouldReceive('create')->once()->with($originalName, $chunksPath, $storagePath, $token, null)->andReturn(
9696
$chunkFile = m::mock('Recca0120\Upload\ChunkFile')
9797
);
9898

0 commit comments

Comments
 (0)