emscripten Humble Cloud interface
Edward Rudd
2014-08-21 57f702f69be10e90cf3bb49e25291c0ad4a03d18

implement put functionality in simple server

3 files modified
61 ■■■■■ changed files
server/src/Custom/CorsClient.php 1 ●●●● patch | view | raw | blame | history
server/src/DB/DA.php 28 ●●●●● patch | view | raw | blame | history
server/src/RestAPI/Storage.php 32 ●●●●● patch | view | raw | blame | history
server/src/Custom/CorsClient.php
@@ -5,6 +5,7 @@
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);
    }
server/src/DB/DA.php
@@ -48,6 +48,34 @@
        $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
server/src/RestAPI/Storage.php
@@ -27,6 +27,38 @@
        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