Created
April 4, 2013 10:49
-
-
Save sandeepone/5309456 to your computer and use it in GitHub Desktop.
Upload Contorller related to media model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') or die('No direct script access.'); | |
class Controller_Upload extends Controller { | |
protected $path; | |
public function before() | |
{ | |
// Set the proper headers to allow caching | |
$this->response->headers('Pragma', 'no-cache'); | |
$this->response->headers('Cache-Control', 'no-store, no-cache, must-revalidate'); | |
if ( isset($_SERVER['HTTP_ACCEPT']) AND (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) | |
{ | |
$this->response->headers('Content-type', 'application/json'); | |
} | |
//$this->response->headers('Content-type', 'application/json'); | |
$this->response->headers('Content-Disposition: inline', 'filename="files.json"'); | |
$this->response->headers('Vary', 'Accept'); | |
$this->response->headers('X-Content-Type-Options', 'nosniff'); | |
$this->response->headers('Access-Control-Allow-Methods', 'OPTIONS, HEAD, GET, POST, PUT, DELETE'); | |
$this->response->headers('Access-Control-Allow-Headers', 'X-File-Name, X-File-Type, X-File-Size'); | |
$this->response->headers('Access-Control-Allow-Origin', '*'); | |
// Ajax request only! | |
if ( !$this->request->is_ajax() AND ! isset($_POST['script']) ) | |
{ | |
throw new HTTP_Exception_404('Accessing an ajax request <small>:type</small> externally', | |
array( ':type' => $this->request->uri() )); | |
} | |
$this->_upload_request($this->request->method()); | |
//ACL::Required('view uploaded files'); | |
parent::before(); | |
} | |
public function action_list() | |
{ | |
$mid = (int) $this->request->param('id', 0); | |
$model = $this->request->param('model', 'post'); | |
$files = Model::factory('Media')->find_all($mid, $model); | |
$this->response->body( JSON::encode($files) ); | |
} | |
public function action_post() | |
{ | |
$mid = (int) $this->request->param('id', 0); | |
$model = $this->request->param('model', 'post'); | |
$files = Model::factory('Media')->set('model', $model)->set('mid', $mid)->save( $_FILES ); | |
$this->response->body( JSON::encode($files) ); | |
} | |
public function action_delete() | |
{ | |
$id = (int) $this->request->param('id', 0); | |
$files = Model::factory('Media')->delete( $id ); | |
$this->response->body( JSON::encode($files) ); | |
} | |
private function _upload_request($method) | |
{ | |
switch ($method) | |
{ | |
case 'OPTIONS': | |
break; | |
case 'HEAD': | |
case 'GET': | |
$this->request->action('list'); | |
break; | |
case 'POST': | |
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') | |
{ | |
$this->request->action('delete'); | |
} | |
else | |
{ | |
$this->request->action('post'); | |
} | |
break; | |
case 'DELETE': | |
$this->request->action('delete'); | |
break; | |
default: | |
throw new HTTP_Exception_405('HTTP/1.1 405 Method Not Allowed'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment