diff --git a/application/controllers/api/Resources.php b/application/controllers/api/Resources.php index b714529d..ea14936e 100644 --- a/application/controllers/api/Resources.php +++ b/application/controllers/api/Resources.php @@ -246,10 +246,10 @@ function index_post($sid=null,$resource_id=null) throw new Exception("Resource not found"); } - if($resource['filename'] && $options['filename']){ + /*if($resource['filename'] && $options['filename']){ //delete old file $this->Editor_resource_model->delete_file_by_resource($sid,$resource_id); - } + }*/ $resource_id=$this->Editor_resource_model->update($resource_id,$options); } @@ -310,4 +310,16 @@ function delete_post($sid=null,$resource_id=null) $this->set_response($error_output, REST_Controller::HTTP_BAD_REQUEST); } } + + + function download_get($sid, $resource_id) + { + try{ + $this->editor_acl->user_has_project_access($sid,$permission='view'); + $this->Editor_resource_model->download_resource($sid,$resource_id,$resource_type='documentation'); + } + catch(Exception $e){ + $this->set_response($e->getMessage(), REST_Controller::HTTP_BAD_REQUEST); + } + } } diff --git a/application/models/Editor_resource_model.php b/application/models/Editor_resource_model.php index 963a60ba..99b8a5fa 100644 --- a/application/models/Editor_resource_model.php +++ b/application/models/Editor_resource_model.php @@ -1082,4 +1082,32 @@ function unzip_file($sid,$file_name) } } + + + function download_resource($sid,$resource_id,$resource_type='documentation') + { + $resource=$this->select_single($sid,$resource_id); + + if (!$resource || !$resource['filename']){ + throw new Exception("Resource not found or filename not set"); + } + + $project_folder=$this->Editor_model->get_project_folder($sid); + $resource_file=$project_folder.'/'.$resource_type.'/'.$resource['filename']; + + if (!file_exists($resource_file)){ + throw new Exception("File not found:" . $resource_file); + } + + $ext=pathinfo($resource['filename'], PATHINFO_EXTENSION); + $basename_no_ext=pathinfo($resource['filename'], PATHINFO_FILENAME); + + //download + header('Content-Type: application/octet-stream'); + header('Content-Disposition: attachment; filename="'.$basename_no_ext.'.'.$ext.'"'); + header('Content-Length: ' . filesize($resource_file)); + readfile($resource_file); + exit; + } + } \ No newline at end of file