Skip to content

Commit

Permalink
Add endpoint to download resources
Browse files Browse the repository at this point in the history
  • Loading branch information
mah0001 committed Jun 5, 2024
1 parent f644ca5 commit c1f51d2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
16 changes: 14 additions & 2 deletions application/controllers/api/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
}
28 changes: 28 additions & 0 deletions application/models/Editor_resource_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

0 comments on commit c1f51d2

Please sign in to comment.