Skip to content

Commit 65fde2b

Browse files
author
Will Banfield
committed
Straighten out abort method
1 parent 21fb2c9 commit 65fde2b

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

src/GridFS/GridFsUpload.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,8 @@ public function uploadFromStream($source)
107107
throw new UnexpectedTypeException('stream', $source);
108108
} else{
109109
$streamMetadata = stream_get_meta_data($source);
110-
} if (!is_readable($streamMetadata['uri'])) {
111-
// throw new InvalidArgumentException("stream not readable");
112-
//issue being that php's is_readable reports native streams as not readable like php://temp
113-
}
114-
while ($data = fread($source, $this->chunkSize)) {
110+
111+
while ($data = $this->readChunk($source)) {
115112
$this->insertChunk($data);
116113
}
117114
return $this->fileCollectionInsert();
@@ -198,12 +195,7 @@ private function insertChunk($data)
198195
}
199196
$toUpload = ["files_id" => $this->file['_id'], "n" => $this->chunkOffset, "data" => new \MongoDB\BSON\Binary($data, \MongoDB\BSON\Binary::TYPE_GENERIC)];
200197
hash_update($this->ctx, $data);
201-
try{
202-
$this->collectionsWrapper->chunkInsert($toUpload);
203-
} catch (\MongoDB\Exception $e){
204-
$this->abort();
205-
throw $e;
206-
}
198+
$this->collectionsWrapper->chunkInsert($toUpload);
207199
$this->length += strlen($data);
208200
$this->chunkOffset++;
209201
}
@@ -214,12 +206,7 @@ private function fileCollectionInsert()
214206
}
215207
$md5 = hash_final($this->ctx);
216208
$this->file = array_merge($this->file, ['length' => $this->length, 'md5' => $md5]);
217-
try{
218-
$this->collectionsWrapper->fileInsert($this->file);
219-
} catch (\MongoDB\Exception $e){
220-
$this->abort();
221-
throw $e;
222-
}
209+
$this->collectionsWrapper->fileInsert($this->file);
223210
return $this->file['_id'];
224211
}
225212
//from: http://stackoverflow.com/questions/3656713/how-to-get-current-time-in-milliseconds-in-php
@@ -228,4 +215,16 @@ private function millitime() {
228215
$comps = explode(' ', $microtime);
229216
return sprintf('%d%03d', $comps[1], $comps[0] * 1000);
230217
}
218+
219+
private function readChunk($source)
220+
{
221+
$data;
222+
try{
223+
$data = fread($source, $this->chunkSize);
224+
} catch(Exception $e) {
225+
$this->abort();
226+
throw $e;
227+
}
228+
return $data;
229+
}
231230
}

0 commit comments

Comments
 (0)