2012-07-27 13:59:52 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
class OC_FileChunking {
|
2012-07-27 14:14:18 +04:00
|
|
|
protected $info;
|
|
|
|
protected $cache;
|
|
|
|
|
2012-07-27 13:59:52 +04:00
|
|
|
static public function decodeName($name) {
|
|
|
|
preg_match('/(?P<name>.*)-chunking-(?P<transferid>\d+)-(?P<chunkcount>\d+)-(?P<index>\d+)/', $name, $matches);
|
|
|
|
return $matches;
|
|
|
|
}
|
|
|
|
|
2012-07-27 14:14:18 +04:00
|
|
|
public function __construct($info) {
|
|
|
|
$this->info = $info;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPrefix() {
|
|
|
|
$name = $this->info['name'];
|
|
|
|
$transferid = $this->info['transferid'];
|
|
|
|
$chunkcount = $this->info['chunkcount'];
|
|
|
|
|
2012-07-27 13:59:52 +04:00
|
|
|
return $name.'-chunking-'.$transferid.'-'.$chunkcount.'-';
|
|
|
|
}
|
|
|
|
|
2012-07-27 14:14:18 +04:00
|
|
|
protected function getCache() {
|
|
|
|
if (!isset($this->cache)) {
|
|
|
|
$this->cache = new OC_Cache_File();
|
|
|
|
}
|
|
|
|
return $this->cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function store($index, $data) {
|
|
|
|
$cache = $this->getCache();
|
|
|
|
$name = $this->getPrefix().$index;
|
2012-07-27 13:59:52 +04:00
|
|
|
$cache->set($name, $data);
|
|
|
|
}
|
|
|
|
|
2012-07-27 14:14:18 +04:00
|
|
|
public function isComplete() {
|
|
|
|
$prefix = $this->getPrefix();
|
2012-07-27 13:59:52 +04:00
|
|
|
$parts = 0;
|
2012-07-27 14:14:18 +04:00
|
|
|
$cache = $this->getCache();
|
|
|
|
for($i=0; $i < $this->info['chunkcount']; $i++) {
|
2012-07-27 13:59:52 +04:00
|
|
|
if ($cache->hasKey($prefix.$i)) {
|
|
|
|
$parts ++;
|
|
|
|
}
|
|
|
|
}
|
2012-07-27 14:14:18 +04:00
|
|
|
return $parts == $this->info['chunkcount'];
|
2012-07-27 13:59:52 +04:00
|
|
|
}
|
|
|
|
|
2012-07-27 14:14:18 +04:00
|
|
|
public function assemble($f) {
|
|
|
|
$cache = $this->getCache();
|
|
|
|
$prefix = $this->getPrefix();
|
|
|
|
for($i=0; $i < $this->info['chunkcount']; $i++) {
|
2012-07-27 13:59:52 +04:00
|
|
|
$chunk = $cache->get($prefix.$i);
|
|
|
|
$cache->remove($prefix.$i);
|
|
|
|
fwrite($f,$chunk);
|
|
|
|
}
|
|
|
|
fclose($f);
|
|
|
|
}
|
|
|
|
}
|