Skip to content

Commit

Permalink
MDL-23308, coursefiles plugin could return the file link instead of c…
Browse files Browse the repository at this point in the history
…opying it
  • Loading branch information
Dongsheng Cai committed Sep 3, 2010
1 parent f5e2602 commit 93beda0
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 74 deletions.
5 changes: 0 additions & 5 deletions file.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@
// security: limit access to existing course subdirectories
$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);

if ($course->legacyfiles != 2) {
// course files disabled
send_file_not_found();
}

if ($course->id != SITEID) {
require_login($course->id, true, null, false);

Expand Down
40 changes: 31 additions & 9 deletions repository/coursefiles/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,6 @@ public function get_listing($encodedpath = '') {
return $ret;
}

/**
* course files don't support to link to external links
*
* @return int
*/
public function supported_returntypes() {
return FILE_INTERNAL;
}

/**
* Copy a file to file area
*
Expand Down Expand Up @@ -178,4 +169,35 @@ public function copy_to_area($encoded, $draftitemid, $new_filepath, $new_filenam

return $info;
}

public function get_link($encoded) {
$info = array();

$browser = get_file_browser();

// the final file
$params = unserialize(base64_decode($encoded));
$contextid = clean_param($params['contextid'], PARAM_INT);
$fileitemid = clean_param($params['itemid'], PARAM_INT);
$filename = clean_param($params['filename'], PARAM_FILE);
$filepath = clean_param($params['filepath'], PARAM_PATH);;
$filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
$component = clean_param($params['component'], PARAM_ALPHAEXT);
$context = get_context_instance_by_id($contextid);

$file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
return $file_info->get_url();
}

public function get_name() {
global $COURSE;
return $COURSE->fullname;
}

public function supported_returntypes() {
return (FILE_INTERNAL | FILE_EXTERNAL);
}
public static function get_type_option_names() {
return array();
}
}
119 changes: 59 additions & 60 deletions repository/repository_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,77 +174,76 @@
// We have two special repository type need to deal with
// local and recent plugins don't added new files to moodle, just add new records to database
// so we don't check user quota and maxbytes here
if (in_array($repo->options['type'], array('local', 'recent', 'user', 'coursefiles'))) {
$fileinfo = $repo->copy_to_area($source, $itemid, $saveas_path, $saveas_filename);
$allowexternallink = (int)get_config(null, 'repositoryallowexternallinks');
if (!empty($allowexternallink)) {
$allowexternallink = true;
} else {
$allowexternallink = false;
}
// allow external links in url element all the time
$allowexternallink = ($allowexternallink || ($env == 'url'));

// Use link of the files
if ($allowexternallink and $linkexternal === 'yes' and ($repo->supported_returntypes() & FILE_EXTERNAL)) {
// use external link
$link = $repo->get_link($source);
$info = array();
$info['file'] = $fileinfo['title'];
$info['id'] = $itemid;
$info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user/draft/'.$itemid.'/'.$fileinfo['title'];
$filesize = $fileinfo['filesize'];
if (($maxbytes!==-1) && ($filesize>$maxbytes)) {
throw new file_exception('maxbytes');
}
$info['filename'] = $saveas_filename;
$info['type'] = 'link';
$info['url'] = $link;
echo json_encode($info);
die; // ends here!!
die;
} else {
$allowexternallink = (int)get_config(null, 'repositoryallowexternallinks');
if (!empty($allowexternallink)) {
$allowexternallink = true;
} else {
$allowexternallink = false;
}
// allow external links in url element all the time
$allowexternallink = ($allowexternallink || ($env == 'url'));

// Use link of the files
if ($allowexternallink and $linkexternal === 'yes' and ($repo->supported_returntypes() & FILE_EXTERNAL)) {
// use external link
$link = $repo->get_link($source);
if (in_array($repo->options['type'], array('local', 'recent', 'user', 'coursefiles'))) {
$fileinfo = $repo->copy_to_area($source, $itemid, $saveas_path, $saveas_filename);
$info = array();
$info['filename'] = $saveas_filename;
$info['type'] = 'link';
$info['url'] = $link;
$info['file'] = $fileinfo['title'];
$info['id'] = $itemid;
$info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user/draft/'.$itemid.'/'.$fileinfo['title'];
$filesize = $fileinfo['filesize'];
if (($maxbytes!==-1) && ($filesize>$maxbytes)) {
throw new file_exception('maxbytes');
}
echo json_encode($info);
die;
} else {
// Download file to moodle
$file = $repo->get_file($source, $saveas_filename);
if ($file['path'] === false) {
$err->error = get_string('cannotdownload', 'repository');
die(json_encode($err));
}
}
// Download file to moodle
$file = $repo->get_file($source, $saveas_filename);
if ($file['path'] === false) {
$err->error = get_string('cannotdownload', 'repository');
die(json_encode($err));
}

// check if exceed maxbytes
if (($maxbytes!==-1) && (filesize($file['path']) > $maxbytes)) {
throw new file_exception('maxbytes');
}
// check if exceed maxbytes
if (($maxbytes!==-1) && (filesize($file['path']) > $maxbytes)) {
throw new file_exception('maxbytes');
}

$record = new stdclass;
$record->filepath = $saveas_path;
$record->filename = $saveas_filename;
$record->component = 'user';
$record->filearea = 'draft';
$record->itemid = $itemid;
$record = new stdclass;
$record->filepath = $saveas_path;
$record->filename = $saveas_filename;
$record->component = 'user';
$record->filearea = 'draft';
$record->itemid = $itemid;

if (!empty($file['license'])) {
$record->license = $file['license'];
} else {
$record->license = $license;
}
if (!empty($file['author'])) {
$record->author = $file['author'];
} else {
$record->author = $author;
}
$record->source = !empty($file['url']) ? $file['url'] : '';
if (!empty($file['license'])) {
$record->license = $file['license'];
} else {
$record->license = $license;
}
if (!empty($file['author'])) {
$record->author = $file['author'];
} else {
$record->author = $author;
}
$record->source = !empty($file['url']) ? $file['url'] : '';

$info = repository::move_to_filepool($file['path'], $record);
if (empty($info)) {
$info['e'] = get_string('error', 'moodle');
}
echo json_encode($info);
die;
$info = repository::move_to_filepool($file['path'], $record);
if (empty($info)) {
$info['e'] = get_string('error', 'moodle');
}
echo json_encode($info);
die;
}
break;
case 'upload':
Expand Down

0 comments on commit 93beda0

Please sign in to comment.