Example #1
0
 /**
  * Create page object in "pageUID" file
  *
  * @param Page $page Page object
  * @throws IOException If the destination folder is not writable
  * @throws PageException If the page does not exists
  * @return bool
  */
 public function create($page)
 {
     if (!isset($page->slug)) {
         throw new IOException('Page cannot be created. It must have a slug');
     }
     $fs = new Files($this->folder);
     if (!$fs->isWritable()) {
         throw new IOException('Page destination folder is not writable');
     }
     $slug = $page->slug;
     $pagePath = $slug . $this->fileExtension;
     try {
         if ($fs->exists($pagePath)) {
             return false;
         }
         // If parent folder does not exists
         if (!$fs->exists(dirname($pagePath))) {
             $fs->makeDirectory(dirname($pagePath));
         }
         $data = $this->renderer->render($page);
         if (!$fs->write($pagePath, $data)) {
             return false;
         }
     } catch (IOException $e) {
         throw new PageException('Page cannot be created');
     }
     $this->data[$slug] = $page;
     return true;
 }
Example #2
0
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 201 OK
 *     {
 *       "filename": "supercat.png",
 *       "directory": "static\/files"
 *     }
 */
$this->post('/upload', function ($req, $res) use($app, $fs) {
    $config = $app['config'];
    $files = $req->getUploadedFiles();
    try {
        if (empty($files['file'])) {
            return $this->api->json($res, ['error' => 'NoFileSent', 'message' => 'No file was sent'], 400);
        }
        if (!$fs->isWritable()) {
            return $this->api->json($res, ['error' => 'InternalError', 'message' => 'Upload folder is not writable'], 500);
        }
        $file = $files['file'];
        // Check file name length
        if (strlen($file->getClientFilename()) > 128) {
            return $this->api->json($res, ['error' => 'FileNameError', 'message' => 'Exceeded file name limit'], 400);
        }
        // Filesize check
        $maxSize = $config->get('upload.maxSize') * 1000 * 1000;
        if ($maxSize >= 0 && $file->getSize() > $maxSize) {
            return $this->api->json($res, ['error' => 'FileSizeExceeded', 'message' => 'Exceeded file size limit'], 400);
        }
        switch ($file->getError()) {
            case UPLOAD_ERR_OK:
                break;