Commit ebf903d6 authored by Will Banfield's avatar Will Banfield Committed by Jeremy Mikola

Straighten out abort method

parent 9709007b
...@@ -107,11 +107,8 @@ class GridFsUpload ...@@ -107,11 +107,8 @@ class GridFsUpload
throw new UnexpectedTypeException('stream', $source); throw new UnexpectedTypeException('stream', $source);
} else{ } else{
$streamMetadata = stream_get_meta_data($source); $streamMetadata = stream_get_meta_data($source);
} if (!is_readable($streamMetadata['uri'])) {
// throw new InvalidArgumentException("stream not readable"); while ($data = $this->readChunk($source)) {
//issue being that php's is_readable reports native streams as not readable like php://temp
}
while ($data = fread($source, $this->chunkSize)) {
$this->insertChunk($data); $this->insertChunk($data);
} }
return $this->fileCollectionInsert(); return $this->fileCollectionInsert();
...@@ -198,12 +195,7 @@ class GridFsUpload ...@@ -198,12 +195,7 @@ class GridFsUpload
} }
$toUpload = ["files_id" => $this->file['_id'], "n" => $this->chunkOffset, "data" => new \MongoDB\BSON\Binary($data, \MongoDB\BSON\Binary::TYPE_GENERIC)]; $toUpload = ["files_id" => $this->file['_id'], "n" => $this->chunkOffset, "data" => new \MongoDB\BSON\Binary($data, \MongoDB\BSON\Binary::TYPE_GENERIC)];
hash_update($this->ctx, $data); hash_update($this->ctx, $data);
try{
$this->collectionsWrapper->chunkInsert($toUpload); $this->collectionsWrapper->chunkInsert($toUpload);
} catch (\MongoDB\Exception $e){
$this->abort();
throw $e;
}
$this->length += strlen($data); $this->length += strlen($data);
$this->chunkOffset++; $this->chunkOffset++;
} }
...@@ -214,12 +206,7 @@ class GridFsUpload ...@@ -214,12 +206,7 @@ class GridFsUpload
} }
$md5 = hash_final($this->ctx); $md5 = hash_final($this->ctx);
$this->file = array_merge($this->file, ['length' => $this->length, 'md5' => $md5]); $this->file = array_merge($this->file, ['length' => $this->length, 'md5' => $md5]);
try{
$this->collectionsWrapper->fileInsert($this->file); $this->collectionsWrapper->fileInsert($this->file);
} catch (\MongoDB\Exception $e){
$this->abort();
throw $e;
}
return $this->file['_id']; return $this->file['_id'];
} }
//from: http://stackoverflow.com/questions/3656713/how-to-get-current-time-in-milliseconds-in-php //from: http://stackoverflow.com/questions/3656713/how-to-get-current-time-in-milliseconds-in-php
...@@ -228,4 +215,16 @@ class GridFsUpload ...@@ -228,4 +215,16 @@ class GridFsUpload
$comps = explode(' ', $microtime); $comps = explode(' ', $microtime);
return sprintf('%d%03d', $comps[1], $comps[0] * 1000); return sprintf('%d%03d', $comps[1], $comps[0] * 1000);
} }
private function readChunk($source)
{
$data;
try{
$data = fread($source, $this->chunkSize);
} catch(Exception $e) {
$this->abort();
throw $e;
}
return $data;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment