Skip to content

Commit

Permalink
fix mimeType error
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Mar 23, 2018
1 parent 88af5cc commit 2f18e2a
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ protected function storagePath()
* @param string $uuid
* @return \Recca0120\Upload\ChunkFile
*/
protected function createChunkFile($name, $uuid = null)
protected function createChunkFile($name, $uuid = null, $mimeType = null)
{
return $this->chunkFileFactory->create(
$name, $this->chunkPath(), $this->storagePath(), $uuid
$name, $this->chunkPath(), $this->storagePath(), $uuid, $mimeType
);
}
}
14 changes: 10 additions & 4 deletions src/ChunkFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Recca0120\Upload;

use ErrorException;
use Recca0120\Upload\Exceptions\ChunkedResponseException;

class ChunkFile
Expand Down Expand Up @@ -68,17 +69,18 @@ class ChunkFile
* @param string $name
* @param string $chunkPath
* @param string $storagePath
* @param string $mimeType
* @param string $token
* @param \Recca0120\Upload\Filesystem $files
*/
public function __construct($name, $chunkPath, $storagePath, $token = null, Filesystem $files = null)
public function __construct($name, $chunkPath, $storagePath, $token = null, $mimeType = null, Filesystem $files = null)
{
$this->files = $files ?: new Filesystem();
$this->name = $name;
$this->chunkPath = $chunkPath;
$this->storagePath = $storagePath;
$this->token = $token;
$this->mimeType = $this->files->mimeType($this->name);
$this->mimeType = $mimeType;
}

/**
Expand All @@ -88,7 +90,11 @@ public function __construct($name, $chunkPath, $storagePath, $token = null, File
*/
public function getMimeType()
{
return $this->mimeType;
try {
return $this->mimeType ?: $this->files->mimeType($this->name);
} catch (ErrorException $e) {
return;
}
}

/**
Expand Down Expand Up @@ -157,7 +163,7 @@ public function createUploadedFile($chunks = null, $storageFile = null)
}

return $this->files->createUploadedFile(
$storageFile, $this->name, $this->mimeType
$storageFile, $this->name, $this->getMimeType()
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/ChunkFileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public function __construct(Filesystem $files = null)
*
* @return \Recca0120\Upload\ChunkFile
*/
public function create($name, $chunksPath, $storagePath, $token = null)
public function create($name, $chunksPath, $storagePath, $token = null, $mimeType = null)
{
return new ChunkFile($name, $chunksPath, $storagePath, $token, $this->files);
return new ChunkFile($name, $chunksPath, $storagePath, $token, $mimeType, $this->files);
}
}
19 changes: 18 additions & 1 deletion src/FileAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public function receive($name)

list($start, $end, $total) = $this->parseContentRange();
$originalName = $this->getOriginalName($contentDisposition);
$mimeType = $this->getMimeType($originalName);
$uuid = $this->request->get('token');
$completed = $end >= $total - 1;

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

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

/**
* getMimeType.
*
* @param string $originalName
* @return string
*/
protected function getMimeType($originalName)
{
$mimeType = (string) $this->request->header('content-type');
if (empty($mimeType) === true) {
$mimeType = $this->files->mimeType($originalName);
}

return $mimeType;
}

/**
* parseContentRange.
*
Expand Down
11 changes: 8 additions & 3 deletions tests/ChunkFileFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ public function testCreate()
$files = m::mock('Recca0120\Upload\Filesystem')
);

$files->shouldReceive('mimeType')->once()->andReturn('text/plain');

$this->assertInstanceOf('Recca0120\Upload\ChunkFile', $chunkFileFactory->create('foo.php', 'foo.chunksPath', 'foo.storagePath', 'foo.token'));
$chunkFile = $chunkFileFactory->create(
$name = 'foo.php',
$cunksPath = 'foo.chunksPath',
$storagePath = 'foo.storagePath',
$token = 'foo.token'.
$mimeType = 'foo.mimeType'
);
$this->assertInstanceOf('Recca0120\Upload\ChunkFile', $chunkFile);
}
}
13 changes: 4 additions & 9 deletions tests/ChunkFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ protected function tearDown()
public function testAppendStream()
{
$files = m::mock('Recca0120\Upload\Filesystem');
$files->shouldReceive('mimeType')->once()->andReturn(
$mimeType = 'text/plain'
);

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

Expand All @@ -48,15 +46,13 @@ public function testAppendStream()
public function testAppendFile()
{
$files = m::mock('Recca0120\Upload\Filesystem');
$files->shouldReceive('mimeType')->once()->andReturn(
$mimeType = 'text/plain'
);

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

Expand Down Expand Up @@ -86,6 +82,7 @@ public function testCreateUploadedFile()
$chunkPath = 'storage/chunk/',
$storagePath = 'storage/',
$token = uniqid(),
null,
$files
);

Expand All @@ -108,15 +105,13 @@ public function testCreateUploadedFile()
public function testThrowException()
{
$files = m::mock('Recca0120\Upload\Filesystem');
$files->shouldReceive('mimeType')->once()->andReturn(
$mimeType = 'text/plain'
);

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

Expand Down
4 changes: 2 additions & 2 deletions tests/DropzoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testReceiveChunkedFile()
$realPath = 'foo.realpath'
);

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

Expand Down Expand Up @@ -109,7 +109,7 @@ public function testReceiveChunkedFileWithParts()
$realPath = 'foo.realpath'
);

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

Expand Down
16 changes: 13 additions & 3 deletions tests/FileAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ public function testReceiveChunkedFile()
$request->shouldReceive('header')->once()->with('content-range')->andReturn(
$contentRange = 'bytes '.$start.'-'.$end.'/'.$total
);
$request->shouldReceive('header')->once()->with('content-type')->andReturn(
$mimeType = 'foo'
);

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

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

Expand Down Expand Up @@ -87,10 +90,13 @@ public function testReceiveChunkedFileWithoutContentRange()
$request->shouldReceive('header')->once()->with('content-disposition')->andReturn(
'attachment; filename="'.($originalName = 'foo.php').'"'
);
$request->shouldReceive('header')->once()->with('content-type')->andReturn(
$mimeType = 'foo'
);

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

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

Expand Down Expand Up @@ -124,9 +130,13 @@ public function testReceiveChunkedFileAndThrowChunkedResponseException()
$request->shouldReceive('header')->once()->with('content-disposition')->andReturn(
'attachment; filename='.($originalName = 'foo.php')
);
$request->shouldReceive('header')->once()->with('content-type')->andReturn(
$mimeType = 'foo'
);

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

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

Expand Down
4 changes: 2 additions & 2 deletions tests/FineUploaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testReceiveChunkedFile()
$uuid = 'foo.qquuid'
);

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

Expand Down Expand Up @@ -103,7 +103,7 @@ public function testReceiveChunkedFileWithParts()
$realPath = 'foo.realpath'
);

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

Expand Down
4 changes: 2 additions & 2 deletions tests/PluploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testReceiveChunkedFile()

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

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

Expand Down Expand Up @@ -92,7 +92,7 @@ public function testReceiveChunkedFileAndThrowChunkedResponseException()

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

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

Expand Down

0 comments on commit 2f18e2a

Please sign in to comment.