From 41076c587f7833a5d8b1bb7aeb1a7b729963c94f Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Mon, 2 Nov 2009 06:45:12 +0000 Subject: [PATCH] "MDL-20602, add support for FILE_INTERNAL and FILE_EXTERNAL" --- repository/alfresco/repository.class.php | 3 + repository/boxnet/repository.class.php | 3 + repository/draft/repository.class.php | 5 +- repository/filesystem/repository.class.php | 3 + repository/flickr/repository.class.php | 3 + repository/flickr_public/image.php | 8 +- repository/flickr_public/repository.class.php | 11 ++- repository/googledocs/repository.class.php | 11 ++- repository/lib.php | 67 ++++++++-------- repository/local/repository.class.php | 5 +- repository/mahara/repository.class.php | 3 + repository/picasa/repository.class.php | 3 + repository/remotemoodle/repository.class.php | 3 + repository/repository.src.js | 77 +++++++++++-------- repository/{ws.php => repository_ajax.php} | 53 ++++++------- repository/s3/repository.class.php | 3 + repository/upload/repository.class.php | 3 + repository/url/repository.class.php | 3 + repository/webdav/repository.class.php | 3 + repository/wikimedia/repository.class.php | 3 + repository/youtube/repository.class.php | 9 +-- 21 files changed, 164 insertions(+), 118 deletions(-) rename repository/{ws.php => repository_ajax.php} (85%) mode change 100644 => 100755 diff --git a/repository/alfresco/repository.class.php b/repository/alfresco/repository.class.php index eb557c48d77b4..bdc09eac56cdd 100755 --- a/repository/alfresco/repository.class.php +++ b/repository/alfresco/repository.class.php @@ -201,5 +201,8 @@ public static function plugin_init() { return true; } } + public function supported_returntypes() { + return FILE_INTERNAL | FILE_EXTERNAL; + } } diff --git a/repository/boxnet/repository.class.php b/repository/boxnet/repository.class.php index 19071d88335a0..cb0975db4fcf2 100755 --- a/repository/boxnet/repository.class.php +++ b/repository/boxnet/repository.class.php @@ -242,5 +242,8 @@ public function type_config_form(&$mform) { $mform->addRule('api_key', $strrequired, 'required', null, 'client'); $mform->addElement('static', null, '', get_string('information','repository_boxnet')); } + public function supported_returntypes() { + return FILE_INTERNAL | FILE_EXTERNAL; + } } diff --git a/repository/draft/repository.class.php b/repository/draft/repository.class.php index 806f569852904..619f608baf234 100755 --- a/repository/draft/repository.class.php +++ b/repository/draft/repository.class.php @@ -82,7 +82,7 @@ public function get_listing($path = '/', $page = '') { * @param string $itemid * @return string the location of the file */ - public function get_file($fid, $title = '', $itemid = '') { + public function move_to_draft($fid, $title = '', $itemid = '') { global $USER; $ret = array(); $browser = get_file_browser(); @@ -100,5 +100,8 @@ public function get_file($fid, $title = '', $itemid = '') { public function get_name(){ return get_string('repositoryname', 'repository_draft');; } + public function supported_returntypes() { + return (FILE_INTERNAL | FILE_EXTERNAL); + } } diff --git a/repository/filesystem/repository.class.php b/repository/filesystem/repository.class.php index d728a34af44e3..f4c553c7d4ac2 100644 --- a/repository/filesystem/repository.class.php +++ b/repository/filesystem/repository.class.php @@ -142,4 +142,7 @@ public function instance_config_form(&$mform) { } $mform->addElement('static', null, '', get_string('information','repository_filesystem')); } + public function supported_returntypes() { + return FILE_INTERNAL; + } } diff --git a/repository/flickr/repository.class.php b/repository/flickr/repository.class.php index b9a05521826f4..5ab542a705814 100755 --- a/repository/flickr/repository.class.php +++ b/repository/flickr/repository.class.php @@ -286,4 +286,7 @@ public static function get_type_option_names() { public function supported_filetypes() { return array('web_image'); } + public function supported_returntypes() { + return (FILE_INTERNAL | FILE_EXTERNAL); + } } diff --git a/repository/flickr_public/image.php b/repository/flickr_public/image.php index 373b4129ce77d..70511ce269d1a 100755 --- a/repository/flickr_public/image.php +++ b/repository/flickr_public/image.php @@ -33,9 +33,6 @@ function __construct($img) { default: break; } - if (empty($this->image)) { - throw new moodle_exception('invalidimage'); - } $this->width = imagesx($this->image); $this->height = imagesy($this->image); } @@ -103,10 +100,7 @@ function resize($w, $h, $use_resize = true) { return $this; } - function saveas($imagepath='') { - if (empty($imagepath)) { - $imagepath = $this->imagepath; - } + function saveas($imagepath) { switch($this->info['mime']) { case 'image/jpeg': return imagejpeg($this->image, $imagepath); diff --git a/repository/flickr_public/repository.class.php b/repository/flickr_public/repository.class.php index 6fbfc0f3862fd..53093a669a849 100644 --- a/repository/flickr_public/repository.class.php +++ b/repository/flickr_public/repository.class.php @@ -383,9 +383,13 @@ public function get_file($photo_id, $file = '') { $c->download(array(array('url'=>$url, 'file'=>$fp))); $watermark = get_config('flickr_public', 'watermark'); - if (!empty($watermark)) { + if ($watermark === 'on') { $img = new moodle_image($path); - $img->watermark($url, array(10,10), array('ttf'=>true, 'fontsize'=>9))->saveas($path); + $pathinfo = pathinfo($path); + $newpath = $pathinfo['dirname'] . '/wm_' . $pathinfo['basename']; + $img->watermark($url, array(10,10), array('ttf'=>true, 'fontsize'=>9))->saveas($newpath); + unlink($path); + $path = $newpath; } return $path; } @@ -451,4 +455,7 @@ public static function plugin_init() { public function supported_filetypes() { return array('web_image'); } + public function supported_returntypes() { + return (FILE_INTERNAL | FILE_EXTERNAL); + } } diff --git a/repository/googledocs/repository.class.php b/repository/googledocs/repository.class.php index f854e2645d5fd..8a8694b1feb24 100644 --- a/repository/googledocs/repository.class.php +++ b/repository/googledocs/repository.class.php @@ -102,10 +102,13 @@ public function get_file($url, $file) { $gdocs->download_file($url, $fp); return $path; - } + } - public function supported_filetypes() { - return array('document'); - } + public function supported_filetypes() { + return array('document'); + } + public function supported_returntypes() { + return FILE_INTERNAL; + } } //Icon from: http://www.iconspedia.com/icon/google-2706.html diff --git a/repository/lib.php b/repository/lib.php index 2e469d448e117..84db2a17782af 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -40,6 +40,11 @@ require_once(dirname(dirname(__FILE__)) . '/config.php'); require_once($CFG->libdir . '/filelib.php'); require_once($CFG->libdir . '/formslib.php'); + +define('FILE_EXTERNAL', 1); +define('FILE_INTERNAL', 2); + + // File picker javascript code /** @@ -398,7 +403,7 @@ public function delete() { global $DB; //delete all instances of this type - $instances = repository::get_instances(array(),null,false,$this->_typename); + $instances = repository::get_instances(array(), null, false, $this->_typename); foreach ($instances as $instance) { $instance->delete(); } @@ -444,6 +449,7 @@ abstract class repository { public $context; public $options; public $readonly; + public $returntypes; /** * Return a type for a given type name. @@ -569,7 +575,7 @@ public static function get_editable_types($context = null) { * @param string $type a type name to retrieve * @return array repository instances */ - public static function get_instances($contexts=array(), $userid = null, $onlyvisible = true, $type=null, $accepted_types = '*', $returnvalue = '*') { + public static function get_instances($contexts=array(), $userid = null, $onlyvisible = true, $type=null, $accepted_types = '*', $returntypes = 3) { global $DB, $CFG, $USER; $params = array(); @@ -637,13 +643,12 @@ public static function get_instances($contexts=array(), $userid = null, $onlyvis } } } - if ($returnvalue !== '*' and $repository->supported_return_value() !== '*') { - $tmp = $repository->supported_return_value(); - if ($tmp != $returnvalue) { - if ($returnvalue == 'link' && $repository->supported_external_link()) { - } else { - $is_supported = false; - } + if ($returntypes !== 3 and $repository->supported_returntypes() !== 3) { + $type = $repository->supported_returntypes(); + if ($type & $returntypes) { + // + } else { + $is_supported = false; } } if (!$onlyvisible || ($repository->is_visible() && !$repository->disabled)) { @@ -1014,7 +1019,7 @@ public static function display_instances_list($context, $typename = null) { //instances of a type, even if this type is not visible. In course/user context we //want to display only visible instances, but for every type types. The repository::get_instances() //third parameter displays only visible type. - $instances = repository::get_instances(array($context),null,!$admin,$typename); + $instances = repository::get_instances(array($context), null, !$admin, $typename); $instancesnumber = count($instances); $alreadyplugins = array(); @@ -1111,6 +1116,7 @@ public function __construct($repositoryid, $contextid = SITEID, $options = array $this->options[$n] = $v; } $this->name = $this->get_name(); + $this->returntypes = $this->supported_returntypes(); $this->super_called = true; } @@ -1148,15 +1154,11 @@ public function prepare_file($filename) { */ public function get_file($url, $filename = '') { global $CFG; - if (!empty($CFG->repositoryuseexternallink) && $this->supported_external_link()) { - return $url; - } else { - $path = $this->prepare_file($filename); - $fp = fopen($path, 'w'); - $c = new curl; - $c->download(array(array('url'=>$url, 'file'=>$fp))); - return $path; - } + $path = $this->prepare_file($filename); + $fp = fopen($path, 'w'); + $c = new curl; + $c->download(array(array('url'=>$url, 'file'=>$fp))); + return $path; } /** @@ -1214,17 +1216,8 @@ public function supported_filetypes() { * does it return a file url or a item_id * @return string */ - public function supported_return_value() { - // return 'link'; - // return 'ref_id'; - return 'ref_id'; - } - /** - * does it return a file url or a item_id - * @return string - */ - public function supported_external_link() { - return false; + public function supported_returntypes() { + return (FILE_INTERNAL | FILE_EXTERNAL); } /** @@ -1781,10 +1774,10 @@ function repository_head_setup() { * @param object $context the context * @param string $id unique id for every file picker * @param string $accepted_filetypes - * @param string $returnvalue the return value of file picker + * @param string $returntypes the return value of file picker * @return array */ -function repository_get_client($context, $id = '', $accepted_filetypes = '*', $returnvalue = '*') { +function repository_get_client($context, $id = '', $accepted_filetypes = '*', $returntypes = 3) { global $CFG, $USER, $PAGE, $OUTPUT; $ft = new file_type_to_ext(); @@ -1856,8 +1849,8 @@ function repository_get_client($context, $id = '', $accepted_filetypes = '*', $ EOD; @@ -1872,17 +1865,17 @@ function repository_get_client($context, $id = '', $accepted_filetypes = '*', $ if (is_array($accepted_filetypes) && in_array('*', $accepted_filetypes)) { $accepted_filetypes = '*'; } - $repos = repository::get_instances(array($user_context, $context, get_system_context()), null, true, null, $accepted_filetypes, $returnvalue); + $repos = repository::get_instances(array($user_context, $context, get_system_context()), null, true, null, $accepted_filetypes, $returntypes); // print repository instances listing $js .= << -repository_listing['$id'] = []; +MOODLE.repository.listing['$id'] = []; EOD; foreach ($repos as $repo) { $meta = $repo->get_meta(); $js .= "\r\n"; - $js .= 'repository_listing[\''.$id.'\']['.$meta->id.']='.json_encode($meta).';'; + $js .= 'MOODLE.repository.listing[\''.$id.'\']['.$meta->id.']='.json_encode($meta).';'; $js .= "\n"; } $js .= "\r\n"; diff --git a/repository/local/repository.class.php b/repository/local/repository.class.php index b29ddf13688b8..866d4b83c89e6 100755 --- a/repository/local/repository.class.php +++ b/repository/local/repository.class.php @@ -128,7 +128,7 @@ public function get_listing($encodedpath = '', $page = '') { * @return array The metainfo of file * @see curl package */ - public function get_file($encoded, $title = '', $itemid = '', $save_path = '/') { + public function move_to_draft($encoded, $title = '', $itemid = '', $save_path = '/') { global $USER, $DB; $ret = array(); @@ -158,5 +158,8 @@ public function get_file($encoded, $title = '', $itemid = '', $save_path = '/') public function get_name(){ return get_string('repositoryname', 'repository_local');; } + public function supported_returntypes() { + return FILE_INTERNAL; + } } diff --git a/repository/mahara/repository.class.php b/repository/mahara/repository.class.php index 7f6f32e090cfb..a99db28c5628d 100644 --- a/repository/mahara/repository.class.php +++ b/repository/mahara/repository.class.php @@ -342,5 +342,8 @@ public static function get_instance_option_names() { ///the administrator just need to set a peer return array('peer'); } + public function supported_returntypes() { + return FILE_INTERNAL; + } } diff --git a/repository/picasa/repository.class.php b/repository/picasa/repository.class.php index f03a91d50daac..242f9f88b9f76 100644 --- a/repository/picasa/repository.class.php +++ b/repository/picasa/repository.class.php @@ -97,6 +97,9 @@ public function get_name(){ public function supported_filetypes() { return array('web_image'); } + public function supported_returntypes() { + return (FILE_INTERNAL | FILE_EXTERNAL); + } } // Icon for this plugin retrieved from http://www.iconspedia.com/icon/picasa-2711.html diff --git a/repository/remotemoodle/repository.class.php b/repository/remotemoodle/repository.class.php index 9898144d17a1a..b215e048a9fec 100644 --- a/repository/remotemoodle/repository.class.php +++ b/repository/remotemoodle/repository.class.php @@ -320,5 +320,8 @@ public static function get_instance_option_names() { ///the administrator just need to set a peer return array('peer'); } + public function supported_returntypes() { + return FILE_INTERNAL; + } } diff --git a/repository/repository.src.js b/repository/repository.src.js index b56f1539e0574..d830e21de0662 100644 --- a/repository/repository.src.js +++ b/repository/repository.src.js @@ -10,18 +10,32 @@ * repo = new repository_client(); */ -var id2clientid = {}; -var id2itemid = {}; +if (!MOODLE) { + var MOODLE = {}; +} +if (!MOODLE.repository) { + MOODLE.repository = {}; +} + +MOODLE.repository.listing = {}; +MOODLE.repository.extensions = {}; +MOODLE.repository.cache = {}; +MOODLE.repository.cache.client_id = {}; -var repository_listing = {}; -var cached_client_id = {}; -var file_extensions = {}; /* when selected a file, filename will be cached in this varible */ var new_filename = ''; // will be used by login form var cached_id; var cached_repo_id; -// repository_client has static functions +var id2clientid = {}; +var id2itemid = {}; + +/** + * repository_client is a javascript class, it contains several static + * methods you can call it directly without creating an instance. + * If you are going to create a file picker, you need create an instance + * repo = new repository_client(); + */ var repository_client = (function(){ // private static field var version = '2.0'; @@ -51,6 +65,9 @@ var repository_client = (function(){ el.className = 'file-picker'; this.client_id = client_id; document.body.appendChild(el); + + MOODLE.repository.api = moodle_cfg.wwwroot+'/repository/repository_ajax.php'; + this.filepicker = new YAHOO.widget.Panel('file-picker-' + client_id, { draggable: true, close: true, @@ -134,7 +151,7 @@ var repository_client = (function(){ params['client_id']=this.client_id; repository_client.loading(this.client_id, 'load'); var trans = YAHOO.util.Connect.asyncRequest('POST', - moodle_cfg.wwwroot+'/repository/ws.php?action=gsearch', this.search_cb, repository_client.postdata(params)); + MOODLE.repository.api+'?action=gsearch', this.search_cb, repository_client.postdata(params)); } search_btn.on('contentReady', function() { search_btn.on('click', this.fnSearch, this.input_keyword); @@ -173,9 +190,9 @@ var repository_client = (function(){ var container = new YAHOO.util.Element('repo-list-'+this.client_id); container.set('innerHTML', ''); container.on('contentReady', function() { - this.init_search(); - for(var i in repository_listing[this.client_id]) { - var repo = repository_listing[this.client_id][i]; + this.init_search(); + for(var i in MOODLE.repository.listing[this.client_id]) { + var repo = MOODLE.repository.listing[this.client_id][i]; var support = false; if(this.env=='editor' && this.accepted_types != '*'){ if(repo.supported_types!='*'){ @@ -210,7 +227,7 @@ var repository_client = (function(){ var client_id = result[1]; var repo_id = result[2]; // high light currect selected repository - for(var cc in repository_listing[client_id]){ + for(var cc in MOODLE.repository.listing[client_id]){ var tmp_id = 'repo-call-'+client_id+'-'+ cc; var el = document.getElementById(tmp_id); if(el){ @@ -260,7 +277,7 @@ repository_client.req = function(client_id, id, path, page) { params['page']=page; } params['accepted_types'] = r.accepted_types; - var trans = YAHOO.util.Connect.asyncRequest('POST', moodle_cfg.wwwroot+'/repository/ws.php?action=list', this.req_cb, this.postdata(params)); + var trans = YAHOO.util.Connect.asyncRequest('POST', MOODLE.repository.api+'?action=list', this.req_cb, this.postdata(params)); } repository_client.req_cb = { @@ -319,7 +336,7 @@ repository_client.req_search_results = function(client_id, id, path, page) { params['page']=page; } params['accepted_types'] = r.accepted_types; - var trans = YAHOO.util.Connect.asyncRequest('POST', moodle_cfg.wwwroot+'/repository/ws.php?action=search', this.req_cb, this.postdata(params)); + var trans = YAHOO.util.Connect.asyncRequest('POST', MOODLE.repository.api+'?action=search', this.req_cb, this.postdata(params)); } repository_client.print_login = function(id, data) { @@ -421,7 +438,7 @@ repository_client.login = function(id, repo_id) { params['accepted_types'] = this.fp[id].accepted_types; this.loading(id, 'load'); var trans = YAHOO.util.Connect.asyncRequest('POST', - moodle_cfg.wwwroot+'/repository/ws.php?action=sign', this.req_cb, this.postdata(params)); + MOODLE.repository.api+'?action=sign', this.req_cb, this.postdata(params)); } repository_client.login_keypress = function(evt,action) { evt = (evt) ? evt : ((window.event) ? window.event : "") @@ -465,7 +482,7 @@ repository_client.search = function(id, repo_id) { params['accepted_types'] = this.fp[id].accepted_types; this.loading(id, 'load'); var trans = YAHOO.util.Connect.asyncRequest('POST', - moodle_cfg.wwwroot+'/repository/ws.php?action=search', this.req_cb, this.postdata(params)); + MOODLE.repository.api+'?action=search', this.req_cb, this.postdata(params)); } repository_client.loading = function(id, type, name) { var panel = new YAHOO.util.Element('panel-'+id); @@ -529,7 +546,7 @@ repository_client.view_as_list = function(client_id, data) { params['client_id']=node.client_id; params['accepted_types']=fp.accepted_types; var trans = YAHOO.util.Connect.asyncRequest('POST', - moodle_cfg.wwwroot+'/repository/ws.php?action=list',callback,repository_client.postdata(params)); + MOODLE.repository.api+'?action=list',callback,repository_client.postdata(params)); } tree.dynload.client_id = client_id; if(fp.fs.dynload) { @@ -811,7 +828,7 @@ repository_client.view_as_icons = function(client_id, data) { params['itemid'] = this.itemid; params['title'] = this.title; var trans = YAHOO.util.Connect.asyncRequest('POST', - moodle_cfg.wwwroot+'/repository/ws.php?action=delete', + MOODLE.repository.api+'?action=delete', this, repository_client.postdata(params) ); @@ -842,7 +859,7 @@ repository_client.view_as_icons = function(client_id, data) { params['client_id'] = client_id; repository_client.loading(client_id, 'load'); var trans = YAHOO.util.Connect.asyncRequest('POST', - moodle_cfg.wwwroot+'/repository/ws.php?action=list', repository_client.req_cb, repository_client.postdata(params)); + MOODLE.repository.api+'?action=list', repository_client.req_cb, repository_client.postdata(params)); }else{ repository_client.view_as_icons(client_id, this.fs); } @@ -931,7 +948,7 @@ repository_client.print_footer = function(client_id) { params['client_id']=client_id; repository_client.loading(client_id, 'load'); var trans = YAHOO.util.Connect.asyncRequest('POST', - moodle_cfg.wwwroot+'/repository/ws.php?action=ccache', repository_client.req_cb, repository_client.postdata(params)); + MOODLE.repository.api+'?action=ccache', repository_client.req_cb, repository_client.postdata(params)); } } if(fs.manage) { @@ -977,17 +994,17 @@ repository_client.postdata = function(obj) { repository_client.popup = function(client_id, url) { window.open(url,'repo_auth', 'location=0,status=0,scrollbars=0,width=500,height=300'); - cached_client_id = client_id; + MOODLE.repository.cache.client_id = client_id; return true; } function repository_callback(id) { - repository_client.req(cached_client_id, id, ''); + repository_client.req(MOODLE.repository.cache.client_id, id, ''); } repository_client.logout = function(client_id, repo_id) { var params = []; params['repo_id'] = repo_id; params['client_id'] = client_id; - var trans = YAHOO.util.Connect.asyncRequest('POST', moodle_cfg.wwwroot+'/repository/ws.php?action=logout', + var trans = YAHOO.util.Connect.asyncRequest('POST', MOODLE.repository.api+'?action=logout', repository_client.req_cb, repository_client.postdata(params)); } repository_client.download = function(client_id, repo_id) { @@ -1009,7 +1026,7 @@ repository_client.download = function(client_id, repo_id) { params['repo_id']=repo_id; params['client_id']=client_id; var trans = YAHOO.util.Connect.asyncRequest('POST', - moodle_cfg.wwwroot+'/repository/ws.php?action=download', + MOODLE.repository.api+'?action=download', repository_client.download_cb, repository_client.postdata(params)); } @@ -1096,7 +1113,7 @@ repository_client.upload = function(client_id) { YAHOO.util.Connect.setForm(aform, true, true); var trans = YAHOO.util.Connect.asyncRequest('POST', - moodle_cfg.wwwroot+'/repository/ws.php?action=upload&itemid='+fp.itemid + MOODLE.repository.api+'?action=upload&itemid='+fp.itemid +'&sesskey='+moodle_cfg.sesskey +'&ctx_id='+fp_config.contextid +'&savepath='+fp.savepath @@ -1138,7 +1155,7 @@ repository_client.search_form = function(client_id, id) { params['ctx_id']=fp_config.contextid; params['repo_id']=id; var trans = YAHOO.util.Connect.asyncRequest('POST', - moodle_cfg.wwwroot+'/repository/ws.php?action=searchform', + MOODLE.repository.api+'?action=searchform', repository_client.search_form_cb, repository_client.postdata(params)); } @@ -1155,13 +1172,13 @@ success: function(o) { el.id = 'fp-search-dlg'; var dlg_title = document.createElement('DIV'); dlg_title.className = 'hd'; - dlg_title.innerHTML = fp_lang.searching+"\"" + repository_listing[data.client_id][fp.fs.repo_id].name + '"'; + dlg_title.innerHTML = fp_lang.searching+"\"" + MOODLE.repository.listing[data.client_id][fp.fs.repo_id].name + '"'; var dlg_body = document.createElement('DIV'); dlg_body.className = 'bd'; var sform = document.createElement('FORM'); sform.method = 'POST'; sform.id = "fp-search-form"; - sform.action = moodle_cfg.wwwroot+'/repository/ws.php?action=search'; + sform.action = MOODLE.repository.api+'?action=search'; sform.innerHTML = data['form']; dlg_body.appendChild(sform); el.appendChild(dlg_title); @@ -1173,7 +1190,7 @@ success: function(o) { repository_client.loading(client_id, 'load'); YAHOO.util.Connect.setForm('fp-search-form', false, false); this.cancel(); - var url = moodle_cfg.wwwroot+'/repository/ws.php?action=search&env='+dlg_handler.env + var url = MOODLE.repository.api+'?action=search&env='+dlg_handler.env +'&client_id='+client_id; var trans = YAHOO.util.Connect.asyncRequest('POST', url, repository_client.req_cb); @@ -1245,9 +1262,9 @@ function open_filepicker(id, params) { } if(params.filetype) { if(params.filetype == 'image') { - r.accepted_types = file_extensions.image; + r.accepted_types = MOODLE.repository.extensions.image; } else if(params.filetype == 'video' || params.filetype== 'media') { - r.accepted_types = file_extensions.media; + r.accepted_types = MOODLE.repository.extensions.media; } else if(params.filetype == 'file') { r.accepted_types = '*'; } diff --git a/repository/ws.php b/repository/repository_ajax.php old mode 100644 new mode 100755 similarity index 85% rename from repository/ws.php rename to repository/repository_ajax.php index 4d203adb823b7..c0c57f564e0cb --- a/repository/ws.php +++ b/repository/repository_ajax.php @@ -1,4 +1,4 @@ -dirroot.'/repository/'.$type.'/repository.class.php'); $classname = 'repository_' . $type; try { - $repo = new $classname($repo_id, $contextid, array('ajax'=>true, 'name'=>$repository->name, 'client_id'=>$client_id)); + $repo = new $classname($repo_id, $contextid, array('ajax'=>true, 'name'=>$repository->name, 'type'=>$type, 'client_id'=>$client_id)); } catch (repository_exception $e){ $err->e = $e->getMessage(); die(json_encode($err)); @@ -200,41 +200,34 @@ break; case 'download': try { - // $file is the specific information of file, such as url, or meta information - // $title is the file name in file pool - // $itemid and $save_path will be used by local plugin only - if ($env == 'texturl') { - $CFG->repositoryuseexternallink = true; - } - $filepath = $repo->get_file($file, $title, $itemid, $save_path); - if ($filepath === false) { - $err->e = get_string('cannotdownload', 'repository'); - die(json_encode($err)); - } - if (empty($itemid)) { - $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100); + if ($env == 'url' /* TODO: or request_external_url by user */) { + if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $file)) { + die(json_encode(array('type'=>'link', 'client_id'=>$client_id, + 'url'=>$file, 'id'=>$file, 'file'=>$file))); + } else { + $err->e = get_string('invalidurl'); + die(json_encode($err)); + } } - if (is_array($filepath)) { - // file api don't have real file path, so we need more file api specific info for "local" plugin - // only used by local plugin - $fileinfo = $filepath; + // we have two special repoisitory type need to deal with + if ($repo->options['type'] == 'local' or $repo->options['type'] == 'draft') { + $fileinfo = $repo->move_to_draft($file, $title, $itemid, $save_path); $info = array(); $info['client_id'] = $client_id; $info['file'] = $fileinfo['title']; $info['id'] = $itemid; $info['url'] = $CFG->httpswwwroot.'/draftfile.php/'.$fileinfo['contextid'].'/user_draft/'.$itemid.'/'.$fileinfo['title']; - echo json_encode($info); - } else if (preg_match('#(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#', $filepath)) { - // process external link - $url = $filepath; - echo json_encode(array('type'=>'link', 'client_id'=>$client_id, 'url'=>$url, 'id'=>$url, 'file'=>$url)); - } else { - // used by most repository plugins - // move downloaded file to file pool - $info = repository::move_to_filepool($filepath, $title, $itemid, $save_path); - $info['client_id'] = $client_id; - echo json_encode($info); + die(json_encode($info)); + } + + $filepath = $repo->get_file($file, $title, $itemid, $save_path); + if ($filepath === false) { + $err->e = get_string('cannotdownload', 'repository'); + die(json_encode($err)); } + $info = repository::move_to_filepool($filepath, $title, $itemid, $save_path); + $info['client_id'] = $client_id; + echo json_encode($info); } catch (repository_exception $e){ $err->e = $e->getMessage(); die(json_encode($err)); diff --git a/repository/s3/repository.class.php b/repository/s3/repository.class.php index beb61977bbee6..a9a5182d93a91 100644 --- a/repository/s3/repository.class.php +++ b/repository/s3/repository.class.php @@ -81,4 +81,7 @@ public function type_config_form(&$mform) { $mform->addRule('secret_key', $strrequired, 'required', null, 'client'); return true; } + public function supported_returntypes() { + return FILE_INTERNAL; + } } diff --git a/repository/upload/repository.class.php b/repository/upload/repository.class.php index 5813cd4016539..72d5e6152eec3 100755 --- a/repository/upload/repository.class.php +++ b/repository/upload/repository.class.php @@ -70,5 +70,8 @@ public function get_listing($path='', $page='') { public function get_name(){ return get_string('repositoryname', 'repository_upload'); } + public function supported_returntypes() { + return FILE_INTERNAL; + } } diff --git a/repository/url/repository.class.php b/repository/url/repository.class.php index e295dc2695bd9..c36e7fad55d29 100755 --- a/repository/url/repository.class.php +++ b/repository/url/repository.class.php @@ -137,5 +137,8 @@ public function guess_filename($url, $type) { public function get_name(){ return get_string('repositoryname', 'repository_url');; } + public function supported_returntypes() { + return (FILE_INTERNAL | FILE_EXTERNAL); + } } diff --git a/repository/webdav/repository.class.php b/repository/webdav/repository.class.php index f49aa2b59e4c8..80ae36d81bffb 100644 --- a/repository/webdav/repository.class.php +++ b/repository/webdav/repository.class.php @@ -144,4 +144,7 @@ public function instance_config_form(&$mform) { $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40')); $mform->addElement('text', 'webdav_password', get_string('webdav_password', 'repository_webdav'), array('size' => '40')); } + public function supported_returntypes() { + return (FILE_INTERNAL | FILE_EXTERNAL); + } } diff --git a/repository/wikimedia/repository.class.php b/repository/wikimedia/repository.class.php index c65044693cd0d..69c1d220becf5 100644 --- a/repository/wikimedia/repository.class.php +++ b/repository/wikimedia/repository.class.php @@ -60,4 +60,7 @@ public function logout() { public static function get_type_option_names() { return null; } + public function supported_returntypes() { + return (FILE_INTERNAL | FILE_EXTERNAL); + } } diff --git a/repository/youtube/repository.class.php b/repository/youtube/repository.class.php index 5a79a4305b57e..1af405148e3fc 100644 --- a/repository/youtube/repository.class.php +++ b/repository/youtube/repository.class.php @@ -55,9 +55,6 @@ private function _get_collection($keyword, $start, $max, $sort) { return $list; } - public function get_file($url, $title) { - return $url; - } public function global_search() { return false; } @@ -80,10 +77,10 @@ public function print_login($ajax = true) { $ret['login_btn_action'] = 'search'; return $ret; } - public function supported_return_value() { - return 'link'; - } public function supported_filetypes() { return array('web_video'); } + public function supported_returntypes() { + return FILE_EXTERNAL; + } }