MDL-33513: Make files->source field consistent

This commit is contained in:
Dongsheng Cai 2012-06-10 17:47:00 +10:00 committed by Dan Poltawski
parent ed2ed3f97e
commit d6453211b6
13 changed files with 180 additions and 32 deletions

View File

@ -188,6 +188,29 @@ class boxclient {
return $ret; return $ret;
} }
/**
* Get box.net file info
*
* @param string $fileid
* @return string|null
*/
function get_file_info($fileid) {
$this->_clearErrors();
$params = array();
$params['action'] = 'get_file_info';
$params['file_id'] = $fileid;
$params['auth_token'] = $this->auth_token;
$params['api_key'] = $this->api_key;
$http = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
$xml = $http->get($this->_box_api_url, $params);
$o = simplexml_load_string(trim($xml));
if ($o->status == 's_get_file_info') {
return $o->info;
} else {
return null;
}
}
/** /**
* @param array $sax * @param array $sax
* @param array $tree Passed by reference * @param array $tree Passed by reference

View File

@ -773,6 +773,7 @@ function file_save_draft_area_files($draftitemid, $contextid, $component, $filea
$newhashes = array(); $newhashes = array();
foreach ($draftfiles as $file) { foreach ($draftfiles as $file) {
$newhash = $fs->get_pathname_hash($contextid, $component, $filearea, $itemid, $file->get_filepath(), $file->get_filename()); $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; $newhashes[$newhash] = $file;
} }
$filecount = 0; $filecount = 0;
@ -793,7 +794,6 @@ function file_save_draft_area_files($draftitemid, $contextid, $component, $filea
continue; continue;
} }
file_restore_source_field_from_draft_file($newfile);
// Replaced file content // Replaced file content
if ($oldfile->get_contenthash() != $newfile->get_contenthash()) { if ($oldfile->get_contenthash() != $newfile->get_contenthash()) {
$oldfile->replace_content_with($newfile); $oldfile->replace_content_with($newfile);

View File

@ -308,6 +308,23 @@ class repository_boxnet extends repository {
} }
} }
/**
* Return the source information
*
* @param stdClass $url
* @return string|null
*/
public function get_file_source_info($url) {
$array = explode('/', $url);
$fileid = array_pop($array);
$fileinfo = $this->boxclient->get_file_info($fileid);
if (!empty($fileinfo)) {
return 'Box:' . (string)$fileinfo->file_name;
} else {
return $url;
}
}
/** /**
* Repository method to serve the referenced file * Repository method to serve the referenced file
* *

View File

@ -425,6 +425,16 @@ class repository_dropbox extends repository {
} }
} }
/**
* Return the source information
*
* @param stdClass $filepath
* @return string|null
*/
public function get_file_source_info($filepath) {
return 'Dropbox:' . $filepath;
}
/** /**
* Repository method to serve the referenced file * Repository method to serve the referenced file
* *

View File

@ -152,6 +152,16 @@ class repository_filesystem extends repository {
return array('path'=>$file, 'url'=>''); 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() { public function logout() {
return true; return true;
} }

View File

@ -222,9 +222,13 @@ class repository_flickr extends repository {
return $this->search('', $page); return $this->search('', $page);
} }
public function get_link($photo_id) { /**
global $CFG; * Return photo url by given photo id
$result = $this->flickr->photos_getSizes($photo_id); * @param string $photoid
* @return string
*/
private function build_photo_url($photoid) {
$result = $this->flickr->photos_getSizes($photoid);
$url = ''; $url = '';
if(!empty($result[4])) { if(!empty($result[4])) {
$url = $result[4]['source']; $url = $result[4]['source'];
@ -236,23 +240,18 @@ class repository_flickr extends repository {
return $url; return $url;
} }
public function get_link($photoid) {
return $this->build_photo_url($photoid);
}
/** /**
* *
* @param string $photo_id * @param string $photoid
* @param string $file * @param string $file
* @return string * @return string
*/ */
public function get_file($photo_id, $file = '') { public function get_file($photoid, $file = '') {
global $CFG; $url = $this->build_photo_url($photoid);
$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'];
}
$path = $this->prepare_file($file); $path = $this->prepare_file($file);
$fp = fopen($path, 'w'); $fp = fopen($path, 'w');
$c = new curl; $c = new curl;
@ -315,4 +314,14 @@ class repository_flickr extends repository {
public function supported_returntypes() { public function supported_returntypes() {
return (FILE_INTERNAL | FILE_EXTERNAL); return (FILE_INTERNAL | FILE_EXTERNAL);
} }
/**
* Return the source information
*
* @param string $photoid
* @return string|null
*/
public function get_file_source_info($photoid) {
return $this->build_photo_url($photoid);
}
} }

View File

@ -396,9 +396,13 @@ class repository_flickr_public extends repository {
return $str; return $str;
} }
public function get_link($photo_id) { /**
global $CFG; * Return photo url by given photo id
$result = $this->flickr->photos_getSizes($photo_id); * @param string $photoid
* @return string
*/
private function build_photo_url($photoid) {
$result = $this->flickr->photos_getSizes($photoid);
$url = ''; $url = '';
if(!empty($result[4])) { if(!empty($result[4])) {
$url = $result[4]['source']; $url = $result[4]['source'];
@ -410,23 +414,27 @@ class repository_flickr_public extends repository {
return $url; return $url;
} }
public function get_link($photoid) {
return $this->build_photo_id($photoid);
}
/** /**
* *
* @global object $CFG * @global object $CFG
* @param string $photo_id * @param string $photoid
* @param string $file * @param string $file
* @return string * @return string
*/ */
public function get_file($photo_id, $file = '') { public function get_file($photoid, $file = '') {
global $CFG; global $CFG;
$info = $this->flickr->photos_getInfo($photo_id); $info = $this->flickr->photos_getInfo($photoid);
if ($info['owner']['realname']) { if ($info['owner']['realname']) {
$author = $info['owner']['realname']; $author = $info['owner']['realname'];
} else { } else {
$author = $info['owner']['username']; $author = $info['owner']['username'];
} }
$copyright = get_string('author', 'repository') . ': ' . $author; $copyright = get_string('author', 'repository') . ': ' . $author;
$result = $this->flickr->photos_getSizes($photo_id); $result = $this->flickr->photos_getSizes($photoid);
// download link // download link
$source = ''; $source = '';
// flickr photo page // flickr photo page
@ -517,4 +525,14 @@ class repository_flickr_public extends repository {
public function supported_returntypes() { public function supported_returntypes() {
return (FILE_INTERNAL | FILE_EXTERNAL); return (FILE_INTERNAL | FILE_EXTERNAL);
} }
/**
* Return the source information
*
* @param string $photoid photo id
* @return string|null
*/
public function get_file_source_info($photoid) {
return $this->build_photo_url($photoid);
}
} }

View File

@ -1226,6 +1226,16 @@ abstract class repository {
return null; 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 * Move file from download folder to file pool using FILE API
* *
@ -2308,6 +2318,25 @@ abstract class repository {
$synchronized[$file->get_id()] = true; $synchronized[$file->get_id()] = true;
return true; 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 $source
* @return string serialised source field
*/
public static function build_source_field($source) {
$sourcefield = new stdClass;
$sourcefield->source = $source;
return serialize($sourcefield);
}
} }
/** /**

View File

@ -265,6 +265,11 @@ switch ($action) {
$event['existingfile']->filename = $saveas_filename; $event['existingfile']->filename = $saveas_filename;
$event['existingfile']->url = moodle_url::make_draftfile_url($itemid, $saveas_path, $saveas_filename)->out();; $event['existingfile']->url = moodle_url::make_draftfile_url($itemid, $saveas_path, $saveas_filename)->out();;
} else { } 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); $storedfile = $fs->create_file_from_reference($record, $repo_id, $reference);
$event = array( $event = array(
'url'=>moodle_url::make_draftfile_url($storedfile->get_itemid(), $storedfile->get_filepath(), $storedfile->get_filename())->out(), 'url'=>moodle_url::make_draftfile_url($storedfile->get_itemid(), $storedfile->get_filepath(), $storedfile->get_filename())->out(),
@ -302,14 +307,9 @@ switch ($action) {
throw new file_exception('maxbytes'); throw new file_exception('maxbytes');
} }
// {@link file_restore_source_field_from_draft_file()} // {@link repository::build_source_field()}
$sourcefield = ''; $sourcefield = $repo->get_file_source_info($source);
if (!empty($downloadedfile['url'])) { $record->source = $repo::build_source_field($sourcefield);
$source = new stdClass;
$source->source = $downloadedfile['url'];
$sourcefield = serialize($source);
}
$record->source = $sourcefield;
$info = repository::move_to_filepool($downloadedfile['path'], $record); $info = repository::move_to_filepool($downloadedfile['path'], $record);
if (empty($info)) { if (empty($info)) {

View File

@ -119,6 +119,16 @@ class repository_s3 extends repository {
return array('path'=>$path); return array('path'=>$path);
} }
/**
* Return the source information
*
* @param stdClass $filepath
* @return string
*/
public function get_file_source_info($filepath) {
return 'Amazon S3:' . $filepath;
}
/** /**
* S3 doesn't require login * S3 doesn't require login
* *

View File

@ -143,6 +143,9 @@ class repository_upload extends repository {
self::antivir_scan_file($_FILES[$elname]['tmp_name'], $_FILES[$elname]['name'], true); self::antivir_scan_file($_FILES[$elname]['tmp_name'], $_FILES[$elname]['name'], true);
@chmod($_FILES[$elname]['tmp_name'], $permissions); @chmod($_FILES[$elname]['tmp_name'], $permissions);
// {@link repository::build_source_field()}
$record->source = self::build_source_field($_FILES[$elname]['name']);
if (empty($saveas_filename)) { if (empty($saveas_filename)) {
$record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE); $record->filename = clean_param($_FILES[$elname]['name'], PARAM_FILE);
} else { } else {
@ -186,7 +189,6 @@ class repository_upload extends repository {
} }
$record->contextid = $context->id; $record->contextid = $context->id;
$record->userid = $USER->id; $record->userid = $USER->id;
$record->source = '';
if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) { if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
$existingfilename = $record->filename; $existingfilename = $record->filename;

View File

@ -217,5 +217,15 @@ EOD;
public function supported_returntypes() { public function supported_returntypes() {
return (FILE_INTERNAL | FILE_EXTERNAL); return (FILE_INTERNAL | FILE_EXTERNAL);
} }
/**
* Return the source information
*
* @param stdClass $url
* @return string|null
*/
public function get_file_source_info($url) {
return $url;
}
} }

View File

@ -127,4 +127,14 @@ EOD;
public function supported_returntypes() { public function supported_returntypes() {
return (FILE_INTERNAL | FILE_EXTERNAL); return (FILE_INTERNAL | FILE_EXTERNAL);
} }
/**
* Return the source information
*
* @param stdClass $url
* @return string|null
*/
public function get_file_source_info($url) {
return $url;
}
} }