MDL-34665 Dropbox displays thumbnails and return info about file size and date last...
authorMarina Glancy <marina@moodle.com>
Wed, 1 Aug 2012 07:01:38 +0000 (15:01 +0800)
committerMarina Glancy <marina@moodle.com>
Wed, 29 Aug 2012 03:56:17 +0000 (11:56 +0800)
repository/dropbox/lib.php
repository/dropbox/locallib.php
repository/dropbox/thumbnail.php [new file with mode: 0644]

index 008b566..c0edef3 100644 (file)
@@ -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
index 89c7e73..ea83d36 100644 (file)
@@ -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 (file)
index 0000000..6a4887e
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * 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');