mirror of
https://github.com/moodle/moodle.git
synced 2025-07-27 01:10:51 +02:00
"MDL-20824, use php exception to processs the file which excess the maximum file size"
This commit is contained in:
@@ -304,6 +304,7 @@ $string['listcantmoveright'] = 'Failed to move item right, as there is no peer t
|
|||||||
$string['loginasonecourse'] = 'You cannot enter this course.<br /> You have to terminate the \"Login as\" session before entering any other course.';
|
$string['loginasonecourse'] = 'You cannot enter this course.<br /> You have to terminate the \"Login as\" session before entering any other course.';
|
||||||
$string['loginasnoenrol'] = 'You cannot use enrol or unenrol when in course \"Login as\" session';
|
$string['loginasnoenrol'] = 'You cannot use enrol or unenrol when in course \"Login as\" session';
|
||||||
$string['logfilenotavailable'] = 'Logs not available';
|
$string['logfilenotavailable'] = 'Logs not available';
|
||||||
|
$string['maxbytes'] = 'This file is bigger than the maximum size';
|
||||||
$string['messagingdisable'] = 'Messaging is disabled on this site';
|
$string['messagingdisable'] = 'Messaging is disabled on this site';
|
||||||
$string['missingfield'] = 'Field \"$a\" is missing';
|
$string['missingfield'] = 'Field \"$a\" is missing';
|
||||||
$string['missingkeyinsql'] = 'ERROR: missing param \"$a\" in query';
|
$string['missingkeyinsql'] = 'ERROR: missing param \"$a\" in query';
|
||||||
|
@@ -844,62 +844,6 @@ abstract class repository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Upload file to local filesystem pool
|
|
||||||
* @param string $elname name of element
|
|
||||||
* @param string $filearea
|
|
||||||
* @param string $filepath
|
|
||||||
* @param string $filename - use specified filename, if not specified name of uploaded file used
|
|
||||||
* @param bool $override override file if exists
|
|
||||||
* @return mixed stored_file object or false if error; may throw exception if duplicate found
|
|
||||||
*/
|
|
||||||
public static function upload_to_filepool($elname, $filearea='user_draft', $filepath='/', $itemid='', $filename = '', $override = false) {
|
|
||||||
global $USER;
|
|
||||||
|
|
||||||
if ($filepath !== '/') {
|
|
||||||
$filepath = trim($filepath, '/');
|
|
||||||
$filepath = '/'.$filepath.'/';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isset($_FILES[$elname])) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$filename) {
|
|
||||||
$filename = $_FILES[$elname]['name'];
|
|
||||||
}
|
|
||||||
$context = get_context_instance(CONTEXT_USER, $USER->id);
|
|
||||||
if (empty($itemid)) {
|
|
||||||
$itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
|
|
||||||
}
|
|
||||||
$fs = get_file_storage();
|
|
||||||
$browser = get_file_browser();
|
|
||||||
|
|
||||||
if ($file = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) {
|
|
||||||
if ($override) {
|
|
||||||
$file->delete();
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$file_record = new object();
|
|
||||||
$file_record->contextid = $context->id;
|
|
||||||
$file_record->filearea = $filearea;
|
|
||||||
$file_record->itemid = $itemid;
|
|
||||||
$file_record->filepath = $filepath;
|
|
||||||
$file_record->filename = $filename;
|
|
||||||
$file_record->userid = $USER->id;
|
|
||||||
|
|
||||||
$file = $fs->create_file_from_pathname($file_record, $_FILES[$elname]['tmp_name']);
|
|
||||||
$info = $browser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
|
|
||||||
return array(
|
|
||||||
'url'=>$info->get_url(),
|
|
||||||
'id'=>$itemid,
|
|
||||||
'file'=>$file->get_filename()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the user files tree in a format to be returned by the function get_listing
|
* Return the user files tree in a format to be returned by the function get_listing
|
||||||
* @global object $CFG
|
* @global object $CFG
|
||||||
|
@@ -1147,14 +1147,13 @@ repository_client.upload = function(client_id) {
|
|||||||
repository_client.upload_cb = {
|
repository_client.upload_cb = {
|
||||||
upload: function(o) {
|
upload: function(o) {
|
||||||
var ret = repository_client.parse_json(o.responseText, 'upload');
|
var ret = repository_client.parse_json(o.responseText, 'upload');
|
||||||
client_id = ret.client_id;
|
|
||||||
if(ret && ret.e) {
|
if(ret && ret.e) {
|
||||||
var panel = new YAHOO.util.Element('panel-'+client_id);
|
var panel = new YAHOO.util.Element('panel-'+ret.client_id);
|
||||||
panel.get('element').innerHTML = ret.e;
|
panel.get('element').innerHTML = ret.e;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(ret) {
|
if(ret) {
|
||||||
repository_client.end(client_id, ret);
|
repository_client.end(ret.client_id, ret);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -279,11 +279,12 @@ EOD;
|
|||||||
break;
|
break;
|
||||||
case 'upload':
|
case 'upload':
|
||||||
try {
|
try {
|
||||||
$upload = $repo->get_listing();
|
$result = $repo->upload();
|
||||||
$upload['client_id'] = $client_id;
|
$result['client_id'] = $client_id;
|
||||||
echo json_encode($upload);
|
echo json_encode($result);
|
||||||
} catch (repository_exception $e){
|
} catch (Exception $e){
|
||||||
$err->e = $e->getMessage();
|
$err->e = $e->getMessage();
|
||||||
|
$err->client_id = $client_id;
|
||||||
die(json_encode($err));
|
die(json_encode($err));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@@ -10,68 +10,126 @@ class repository_upload extends repository {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @global object $SESSION
|
|
||||||
* @global string $action
|
|
||||||
* @global object $CFG
|
|
||||||
* @param int $repositoryid
|
* @param int $repositoryid
|
||||||
* @param object $context
|
* @param object $context
|
||||||
* @param array $options
|
* @param array $options
|
||||||
*/
|
*/
|
||||||
public function __construct($repositoryid, $context = SITEID, $options = array()){
|
public function __construct($repositoryid, $context = SITEID, $options = array()){
|
||||||
global $_FILES, $SESSION, $action, $CFG;
|
|
||||||
parent::__construct($repositoryid, $context, $options);
|
parent::__construct($repositoryid, $context, $options);
|
||||||
$itemid = optional_param('itemid', '', PARAM_INT);
|
$this->itemid = optional_param('itemid', '', PARAM_INT);
|
||||||
$filepath = optional_param('savepath', '/', PARAM_PATH);
|
$this->filepath = optional_param('savepath', '/', PARAM_PATH);
|
||||||
if($action=='upload'){
|
|
||||||
$this->info = repository::upload_to_filepool('repo_upload_file', 'user_draft', $filepath, $itemid);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @global object $SESSION
|
|
||||||
* @param boolean $ajax
|
* @param boolean $ajax
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function print_login($ajax = true) {
|
public function print_login($ajax = true) {
|
||||||
global $SESSION;
|
|
||||||
return $this->get_listing();
|
return $this->get_listing();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function upload() {
|
||||||
*
|
try {
|
||||||
* @global object $CFG
|
$this->info = $this->upload_to_filepool('repo_upload_file', 'user_draft', $this->filepath, $this->itemid);
|
||||||
* @global string $action
|
} catch(Exception $e) {
|
||||||
* @param mixed $path
|
throw $e;
|
||||||
* @param string $search
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function get_listing($path='', $page='') {
|
|
||||||
global $CFG, $action;
|
|
||||||
if($action=='upload'){
|
|
||||||
return $this->info;
|
|
||||||
}else{
|
|
||||||
$ret = array();
|
|
||||||
$ret['nologin'] = true;
|
|
||||||
$ret['nosearch'] = true;
|
|
||||||
// define upload form in file picker
|
|
||||||
$ret['upload'] = array('label'=>get_string('attachment', 'repository'), 'id'=>'repo-form');
|
|
||||||
$ret['manage'] = $CFG->wwwroot .'/files/index.php'; // temporary
|
|
||||||
$ret['list'] = array();
|
|
||||||
$ret['dynload'] = false;
|
|
||||||
return $ret;
|
|
||||||
}
|
}
|
||||||
|
return $this->info;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_listing() {
|
||||||
|
$ret = array();
|
||||||
|
$ret['nologin'] = true;
|
||||||
|
$ret['nosearch'] = true;
|
||||||
|
// define upload form in file picker
|
||||||
|
$ret['upload'] = array('label'=>get_string('attachment', 'repository'), 'id'=>'repo-form');
|
||||||
|
$ret['manage'] = $CFG->wwwroot .'/files/index.php'; // temporary
|
||||||
|
$ret['list'] = array();
|
||||||
|
$ret['dynload'] = false;
|
||||||
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Define the name of this repository
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function get_name(){
|
public function get_name(){
|
||||||
return get_string('repositoryname', 'repository_upload');
|
return get_string('repositoryname', 'repository_upload');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* supported return types
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
public function supported_returntypes() {
|
public function supported_returntypes() {
|
||||||
return FILE_INTERNAL;
|
return FILE_INTERNAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upload file to local filesystem pool
|
||||||
|
* @param string $elname name of element
|
||||||
|
* @param string $filearea
|
||||||
|
* @param string $filepath
|
||||||
|
* @param string $filename - use specified filename, if not specified name of uploaded file used
|
||||||
|
* @param bool $override override file if exists
|
||||||
|
* @return mixed stored_file object or false if error; may throw exception if duplicate found
|
||||||
|
*/
|
||||||
|
public function upload_to_filepool($elname, $filearea='user_draft', $filepath='/', $itemid='', $filename = '', $override = false) {
|
||||||
|
global $USER;
|
||||||
|
|
||||||
|
if ($filepath !== '/') {
|
||||||
|
$filepath = trim($filepath, '/');
|
||||||
|
$filepath = '/'.$filepath.'/';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($_FILES[$elname])) {
|
||||||
|
throw new moodle_exception('nofile');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($_FILES[$elname]['error'])) {
|
||||||
|
throw new moodle_exception('maxbytes');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$filename) {
|
||||||
|
$filename = $_FILES[$elname]['name'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$context = get_context_instance(CONTEXT_USER, $USER->id);
|
||||||
|
if (empty($itemid)) {
|
||||||
|
$itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);
|
||||||
|
}
|
||||||
|
$fs = get_file_storage();
|
||||||
|
$browser = get_file_browser();
|
||||||
|
|
||||||
|
if ($file = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) {
|
||||||
|
if ($override) {
|
||||||
|
$file->delete();
|
||||||
|
} else {
|
||||||
|
throw new moodle_exception('fileexist');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$file_record = new object();
|
||||||
|
$file_record->contextid = $context->id;
|
||||||
|
$file_record->filearea = $filearea;
|
||||||
|
$file_record->itemid = $itemid;
|
||||||
|
$file_record->filepath = $filepath;
|
||||||
|
$file_record->filename = $filename;
|
||||||
|
$file_record->userid = $USER->id;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$file = $fs->create_file_from_pathname($file_record, $_FILES[$elname]['tmp_name']);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$e->obj = $_FILES[$elname];
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
$info = $browser->get_file_info($context, $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename());
|
||||||
|
return array(
|
||||||
|
'url'=>$info->get_url(),
|
||||||
|
'id'=>$itemid,
|
||||||
|
'file'=>$file->get_filename()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user