forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
155 lines (140 loc) · 4.74 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?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 is a repository class used to browse Amazon S3 content.
*
* @since 2.0
* @package moodlecore
* @subpackage repository
* @copyright 2009 Dongsheng Cai
* @author Dongsheng Cai <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('S3.php');
class repository_s3 extends repository {
/**
* Constructor
* @param int $repositoryid
* @param object $context
* @param array $options
*/
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
parent::__construct($repositoryid, $context, $options);
$this->access_key = get_config('s3', 'access_key');
$this->secret_key = get_config('s3', 'secret_key');
$this->s = new S3($this->access_key, $this->secret_key);
}
/**
* Get S3 file list
*
* @param string $path
* @return array The file list and options
*/
public function get_listing($path = '') {
global $CFG, $OUTPUT;
if (empty($this->access_key)) {
die(json_encode(array('e'=>get_string('needaccesskey', 'repository_s3'))));
}
$list = array();
$list['list'] = array();
// the management interface url
$list['manage'] = false;
// dynamically loading
$list['dynload'] = true;
// the current path of this list.
// set to true, the login link will be removed
$list['nologin'] = true;
// set to true, the search button will be removed
$list['nosearch'] = true;
$tree = array();
if (empty($path)) {
$buckets = $this->s->listBuckets();
foreach ($buckets as $bucket) {
$folder = array(
'title' => $bucket,
'children' => array(),
'thumbnail'=>$OUTPUT->pix_url('f/folder-32'),
'path'=>$bucket
);
$tree[] = $folder;
}
} else {
$contents = $this->s->getBucket($path);
foreach ($contents as $file) {
$info = $this->s->getObjectInfo($path, baseName($file['name']));
$tree[] = array(
'title'=>$file['name'],
'size'=>$file['size'],
'date'=>userdate($file['time']),
'source'=>$path.'/'.$file['name'],
'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file['name'], 32))
);
}
}
$list['list'] = $tree;
return $list;
}
/**
* Download S3 files to moodle
*
* @param string $filepath
* @param string $file The file path in moodle
* @return array The local stored path
*/
public function get_file($filepath, $file) {
global $CFG;
$arr = explode('/', $filepath);
$bucket = $arr[0];
$filename = $arr[1];
$path = $this->prepare_file($file);
$this->s->getObject($bucket, $filename, $path);
return array('path'=>$path);
}
/**
* S3 doesn't require login
*
* @return bool
*/
public function check_login() {
return true;
}
/**
* S3 doesn't provide search
*
* @return bool
*/
public function global_search() {
return false;
}
public static function get_type_option_names() {
return array('access_key', 'secret_key');
}
public function type_config_form($mform) {
$strrequired = get_string('required');
$mform->addElement('text', 'access_key', get_string('access_key', 'repository_s3'));
$mform->addElement('text', 'secret_key', get_string('secret_key', 'repository_s3'));
$mform->addRule('access_key', $strrequired, 'required', null, 'client');
$mform->addRule('secret_key', $strrequired, 'required', null, 'client');
}
/**
* S3 plugins doesn't support return links of files
*
* @return int
*/
public function supported_returntypes() {
return FILE_INTERNAL;
}
}