Chunked upload: Refactor to static class
This commit is contained in:
parent
a7a5433108
commit
2d85ef0e04
|
@ -49,25 +49,12 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node implements Sa
|
||||||
*/
|
*/
|
||||||
public function createFile($name, $data = null) {
|
public function createFile($name, $data = null) {
|
||||||
if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
|
if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
|
||||||
$cache = new OC_Cache_File();
|
OC_FileChunking::store($name, $data);
|
||||||
$cache->set($name, $data);
|
$info = OC_FileChunking::decodeName($name);
|
||||||
preg_match('/(?P<name>.*)-chunking-(?P<transferid>\d+)-(?P<chunkcount>\d+)-(?P<index>\d+)/', $name, $matches);
|
if (OC_FileChunking::isComplete($info)) {
|
||||||
$prefix = $matches['name'].'-chunking-'.$matches['transferid'].'-'.$matches['chunkcount'].'-';
|
$newPath = $this->path . '/' . $info['name'];
|
||||||
$parts = 0;
|
|
||||||
for($i=0; $i < $matches['chunkcount']; $i++) {
|
|
||||||
if ($cache->hasKey($prefix.$i)) {
|
|
||||||
$parts ++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($parts == $matches['chunkcount']) {
|
|
||||||
$newPath = $this->path . '/' . $matches['name'];
|
|
||||||
$f = OC_Filesystem::fopen($newPath, 'w');
|
$f = OC_Filesystem::fopen($newPath, 'w');
|
||||||
for($i=0; $i < $matches['chunkcount']; $i++) {
|
OC_FileChunking::assemble($info, $f);
|
||||||
$chunk = $cache->get($prefix.$i);
|
|
||||||
$cache->remove($prefix.$i);
|
|
||||||
fwrite($f,$chunk);
|
|
||||||
}
|
|
||||||
fclose($f);
|
|
||||||
return OC_Connector_Sabre_Node::getETagPropertyForPath($newPath);
|
return OC_Connector_Sabre_Node::getETagPropertyForPath($newPath);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
<?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 {
|
||||||
|
static public function decodeName($name) {
|
||||||
|
preg_match('/(?P<name>.*)-chunking-(?P<transferid>\d+)-(?P<chunkcount>\d+)-(?P<index>\d+)/', $name, $matches);
|
||||||
|
return $matches;
|
||||||
|
}
|
||||||
|
|
||||||
|
static public function getPrefix($name, $transferid, $chunkcount) {
|
||||||
|
return $name.'-chunking-'.$transferid.'-'.$chunkcount.'-';
|
||||||
|
}
|
||||||
|
|
||||||
|
static public function store($name, $data) {
|
||||||
|
$cache = new OC_Cache_File();
|
||||||
|
$cache->set($name, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static public function isComplete($info) {
|
||||||
|
$prefix = OC_FileChunking::getPrefix($info['name'],
|
||||||
|
$info['transferid'],
|
||||||
|
$info['chunkcount']
|
||||||
|
);
|
||||||
|
$parts = 0;
|
||||||
|
$cache = new OC_Cache_File();
|
||||||
|
for($i=0; $i < $info['chunkcount']; $i++) {
|
||||||
|
if ($cache->hasKey($prefix.$i)) {
|
||||||
|
$parts ++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $parts == $info['chunkcount'];
|
||||||
|
}
|
||||||
|
|
||||||
|
static public function assemble($info, $f) {
|
||||||
|
$cache = new OC_Cache_File();
|
||||||
|
$prefix = OC_FileChunking::getPrefix($info['name'],
|
||||||
|
$info['transferid'],
|
||||||
|
$info['chunkcount']
|
||||||
|
);
|
||||||
|
for($i=0; $i < $info['chunkcount']; $i++) {
|
||||||
|
$chunk = $cache->get($prefix.$i);
|
||||||
|
$cache->remove($prefix.$i);
|
||||||
|
fwrite($f,$chunk);
|
||||||
|
}
|
||||||
|
fclose($f);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue