|
23 | 23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
24 | 24 | */
|
25 | 25 |
|
26 |
| -use tool_objectfs\local\manager; |
27 |
| -use tool_objectfs\local\report\objectfs_report; |
28 |
| -use tool_objectfs\local\store\object_file_system; |
29 |
| - |
30 | 26 | class tool_objectfs_renderer extends plugin_renderer_base {
|
31 | 27 |
|
32 |
| - /** |
33 |
| - * Delete test files from files table |
34 |
| - * @throws coding_exception |
35 |
| - * @throws dml_exception |
36 |
| - */ |
37 |
| - public function delete_presignedurl_tests_files() { |
38 |
| - $filestorage = get_file_storage(); |
39 |
| - $filesarea = $filestorage->get_area_files( |
40 |
| - \context_system::instance()->id, |
41 |
| - OBJECTFS_PLUGIN_NAME, |
42 |
| - 'settings', |
43 |
| - 0 |
44 |
| - ); |
45 |
| - foreach ($filesarea as $testfile) { |
46 |
| - if ('.' === $testfile->get_filename()) { |
47 |
| - continue; |
48 |
| - } |
49 |
| - $testfile->delete(); |
50 |
| - } |
51 |
| - } |
52 |
| - |
53 |
| - public function presignedurl_tests_load_files($fs) { |
54 |
| - global $CFG; |
55 |
| - $filestorage = get_file_storage(); |
56 |
| - $fixturespath = $CFG->dirroot.'/admin/tool/objectfs/tests/fixtures/'; |
57 |
| - $fixturesfiles = glob($fixturespath.'*'); |
58 |
| - $syscontext = \context_system::instance(); |
59 |
| - |
60 |
| - foreach ($fixturesfiles as $fixturesfile) { |
61 |
| - // Filter out possible compressed files. |
62 |
| - if (false !== strpos($fixturesfile, '.br')) { |
63 |
| - continue; |
64 |
| - } |
65 |
| - $testfilename = str_replace($fixturespath, '', $fixturesfile); |
66 |
| - |
67 |
| - $contextid = $syscontext->id; |
68 |
| - $component = 'tool_objectfs'; |
69 |
| - $filearea = 'settings'; |
70 |
| - $itemid = 0; |
71 |
| - $filepath = '/'; |
72 |
| - |
73 |
| - $filerecord = array( |
74 |
| - 'contextid' => $contextid, |
75 |
| - 'component' => $component, |
76 |
| - 'filearea' => $filearea, |
77 |
| - 'itemid' => $itemid, |
78 |
| - 'filepath' => $filepath, |
79 |
| - 'filename' => $testfilename |
80 |
| - ); |
81 |
| - |
82 |
| - $testfile = $filestorage->get_file($contextid, $component, $filearea, $itemid, $filepath, $testfilename); |
83 |
| - if (!$testfile) { |
84 |
| - $testfile = $filestorage->create_file_from_pathname($filerecord, $fixturesfile); |
85 |
| - } |
86 |
| - |
87 |
| - $contenthash = $testfile->get_contenthash(); |
88 |
| - $readable = $fs->is_file_readable_externally_by_hash($contenthash); |
89 |
| - if (!$readable) { |
90 |
| - $fs->copy_from_local_to_external($contenthash); |
91 |
| - } |
92 |
| - $testfiles[] = $testfile; |
93 |
| - } |
94 |
| - |
95 |
| - return $testfiles; |
96 |
| - } |
97 |
| - |
98 |
| - public function presignedurl_tests_content($fs, $testfiles) { |
99 |
| - global $CFG; |
100 |
| - $CFG->enablepresignedurls = true; |
101 |
| - $output = ''; |
102 |
| - |
103 |
| - $output .= $this->box(''); |
104 |
| - $output .= $this->heading(get_string('presignedurl_testing:test1', 'tool_objectfs'), 4); |
105 |
| - foreach ($testfiles as $file) { |
106 |
| - $presignedurl = $this->generate_file_url($file, false, true); |
107 |
| - $output .= $this->heading($this->get_output($fs, $presignedurl, $file, 'downloadfile'), 5); |
108 |
| - } |
109 |
| - |
110 |
| - $output .= $this->box(''); |
111 |
| - $output .= $this->heading(get_string('presignedurl_testing:test2', 'tool_objectfs'), 4); |
112 |
| - foreach ($testfiles as $file) { |
113 |
| - $presignedurl = $this->generate_file_url($file, false, true); |
114 |
| - |
115 |
| - $output .= $this->heading($this->get_output($fs, $presignedurl, $file, 'downloadfile'), 5); |
116 |
| - } |
117 |
| - |
118 |
| - $output .= $this->box(''); |
119 |
| - $output .= $this->heading(get_string('presignedurl_testing:test3', 'tool_objectfs'), 4); |
120 |
| - foreach ($testfiles as $file) { |
121 |
| - $presignedurl = $this->generate_file_url($file); |
122 |
| - |
123 |
| - $output .= $this->heading($this->get_output($fs, $presignedurl, $file, 'openinbrowser'), 5); |
124 |
| - } |
125 |
| - |
126 |
| - $output .= $this->box(''); |
127 |
| - $output .= $this->heading(get_string('presignedurl_testing:test4', 'tool_objectfs'), 4); |
128 |
| - foreach ($testfiles as $file) { |
129 |
| - $presignedurl = $this->generate_file_url($file); |
130 |
| - |
131 |
| - $outputstring = '"'.$file->get_filename().'" '.get_string('presignedurl_testing:fileiniframe', 'tool_objectfs').':'; |
132 |
| - $output .= $this->heading($outputstring, 5); |
133 |
| - |
134 |
| - $output .= $this->box($this->get_output($fs, $presignedurl, $file, 'iframesnotsupported')); |
135 |
| - $output .= $this->box(''); |
136 |
| - } |
137 |
| - |
138 |
| - $output .= $this->box(''); |
139 |
| - $output .= $this->heading(get_string('presignedurl_testing:test5', 'tool_objectfs'), 4); |
140 |
| - // Expires in seconds. |
141 |
| - $testexpirefiles = ['testimage.png' => 0, 'testlarge.pdf' => 10, 'test.txt' => -1]; |
142 |
| - foreach ($testfiles as $key => $file) { |
143 |
| - $filename = $file->get_filename(); |
144 |
| - if (!isset($testexpirefiles[$filename])) { |
145 |
| - continue; |
146 |
| - } |
147 |
| - $presignedurl = $this->generate_file_url($file, $testexpirefiles[$filename]); |
148 |
| - |
149 |
| - $outputstring = '"' . $filename . '" '. |
150 |
| - get_string('presignedurl_testing:fileiniframe', OBJECTFS_PLUGIN_NAME) . ':'; |
151 |
| - $output .= $this->heading($outputstring, 5); |
152 |
| - |
153 |
| - $output .= $this->box($this->get_output($fs, $presignedurl, $file, 'iframesnotsupported')); |
154 |
| - $output .= $this->box(''); |
155 |
| - } |
156 |
| - |
157 |
| - return $output; |
158 |
| - } |
159 |
| - |
160 |
| - /** |
161 |
| - * Generate a file url with adding a param to set 'Expires' header. |
162 |
| - * @param stored_file $file |
163 |
| - * @param int|bool $expires |
164 |
| - * @param bool $forcedownload |
165 |
| - * @return string |
166 |
| - * @throws dml_exception |
167 |
| - */ |
168 |
| - private function generate_file_url($file, $expires = false, $forcedownload = false) { |
169 |
| - $url = \moodle_url::make_pluginfile_url( |
170 |
| - \context_system::instance()->id, |
171 |
| - OBJECTFS_PLUGIN_NAME, |
172 |
| - 'settings', |
173 |
| - 0, |
174 |
| - '/', |
175 |
| - $file->get_filename(), |
176 |
| - $forcedownload |
177 |
| - ); |
178 |
| - $expires = (-1 !== $expires) ? $expires : false; |
179 |
| - if (false !== $expires) { |
180 |
| - $url->param('expires', $expires); |
181 |
| - } |
182 |
| - return $url->out(); |
183 |
| - } |
184 |
| - |
185 |
| - /** |
186 |
| - * Generates the output string that contains the presignedurl or local url. |
187 |
| - * @param object_file_system $fs |
188 |
| - * @param string $url |
189 |
| - * @param stored_file $file |
190 |
| - * @param string $identifier |
191 |
| - * @return string |
192 |
| - * @throws coding_exception |
193 |
| - */ |
194 |
| - private function get_output($fs, $url, $file, $identifier) { |
195 |
| - $icon = $this->output->pix_icon('i/grade_correct', '', 'moodle', ['class' => 'icon']); |
196 |
| - $redirect = $icon . 'Redirecting to external storage: '; |
197 |
| - if (!$fs->presigned_url_should_redirect($file->get_contenthash())) { |
198 |
| - $redirect = $this->output->pix_icon('i/grade_incorrect', '', 'moodle', ['class' => 'icon']) . 'Not redirecting: '; |
199 |
| - } |
200 |
| - $output = get_string('presignedurl_testing:' . $identifier, 'tool_objectfs') . ': '. |
201 |
| - '<a href="'. $url .'">'. $file->get_filename() . '</a>'; |
202 |
| - if ('iframesnotsupported' === $identifier) { |
203 |
| - $output = '<iframe height="400" width="100%" src="' . $url . '">'. |
204 |
| - get_string('presignedurl_testing:' . $identifier, 'tool_objectfs').'</iframe>'; |
205 |
| - } |
206 |
| - return $output . '<br><small>' . $redirect . $url . '</small>';; |
207 |
| - } |
208 |
| - |
209 | 28 | /**
|
210 | 29 | * Returns a header for Object status history page.
|
211 | 30 | *
|
|
0 commit comments