Skip to content

Commit 04f7443

Browse files
author
anisa kusumadewi
committed
add exceptions
1 parent 1a11b2a commit 04f7443

File tree

4 files changed

+55
-29
lines changed

4 files changed

+55
-29
lines changed

lang/en/pdfannotator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,15 @@
102102
$string['error:deleteAnnotation'] = 'An error has occured while deleting an annotation.';
103103
$string['error:editAnnotation'] = 'An error has occurred while editing an annotation.';
104104
$string['error:editcomment'] = 'An error has occured while trying to edit a comment.';
105+
$string['error:findimage'] = 'An error occured while trying to find image {$a}.';
105106
$string['error:forwardquestion'] = 'An error has occured while forwarding the question.';
106107
$string['error:forwardquestionnorecipient'] = 'An error has occured while forwarding the question.: No person in this course has the capability to receive forwarded questions.';
107108
$string['error:getAllQuestions'] = 'An error has occured while getting the questions of this document.';
108109
$string['error:getAnnotation'] = 'An error has occured while getting the annotation.';
109110
$string['error:getAnnotations'] = 'An error has occured while getting all annotations.';
110111
$string['error:getComments'] = 'An error has occured while getting the comments.';
112+
$string['error:getimageheight'] = 'An error has occured while getting image height of {$a}.';
113+
$string['error:getimageheight'] = 'An error has occured while getting image width of {$a}.';
111114
$string['error:getQuestions'] = 'An error has occured while getting the questions for this page.';
112115
$string['error:printComments'] = 'Error with data from server.';
113116
$string['error:hideComment'] = "An error has occured while trying to hide the comment from participants' view.";

locallib.php

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function pdfannotator_display_embed($pdfannotator, $cm, $course, $file, $page =
6464
// Load and execute the javascript files.
6565
$PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/pdf.js?ver=00002"));
6666
$PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/textclipper.js"));
67-
$PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/index.js?ver=00035"));
67+
$PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/index.js?ver=00036"));
6868
$PAGE->requires->js(new moodle_url("/mod/pdfannotator/shared/locallib.js?ver=00006"));
6969

7070
// Pass parameters from PHP to JavaScript.
@@ -210,42 +210,65 @@ function pdfannotator_split_content_image($content, $res, $itemid, $context=null
210210
}
211211

212212
$tempinfo = [];
213+
$encodedurl = urldecode($url[0]);
213214
foreach($fileinfo as $file) {
214-
$count = substr_count(urldecode($url[0]), $file['filename']);
215+
$count = substr_count($encodedurl, $file['filename']);
215216
if($count) {
216217
$tempinfo = $file;
217218
break;
218219
}
219220
}
220221

221-
if($tempinfo) {
222-
$imagedata = 'data:' . $tempinfo['filemimetype'] . ';base64,' . base64_encode($tempinfo['filecontent']);
223-
$data['image'] = $imagedata;
224-
$data['format'] = $tempinfo['filemimetype'];
225-
$data['fileid'] = $tempinfo['fileid'];
226-
$data['filename'] = $tempinfo['filename'];
227-
$data['filepath'] = $tempinfo['filepath'];
228-
$data['filesize'] = $tempinfo['filesize'];
229-
$data['imagestorage'] = 'intern';
222+
try {
223+
if($tempinfo) {
224+
$imagedata = 'data:' . $tempinfo['filemimetype'] . ';base64,' . base64_encode($tempinfo['filecontent']);
225+
$data['image'] = $imagedata;
226+
$data['format'] = $tempinfo['filemimetype'];
227+
$data['fileid'] = $tempinfo['fileid'];
228+
$data['filename'] = $tempinfo['filename'];
229+
$data['filepath'] = $tempinfo['filepath'];
230+
$data['filesize'] = $tempinfo['filesize'];
231+
$data['imagestorage'] = 'intern';
232+
} else if (!str_contains($CFG->wwwroot, $url[0])){
233+
$data['imagestorage'] = 'extern';
234+
$data['format'] = $format[0];
235+
$imgcontent = @file_get_contents($url[0]);
236+
if ($imgcontent) {
237+
$data['image'] = 'data:image/' . $format[0] . ";base64," . base64_encode($imgcontent);
238+
} else {
239+
throw new Exception(get_string('error:findimage', 'pdfannotator', $encodedurl));
240+
}
241+
} else {
242+
throw new Exception(get_string('error:findimage', 'pdfannotator', $encodedurl));
243+
}
244+
230245
preg_match('/height=[0-9]+/', $imgstr, $height);
231-
$data['imageheight'] = str_replace("\"", "", explode('=', $height[0])[1]);
246+
if ($height) {
247+
$data['imageheight'] = str_replace("\"", "", explode('=', $height[0])[1]);
248+
} else if (!$height && $data['imagestorage'] === 'extern') {
249+
$imagemetadata = getimagesize($url[0]);
250+
$data['imageheight'] = $imagemetadata[1];
251+
} else {
252+
throw new Exception(get_string('error:getimageheight', 'pdfannotator', $encodedurl));
253+
}
232254
preg_match('/width=[0-9]+/', $imgstr, $width);
233-
$data['imagewidth'] = str_replace("\"", "", explode('=', $width[0])[1]);
234-
} else if (!str_contains($CFG->wwwroot, $url[0])){
235-
$data['imagestorage'] = 'extern';
236-
$data['format'] = $format[0];
237-
$imagemetadata = getimagesize($url[0]);
238-
$data['image'] = 'data:image/' . $format[0] . ";base64," . base64_encode(file_get_contents($url[0]));
239-
$data['imagewidth'] = $imagemetadata[0];
240-
$data['imageheight'] = $imagemetadata[1];
241-
} else {
242-
$data['success'] = "error";
243-
$data['message'] = "cannot load image";
255+
if ($width) {
256+
$data['imagewidth'] = str_replace("\"", "", explode('=', $width[0])[1]);
257+
} else if (!$width && $data['imagestorage'] === 'extern') {
258+
$imagemetadata = getimagesize($url[0]);
259+
$data['imagewidth'] = $imagemetadata[0];
260+
} else {
261+
throw new Exception(get_string('error:getimagewidth', 'pdfannotator', $encodedurl));
262+
}
263+
} catch (Exception $ex) {
264+
$data['image'] = "error";
265+
$data['message'] = $ex->getMessage();
266+
} finally {
267+
$res[] = $firststr;
268+
$res[] = $data;
269+
$content = $laststr;
244270
}
245271

246-
$res[] = $firststr;
247-
$res[] = $data;
248-
$content = $laststr;
249272
}
250273
$res[] = $content;
251274

shared/index.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
defined('MOODLE_INTERNAL') || die();
2626

2727
$plugin->component = 'mod_pdfannotator';
28-
$plugin->version = 2022110900;
28+
$plugin->version = 2022110902;
2929
$plugin->release = 'PDF Annotator v1.4 release 11';
3030
$plugin->requires = 2021051700;
3131
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)