Skip to content

Commit

Permalink
adding unit tests for file chunking
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 committed Nov 13, 2014
1 parent a663692 commit fcf6170
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
10 changes: 10 additions & 0 deletions lib/private/cache/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@

namespace OC\Cache;

use OC\Files\View;
use OCP\Files\Storage;

class File {
/**
* @var View|Storage
*/
protected $storage;

public function __construct($storage = null) {
$this->storage = $storage;
}

/**
* Returns the cache storage for the logged in user
* @return \OC\Files\View cache storage
Expand Down
3 changes: 2 additions & 1 deletion lib/private/filechunking.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ static public function decodeName($name) {
/**
* @param string[] $info
*/
public function __construct($info) {
public function __construct($info, $cache = null) {
$this->info = $info;
$this->cache = $cache;
}

public function getPrefix() {
Expand Down
12 changes: 10 additions & 2 deletions lib/private/files/cachingchunkhandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class CachingChunkHandler implements \OCP\Files\IChunkHandler {
*/
private $storage;

private $cache;

public function __construct(Storage $storage) {
$this->storage = $storage;
}
Expand All @@ -40,12 +42,14 @@ function storeChunk($fileName, $index, $numberOfChunk, $chunkSize, $data, $trans
'chunkcount' => $numberOfChunk,
''
);
$chunkHandler = new \OC_FileChunking($info);
$chunkHandler = new \OC_FileChunking($info, $this->cache);
$bytesWritten = $chunkHandler->store($index, $data);
if ($bytesWritten != $chunkSize) {
$chunkHandler->remove($index);
}
$complete = false;
$actualSize = $chunkHandler->getCurrentSize();

if ($chunkHandler->isComplete()) {
$complete = true;
$f = $this->storage->fopen("/files" . $fileName, 'w');
Expand All @@ -56,7 +60,11 @@ function storeChunk($fileName, $index, $numberOfChunk, $chunkSize, $data, $trans
return array(
'complete' => $complete,
'bytesWritten' => $bytesWritten,
'actualSize' => $chunkHandler->getCurrentSize()
'actualSize' => $actualSize
);
}

public function setFileCache($cache) {
$this->cache = $cache;
}
}
27 changes: 27 additions & 0 deletions tests/lib/files/storage/storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

namespace Test\Files\Storage;

use OC\Files\CachingChunkHandler;
use OC\Files\Storage\Temporary;

abstract class Storage extends \PHPUnit_Framework_TestCase {
/**
* @var \OC\Files\Storage\Storage instance
Expand Down Expand Up @@ -533,4 +536,28 @@ public function testInstanceOfStorage() {
$this->assertTrue($this->instance->instanceOfStorage(get_class($this->instance)));
$this->assertFalse($this->instance->instanceOfStorage('\OC'));
}

public function testChunkedUpload() {
$chunkHandler = $this->instance->getChunkHandler();
$this->assertInstanceOf('\OCP\Files\IChunkHandler', $chunkHandler);

if ($chunkHandler instanceof CachingChunkHandler) {
$chunkHandler->setFileCache(new \OC\Cache\File(new Temporary(array())));
}

$transferId = uniqid('transfer-');
$return = $chunkHandler->storeChunk('chunk-test-01.txt', 0, 2, 10, '0123456789', $transferId);
$this->assertTrue(is_array($return));
$this->assertFalse($return['complete']);
$this->assertEquals(10, $return['bytesWritten']);
$this->assertEquals(10, $return['actualSize']);

$return = $chunkHandler->storeChunk('chunk-test-01.txt', 1, 2, 10, '0123456789', $transferId);
$this->assertTrue(is_array($return));
$this->assertTrue($return['complete']);
$this->assertEquals(10, $return['bytesWritten']);
$this->assertEquals(20, $return['actualSize']);

$this->instance->file_exists('chunk-test-01.txt');
}
}

0 comments on commit fcf6170

Please sign in to comment.