Skip to content

Commit

Permalink
MDL-34665 Dropbox displays thumbnails and return info about file size…
Browse files Browse the repository at this point in the history
… and date last modified
  • Loading branch information
marinaglancy committed Aug 29, 2012
1 parent db02d84 commit f4fe646
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 8 deletions.
56 changes: 48 additions & 8 deletions repository/dropbox/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,28 +220,68 @@ public function get_listing($path = '', $page = '1') {
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
Expand Down
26 changes: 26 additions & 0 deletions repository/dropbox/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ protected function prepare_filepath($filepath) {
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
*
Expand Down
44 changes: 44 additions & 0 deletions repository/dropbox/thumbnail.php
Original file line number Diff line number Diff line change
@@ -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');

0 comments on commit f4fe646

Please sign in to comment.