From 8a288971e25eaaa4c8cbd67e450cee0e12435d4e Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Sun, 10 Jun 2012 17:47:00 +1000 Subject: [PATCH] MDL-33513: Make files->source field consistent --- lib/filelib.php | 2 +- repository/boxnet/lib.php | 10 ++++++++ repository/dropbox/lib.php | 10 ++++++++ repository/filesystem/lib.php | 10 ++++++++ repository/flickr/lib.php | 39 ++++++++++++++++++++------------ repository/flickr_public/lib.php | 32 ++++++++++++++++++++------ repository/lib.php | 29 ++++++++++++++++++++++++ repository/repository_ajax.php | 16 ++++++------- repository/s3/lib.php | 10 ++++++++ repository/upload/lib.php | 4 +++- repository/url/lib.php | 10 ++++++++ repository/wikimedia/lib.php | 10 ++++++++ 12 files changed, 150 insertions(+), 32 deletions(-) diff --git a/lib/filelib.php b/lib/filelib.php index ef1abdb1a45..092521f7952 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -770,6 +770,7 @@ function file_save_draft_area_files($draftitemid, $contextid, $component, $filea $newhashes = array(); foreach ($draftfiles as $file) { $newhash = $fs->get_pathname_hash($contextid, $component, $filearea, $itemid, $file->get_filepath(), $file->get_filename()); + file_restore_source_field_from_draft_file($file); $newhashes[$newhash] = $file; } $filecount = 0; @@ -790,7 +791,6 @@ function file_save_draft_area_files($draftitemid, $contextid, $component, $filea continue; } - file_restore_source_field_from_draft_file($newfile); // Replaced file content if ($oldfile->get_contenthash() != $newfile->get_contenthash()) { $oldfile->replace_content_with($newfile); diff --git a/repository/boxnet/lib.php b/repository/boxnet/lib.php index d4102d3d3f0..b1c087c0718 100644 --- a/repository/boxnet/lib.php +++ b/repository/boxnet/lib.php @@ -293,6 +293,16 @@ class repository_boxnet extends repository { return $this->get_name() . ': ' . $reference; } + /** + * Return the source information + * + * @param stdClass $url + * @return string|null + */ + public function get_file_source_info($url) { + return $url; + } + /** * Repository method to serve file * diff --git a/repository/dropbox/lib.php b/repository/dropbox/lib.php index e454615a067..c9960e45cfd 100644 --- a/repository/dropbox/lib.php +++ b/repository/dropbox/lib.php @@ -414,6 +414,16 @@ class repository_dropbox extends repository { return $this->get_name() . ': ' . $ref->path; } + /** + * Return the source information + * + * @param stdClass $filepath + * @return string|null + */ + public function get_file_source_info($filepath) { + return 'Dropbox:' . $filepath; + } + /** * Repository method to serve file * diff --git a/repository/filesystem/lib.php b/repository/filesystem/lib.php index 99b9dad767f..beb0feba592 100644 --- a/repository/filesystem/lib.php +++ b/repository/filesystem/lib.php @@ -152,6 +152,16 @@ class repository_filesystem extends repository { return array('path'=>$file, 'url'=>''); } + /** + * Return the source information + * + * @param stdClass $filepath + * @return string|null + */ + public function get_file_source_info($filepath) { + return $filepath; + } + public function logout() { return true; } diff --git a/repository/flickr/lib.php b/repository/flickr/lib.php index b54ad931f27..368e8ec2bb8 100644 --- a/repository/flickr/lib.php +++ b/repository/flickr/lib.php @@ -222,9 +222,13 @@ class repository_flickr extends repository { return $this->search('', $page); } - public function get_link($photo_id) { - global $CFG; - $result = $this->flickr->photos_getSizes($photo_id); + /** + * Return photo url by given photo id + * @param string $photoid + * @return string + */ + private function build_photo_url($photoid) { + $result = $this->flickr->photos_getSizes($photoid); $url = ''; if(!empty($result[4])) { $url = $result[4]['source']; @@ -236,23 +240,18 @@ class repository_flickr extends repository { return $url; } + public function get_link($photoid) { + return $this->build_photo_url($photoid); + } + /** * - * @param string $photo_id + * @param string $photoid * @param string $file * @return string */ - public function get_file($photo_id, $file = '') { - global $CFG; - $result = $this->flickr->photos_getSizes($photo_id); - $url = ''; - if(!empty($result[4])) { - $url = $result[4]['source']; - } elseif(!empty($result[3])) { - $url = $result[3]['source']; - } elseif(!empty($result[2])) { - $url = $result[2]['source']; - } + public function get_file($photoid, $file = '') { + $url = $this->build_photo_url($photoid); $path = $this->prepare_file($file); $fp = fopen($path, 'w'); $c = new curl; @@ -315,4 +314,14 @@ class repository_flickr extends repository { public function supported_returntypes() { return (FILE_INTERNAL | FILE_EXTERNAL); } + + /** + * Return the source information + * + * @param stdClass $url + * @return string|null + */ + public function get_file_source_info($photoid) { + return $this->build_photo_url($photoid); + } } diff --git a/repository/flickr_public/lib.php b/repository/flickr_public/lib.php index 3b34b980b94..f10bd516ba9 100644 --- a/repository/flickr_public/lib.php +++ b/repository/flickr_public/lib.php @@ -396,9 +396,13 @@ class repository_flickr_public extends repository { return $str; } - public function get_link($photo_id) { - global $CFG; - $result = $this->flickr->photos_getSizes($photo_id); + /** + * Return photo url by given photo id + * @param string $photoid + * @return string + */ + private function build_photo_url($photoid) { + $result = $this->flickr->photos_getSizes($photoid); $url = ''; if(!empty($result[4])) { $url = $result[4]['source']; @@ -410,23 +414,27 @@ class repository_flickr_public extends repository { return $url; } + public function get_link($photoid) { + return $this->build_photo_id($photoid); + } + /** * * @global object $CFG - * @param string $photo_id + * @param string $photoid * @param string $file * @return string */ - public function get_file($photo_id, $file = '') { + public function get_file($photoid, $file = '') { global $CFG; - $info = $this->flickr->photos_getInfo($photo_id); + $info = $this->flickr->photos_getInfo($photoid); if ($info['owner']['realname']) { $author = $info['owner']['realname']; } else { $author = $info['owner']['username']; } $copyright = get_string('author', 'repository') . ': ' . $author; - $result = $this->flickr->photos_getSizes($photo_id); + $result = $this->flickr->photos_getSizes($photoid); // download link $source = ''; // flickr photo page @@ -517,4 +525,14 @@ class repository_flickr_public extends repository { public function supported_returntypes() { return (FILE_INTERNAL | FILE_EXTERNAL); } + + /** + * Return the source information + * + * @param stdClass $url + * @return string|null + */ + public function get_file_source_info($photoid) { + return $this->build_photo_url($photoid); + } } diff --git a/repository/lib.php b/repository/lib.php index 5f72bafe676..f7aa41aea81 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -1133,6 +1133,16 @@ abstract class repository { return null; } + /** + * Return the source information + * + * @param stdClass $url + * @return string|null + */ + public function get_file_source_info($url) { + return $url; + } + /** * Move file from download folder to file pool using FILE API * @@ -2190,6 +2200,25 @@ abstract class repository { return true; } + + /** + * Build draft file's source field + * + * {@link file_restore_source_field_from_draft_file()} + * XXX: This is a hack for file manager (MDL-28666) + * For newly created draft files we have to construct + * source filed in php serialized data format. + * File manager needs to know the original file information before copying + * to draft area, so we append these information in mdl_files.source field + * + * @param string $sourcefield + * @param string + */ + public static function build_source_field($source) { + $sourcefield = new stdClass; + $sourcefield->source = $source; + return serialize($sourcefield); + } } /** diff --git a/repository/repository_ajax.php b/repository/repository_ajax.php index 240faf3cc74..050ccc300a4 100644 --- a/repository/repository_ajax.php +++ b/repository/repository_ajax.php @@ -255,6 +255,11 @@ switch ($action) { $event['existingfile']->filename = $saveas_filename; $event['existingfile']->url = moodle_url::make_draftfile_url($itemid, $saveas_path, $saveas_filename)->out();; } else { + + // {@link repository::build_source_field()} + $sourcefield = $repo->get_file_source_info($source); + $record->source = $repo::build_source_field($sourcefield); + $storedfile = $fs->create_file_from_reference($record, $repo_id, $reference); $event = array( 'url'=>moodle_url::make_draftfile_url($storedfile->get_itemid(), $storedfile->get_filepath(), $storedfile->get_filename())->out(), @@ -292,14 +297,9 @@ switch ($action) { throw new file_exception('maxbytes'); } - // {@link file_restore_source_field_from_draft_file()} - $sourcefield = ''; - if (!empty($downloadedfile['url'])) { - $source = new stdClass; - $source->source = $downloadedfile['url']; - $sourcefield = serialize($source); - } - $record->source = $sourcefield; + // {@link repository::build_source_field()} + $sourcefield = $repo->get_file_source_info($source); + $record->source = $repo::build_source_field($sourcefield); $info = repository::move_to_filepool($downloadedfile['path'], $record); if (empty($info)) { diff --git a/repository/s3/lib.php b/repository/s3/lib.php index 2479050597c..511d76fe11e 100644 --- a/repository/s3/lib.php +++ b/repository/s3/lib.php @@ -119,6 +119,16 @@ class repository_s3 extends repository { return array('path'=>$path); } + /** + * Return the source information + * + * @param stdClass $url + * @return string|null + */ + public function get_file_source_info($filepath) { + return 's3:' . $filepath; + } + /** * S3 doesn't require login * diff --git a/repository/upload/lib.php b/repository/upload/lib.php index a307ab6228d..cf023dd10b4 100644 --- a/repository/upload/lib.php +++ b/repository/upload/lib.php @@ -143,6 +143,9 @@ class repository_upload extends repository { self::antivir_scan_file($_FILES[$elname]['tmp_name'], $_FILES[$elname]['name'], true); @chmod($_FILES[$elname]['tmp_name'], $permissions); + // {@link repository::build_source_field()} + $record->source = self::build_source_field($_FILES[$elname]['name']); + if (empty($saveas_filename)) { $record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE); } else { @@ -186,7 +189,6 @@ class repository_upload extends repository { } $record->contextid = $context->id; $record->userid = $USER->id; - $record->source = ''; if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) { $existingfilename = $record->filename; diff --git a/repository/url/lib.php b/repository/url/lib.php index 16f2eb07ae5..20725c81f23 100644 --- a/repository/url/lib.php +++ b/repository/url/lib.php @@ -217,5 +217,15 @@ EOD; public function supported_returntypes() { return (FILE_INTERNAL | FILE_EXTERNAL); } + + /** + * Return the source information + * + * @param stdClass $url + * @return string|null + */ + public function get_file_source_info($url) { + return $url; + } } diff --git a/repository/wikimedia/lib.php b/repository/wikimedia/lib.php index 50d85562873..cc4bf2be796 100644 --- a/repository/wikimedia/lib.php +++ b/repository/wikimedia/lib.php @@ -127,4 +127,14 @@ EOD; public function supported_returntypes() { return (FILE_INTERNAL | FILE_EXTERNAL); } + + /** + * Return the source information + * + * @param stdClass $url + * @return string|null + */ + public function get_file_source_info($url) { + return $url; + } } -- 2.43.0