implement put functionality in simple server
| | |
| | | class CorsClient extends \RestService\Client { |
| | | public function sendResponse($pHttpCode = '200', $pMessage) |
| | | { |
| | | header("Access-Control-Allow-Methods: GET, PUT"); |
| | | header("Access-Control-Allow-Origin: *"); |
| | | parent::sendResponse($pHttpCode, $pMessage); |
| | | } |
| | |
| | | $ret->setFetchMode(\PDO::FETCH_ASSOC); |
| | | return $ret; |
| | | } |
| | | |
| | | public function getFile($path, $name) |
| | | { |
| | | $this->connect(); |
| | | |
| | | $stmt = $this->pdo->prepare("SELECT id FROM files WHERE path = ? AND name = ?"); |
| | | $stmt->execute(array($path, $name)); |
| | | $id = $stmt->fetchColumn(0); |
| | | return $id; |
| | | } |
| | | |
| | | public function addFile($path, $name, $size, $timestamp) |
| | | { |
| | | $this->connect(); |
| | | |
| | | $stmt = $this->pdo->prepare("INSERT INTO files (path, name, size, timestamp) VALUES(?, ?, ?, ?)"); |
| | | $stmt->execute(array($path, $name, $size, $timestamp)); |
| | | return $this->pdo->lastInsertId(); |
| | | } |
| | | |
| | | public function updateFile($path, $name, $size, $timestamp) |
| | | { |
| | | $this->connect(); |
| | | |
| | | $stmt = $this->pdo->prepare("UPDATE files SET size = ?, timestamp = ? WHERE path =? AND name = ?"); |
| | | $stmt->execute(array($size, $timestamp, $path, $name)); |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | // vim: ts=4 et |
| | |
| | | |
| | | return $ret; |
| | | } |
| | | |
| | | /** |
| | | * @param string $path |
| | | * @param integer $timestamp |
| | | */ |
| | | function putFiles($path, $timestamp) |
| | | { |
| | | $dir = dirname($path); |
| | | $name = basename($path); |
| | | |
| | | $id = $this->da->getFile($dir, $name); |
| | | if (empty($id)) { |
| | | $id = $this->da->addFile($dir, $name, 0, $timestamp); |
| | | } else { |
| | | $this->da->updateFile($dir, $name, 0, $timestamp); |
| | | } |
| | | |
| | | $localfilename = dirname($_SERVER['SCRIPT_FILENAME']) . '/files/' . $id; |
| | | |
| | | $out = fopen($localfilename, 'wb'); |
| | | $fp = fopen('php://input', 'rb'); |
| | | |
| | | while (!feof($fp)) { |
| | | $data = fread($fp, 8192); |
| | | fwrite($out, $data); |
| | | } |
| | | |
| | | fclose($fp); |
| | | fclose($out); |
| | | |
| | | return array('url' => '/files/' . $id); |
| | | } |
| | | } |
| | | |
| | | // vim: ts=4 et |