3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * This is a repository class used to browse Amazon S3 content.
25 * @copyright 2009 Dongsheng Cai
26 * @author Dongsheng Cai <dongsheng@moodle.com>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 require_once('S3.php');
32 class repository_s3
extends repository
{
36 * @param int $repositoryid
37 * @param object $context
38 * @param array $options
40 public function __construct($repositoryid, $context = SYSCONTEXTID
, $options = array()) {
41 parent
::__construct($repositoryid, $context, $options);
42 $this->access_key
= get_config('s3', 'access_key');
43 $this->secret_key
= get_config('s3', 'secret_key');
44 $this->s
= new S3($this->access_key
, $this->secret_key
);
51 * @return array The file list and options
53 public function get_listing($path = '') {
55 if (empty($this->access_key
)) {
56 die(json_encode(array('e'=>get_string('needaccesskey', 'repository_s3'))));
59 $list['list'] = array();
60 // the management interface url
61 $list['manage'] = false;
62 // dynamically loading
63 $list['dynload'] = true;
64 // the current path of this list.
65 // set to true, the login link will be removed
66 $list['nologin'] = true;
67 // set to true, the search button will be removed
68 $list['nosearch'] = true;
71 $buckets = $this->s
->listBuckets();
72 foreach ($buckets as $bucket) {
75 'children' => array(),
76 'thumbnail'=>$OUTPUT->pix_url('f/folder-32')->out(false),
82 $contents = $this->s
->getBucket($path);
83 foreach ($contents as $file) {
84 $info = $this->s
->getObjectInfo($path, baseName($file['name']));
86 'title'=>$file['name'],
87 'size'=>$file['size'],
88 'date'=>userdate($file['time']),
89 'source'=>$path.'/'.$file['name'],
90 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file['name'], 32))->out(false)
95 $list['list'] = $tree;
101 * Download S3 files to moodle
103 * @param string $filepath
104 * @param string $file The file path in moodle
105 * @return array The local stored path
107 public function get_file($filepath, $file) {
109 $arr = explode('/', $filepath);
112 $path = $this->prepare_file($file);
113 $this->s
->getObject($bucket, $filename, $path);
114 return array('path'=>$path);
118 * S3 doesn't require login
122 public function check_login() {
127 * S3 doesn't provide search
131 public function global_search() {
135 public static function get_type_option_names() {
136 return array('access_key', 'secret_key', 'pluginname');
139 public function type_config_form($mform) {
140 parent
::type_config_form($mform);
141 $strrequired = get_string('required');
142 $mform->addElement('text', 'access_key', get_string('access_key', 'repository_s3'));
143 $mform->addElement('text', 'secret_key', get_string('secret_key', 'repository_s3'));
144 $mform->addRule('access_key', $strrequired, 'required', null, 'client');
145 $mform->addRule('secret_key', $strrequired, 'required', null, 'client');
149 * S3 plugins doesn't support return links of files
153 public function supported_returntypes() {
154 return FILE_INTERNAL
;