"MDL-13766, use bigger icons for file picker"
[moodle.git] / repository / s3 / repository.class.php
1 <?php // $Id$
2 require_once('S3.php');
4 class repository_s3 extends repository {
5     public function __construct($repositoryid, $context = SITEID, $options = array()) {
6         parent::__construct($repositoryid, $context, $options);
7         $this->access_key = get_config('s3', 'access_key');
8         $this->secret_key = get_config('s3', 'secret_key');
9         if (empty($this->access_key)) {
10             die(json_encode(array('e'=>get_string('repository_s3', 'needaccesskey'))));
11         }
12         $this->s = new S3($this->access_key, $this->secret_key);
13     }
14     public function get_listing($path = '') {
15         global $CFG;
16         $list = array();
17         $list['list'] = array();
18         // the management interface url
19         $list['manage'] = false;
20         // dynamically loading
21         $list['dynload'] = true;
22         // the current path of this list.
23         // set to true, the login link will be removed
24         $list['nologin'] = true;
25         // set to true, the search button will be removed
26         $list['nosearch'] = true;
27         $tree = array();
28         if (empty($path)) {
29             $buckets = $this->s->listBuckets();
30             foreach ($buckets as $bucket) {
31                 $folder = array(
32                     'title' => $bucket,
33                     'children' => array(),
34                     'thumbnail'=>$CFG->pixpath.'/f/folder-32.png',
35                     'path'=>$bucket
36                     );
37                 $tree[] = $folder;
38             }
39         } else {
40             $contents = $this->s->getBucket($path);
41             foreach ($contents as $file) {
42                 $info = $this->s->getObjectInfo($path, baseName($file['name']));
43                 $tree[] = array(
44                     'title'=>$file['name'],
45                     'size'=>$file['size'],
46                     'date'=>userdate($file['time']),
47                     'source'=>$path.'/'.$file['name'],
48                     'thumbnail'=>$CFG->pixpath.'/f/'.mimeinfo('icon32', $file['name'])
49                     );
50             }
51         }
53         $list['list'] = $tree;
55         return $list;
56     }
57     public function get_file($filepath, $file) {
58         global $CFG;
59         $arr = explode('/', $filepath);
60         $bucket   = $arr[0];
61         $filename = $arr[1];
62         if (!file_exists($CFG->dataroot.'/repository/download')) {
63             mkdir($CFG->dataroot.'/repository/download/', 0777, true);
64         }
65         if(is_dir($CFG->dataroot.'/repository/download')) {
66             $dir = $CFG->dataroot.'/repository/download/';
67         }
68         $this->s->getObject($bucket, $filename, $dir.$file);
69         return $dir.$file;
70     }
71     // login 
72     public function check_login() {
73         return true;
74     }
75     public function global_search() {
76         return false;
77     }
78     public static function get_type_option_names() {
79         return array('access_key', 'secret_key');
80     }
81     public function type_config_form(&$mform) {
82         $strrequired = get_string('required');
83         $mform->addElement('text', 'access_key', get_string('access_key', 'repository_s3'));
84         $mform->addElement('text', 'secret_key', get_string('secret_key', 'repository_s3'));
85         $mform->addRule('access_key', $strrequired, 'required', null, 'client');
86         $mform->addRule('secret_key', $strrequired, 'required', null, 'client');
87         return true;
88     }
89 }