public function deliverAction($uuid) { $file = Models\File::findFirst(array('conditions' => 'uuid = :uuid: AND is_deleted = 0 AND is_ready = 1', 'bind' => array('uuid' => $uuid))); if (!$file) { $this->dispatcher->forward(array('action' => 'route404')); } else { $this->response->setExpires(new \DateTime('+1 week')); if ($file->is_private && !$this->validateOrigin($file->application->origin)) { $this->dispatcher->forward(array('action' => 'route403')); } else { if ($file->is_image) { $this->deliverImage($file); } else { $this->deliverOther($file); } } } }
public function uploadAction() { try { $addedFiles = array(); $uploads = $this->request->getUploadedFiles(); foreach ($uploads as $upload) { $file = new Models\File(); $file->mime_type = $upload->getType(); $file->name = $upload->getName(); $file->size = $upload->getSize(); $file->application_id = $this->application->id; $file->is_private = json_decode($this->request->get('private')); $success = $file->save(); if (!$success) { throw new Exception($file->getMessages()[0]); } $addedFiles[] = array('uuid' => $file->uuid, 'created_at' => $file->created_at, 'updated_at' => $file->updated_at, 'name' => $file->name, 'mime_type' => $file->mime_type, 'is_deleted' => $file->is_deleted, 'is_image' => $file->is_image, 'is_ready' => $file->is_ready, 'size' => $file->size); $destination = $file->getPath(); @mkdir(dirname(dirname($destination))); @mkdir(dirname($destination)); $success = $upload->moveTo($destination); if (!$success) { $file->delete(); throw new Exception('An error occurred'); } else { $file->is_ready = 1; $file->save(); } } $this->response->setJsonContent(array('status' => 'OK', 'result' => $addedFiles)); $this->response->send(); } catch (\Exception $exception) { $this->throwException($exception); } }