/** download file in GridFS **/ public function doDownloadFile() { $this->db = xn("db"); $this->collection = xn("collection"); $this->id = xn("id"); $db = $this->_mongo->selectDB($this->db); $prefix = substr($this->collection, 0, strrpos($this->collection, ".")); $file = $db->getGridFS($prefix)->findOne(array("_id" => rock_real_id($this->id))); $fileinfo = pathinfo($file->getFilename()); $extension = strtolower($fileinfo["extension"]); import("lib.mime.types", false); ob_end_clean(); if (isset($GLOBALS["mime_types"][$extension])) { header("Content-Type:" . $GLOBALS["mime_types"][$extension]); } else { header("Content-Type:text/plain"); } header("Content-Disposition: attachment; filename=" . $fileinfo["basename"]); header("Content-Length:" . $file->getSize()); echo $file->getBytes(); exit; }
/** * update value for a field */ function doUpdate() { $db = $this->_mongo->selectDB($this->db); $id = xn("id"); $newname = trim(xn("newname")); $dataType = xn("data_type"); $value = xn("value"); $boolValue = xn("bool_value"); $integerValue = xn("integer_value"); $longValue = xn("long_value"); $doubleValue = xn("double_value"); $mixedValue = xn("mixed_value"); $format = xn("format"); $this->_rememberFormat($format); if ($newname === "") { $this->_outputJson(array("code" => 300, "message" => "New field name must not be empty")); } $realValue = null; try { $realValue = $this->_convertValue($this->_mongodb, $dataType, $format, $value, $integerValue, $longValue, $doubleValue, $boolValue, $mixedValue); } catch (Exception $e) { $this->_outputJson(array("code" => 400, "message" => $e->getMessage())); } $fieldType = ""; if ($dataType == "integer") { $fieldType = "integer"; } else { if ($dataType == "long") { $fieldType = "long"; } } $ret = array(); if ($id) { $ret = $db->execute('function (collection, id, field, fieldType, value) { var col = db.getCollection(collection); var obj = { "$set": {} }; if (typeof(value) != "object") { if (fieldType == "integer") { if (typeof(NumberInt) != "undefined") { value = NumberInt(value); } } else if (fieldType=="long") { value = NumberLong(value); } } obj["$set"][field] = value; col.update({ "_id": id }, obj, false, false); }', array($this->collection, rock_real_id($id), $newname, $fieldType, $realValue)); } else { $ret = $db->execute('function (collection, field, fieldType, value) { var col = db.getCollection(collection); var obj = { "$set": {} }; if (typeof(value) != "object") { if (fieldType=="integer") { if (typeof(NumberInt) != "undefined") { value = NumberInt(value); } } else if (fieldType=="long") { value = NumberLong(value); } } obj["$set"][field] = value; col.update({}, obj, false, true); }', array($this->collection, $newname, $fieldType, $realValue)); } if ($ret["ok"]) { $this->_outputJson(array("code" => 200)); } else { $this->_outputJson(array("code" => 500, "message" => $ret["errmsg"])); } }
/** * update value for a field */ function doUpdate() { $db = $this->_mongo->selectDB($this->db); $id = xn("id"); $newname = trim(xn("newname")); $dataType = xn("data_type"); $value = xn("value"); $boolValue = xn("bool_value"); $doubleValue = xn("double_value"); $mixedValue = xn("mixed_value"); $format = xn("format"); $this->_rememberFormat($format); if ($newname === "") { $this->_outputJson(array("code" => 300, "message" => "New field name must not be empty")); } $realValue = null; try { $realValue = $this->_convertValue($this->_mongodb, $dataType, $format, $value, $doubleValue, $boolValue, $mixedValue); } catch (Exception $e) { $this->_outputJson(array("code" => 400, "message" => $e->getMessage())); } if ($id) { $ret = $db->execute('function (collection, id, field, value) { var col = db.getCollection(collection); var obj = { "$set": {} }; obj["$set"][field] = value; col.update({ "_id": id }, obj, false, false); }', array($this->collection, rock_real_id($id), $newname, $realValue)); } else { $ret = $db->execute('function (collection, field, value) { var col = db.getCollection(collection); var obj = { "$set": {} }; obj["$set"][field] = value; col.update({}, obj, false, true); }', array($this->collection, $newname, $realValue)); } $this->_outputJson(array("code" => 200)); }