From f4fe646b71debe541ef1432b6c9a51c9a8c427bc Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Wed, 1 Aug 2012 15:01:38 +0800 Subject: [PATCH] MDL-34665 Dropbox displays thumbnails and return info about file size and date last modified --- repository/dropbox/lib.php | 56 +++++++++++++++++++++++++++----- repository/dropbox/locallib.php | 26 +++++++++++++++ repository/dropbox/thumbnail.php | 44 +++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 repository/dropbox/thumbnail.php diff --git a/repository/dropbox/lib.php b/repository/dropbox/lib.php index 008b5665142..c0edef34cb6 100644 --- a/repository/dropbox/lib.php +++ b/repository/dropbox/lib.php @@ -220,28 +220,68 @@ class repository_dropbox extends repository { return $list; } $files = $result->contents; + $dirslist = array(); + $fileslist = array(); foreach ($files as $file) { if ($file->is_dir) { - $list['list'][] = array( + $dirslist[] = array( 'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)), 'path' => file_correct_filepath($file->path), - 'size' => $file->size, - 'date' => $file->modified, - 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false), + 'date' => strtotime($file->modified), + 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(64))->out(false), + 'thumbnail_height' => 64, + 'thumbnail_width' => 64, 'children' => array(), ); } else { - $list['list'][] = array( + $thumbnail = null; + if ($file->thumb_exists) { + $thumburl = new moodle_url('/repository/dropbox/thumbnail.php', + array('repo_id' => $this->id, + 'ctx_id' => $this->context->id, + 'source' => $file->path, + 'rev' => $file->rev // include revision to avoid cache problems + )); + $thumbnail = $thumburl->out(false); + } + $fileslist[] = array( 'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)), 'source' => $file->path, - 'size' => $file->size, - 'date' => $file->modified, - 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->path, 90))->out(false) + 'size' => $file->bytes, + 'date' => strtotime($file->modified), + 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->path, 64))->out(false), + 'realthumbnail' => $thumbnail, + 'thumbnail_height' => 64, + 'thumbnail_width' => 64, ); } } + $fileslist = array_filter($fileslist, array($this, 'filter')); + $list['list'] = array_merge($dirslist, array_values($fileslist)); return $list; } + + /** + * Displays a thumbnail for current user's dropbox file + * + * @param string $string + */ + public function send_thumbnail($source) { + $saveas = $this->prepare_file(''); + try { + $access_key = get_user_preferences($this->setting.'_access_key', ''); + $access_secret = get_user_preferences($this->setting.'_access_secret', ''); + $this->dropbox->set_access_token($access_key, $access_secret); + $this->dropbox->get_thumbnail($source, $saveas, self::SYNCIMAGE_TIMEOUT); + $content = file_get_contents($saveas); + unlink($saveas); + // set 30 days lifetime for the image. If the image is changed in dropbox it will have + // different revision number and URL will be different. It is completely safe + // to cache thumbnail in the browser for a long time + send_file($content, basename($source), 30*24*60*60, 0, true); + } catch (Exception $e) {} + } + /** * Logout from dropbox * @return array diff --git a/repository/dropbox/locallib.php b/repository/dropbox/locallib.php index 89c7e735dbd..ea83d36b976 100644 --- a/repository/dropbox/locallib.php +++ b/repository/dropbox/locallib.php @@ -85,6 +85,32 @@ class dropbox extends oauth_helper { return $filepath; } + /** + * Retrieves the default (64x64) thumbnail for dropbox file + * + * @throws moodle_exception when file could not be downloaded + * + * @param string $filepath local path in Dropbox + * @param string $saveas path to file to save the result + * @param int $timeout request timeout in seconds, 0 means no timeout + * @return array with attributes 'path' and 'url' + */ + public function get_thumbnail($filepath, $saveas, $timeout = 0) { + $url = $this->dropbox_content_api.'/thumbnails/'.$this->mode.$this->prepare_filepath($filepath); + if (!($fp = fopen($saveas, 'w'))) { + throw new moodle_exception('cannotwritefile', 'error', '', $saveas); + } + $this->setup_oauth_http_options(array('timeout' => $timeout, 'file' => $fp, 'BINARYTRANSFER' => true)); + $result = $this->get($url); + fclose($fp); + if ($result === true) { + return array('path'=>$saveas, 'url'=>$url); + } else { + unlink($saveas); + throw new moodle_exception('errorwhiledownload', 'repository', '', $result); + } + } + /** * Downloads a file from Dropbox and saves it locally * diff --git a/repository/dropbox/thumbnail.php b/repository/dropbox/thumbnail.php new file mode 100644 index 00000000000..6a4887e7b80 --- /dev/null +++ b/repository/dropbox/thumbnail.php @@ -0,0 +1,44 @@ +. + +/** + * This script displays one thumbnail of the image in current user's dropbox. + * If {@link repository_dropbox::send_thumbnail()} can not display image + * the default 64x64 filetype icon is returned + * + * @package repository_dropbox + * @copyright 2012 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(dirname(dirname(dirname(__FILE__))).'/config.php'); +require_once(dirname(__FILE__).'/lib.php'); + +$repo_id = optional_param('repo_id', 0, PARAM_INT); // Repository ID +$contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // Context ID +$source = optional_param('source', '', PARAM_TEXT); // File path in current user's dropbox + +if (isloggedin() && $repo_id && $source + && ($repo = repository::get_repository_by_id($repo_id, $contextid)) + && method_exists($repo, 'send_thumbnail')) { + // try requesting thumbnail and outputting it. This function exits if thumbnail was retrieved + $repo->send_thumbnail($source); +} + +// send default icon for the file type +$fileicon = file_extension_icon($source, 64); +send_file($CFG->dirroot.'/pix/'.$fileicon.'.png', basename($fileicon).'.png');