From e58db3a8fd2fea0b55af5cef6beb6f05005d6029 Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Tue, 9 Jun 2020 12:34:25 +0800 Subject: [PATCH 1/2] MDL-68954 repository_flickr_public: Handle when photo cannot be found --- repository/flickr_public/lib.php | 45 +++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/repository/flickr_public/lib.php b/repository/flickr_public/lib.php index 37be696bf59..1dd58b77e04 100644 --- a/repository/flickr_public/lib.php +++ b/repository/flickr_public/lib.php @@ -340,6 +340,8 @@ class repository_flickr_public extends repository { * @return array */ private function build_list($photos, $page = 1, &$ret) { + global $OUTPUT; + if (!empty($this->nsid)) { $photos_url = $this->flickr->urls_getUserPhotos($this->nsid); $ret['manage'] = $photos_url; @@ -371,16 +373,23 @@ class repository_flickr_public extends repository { // append file extension $p['title'] .= $format; } + // Get the thumbnail source URL. + $thumbnailsource = $this->flickr->buildPhotoURL($p, 'Square'); + if (!@getimagesize($thumbnailsource)) { + // Use the file extension icon as a thumbnail if the original thumbnail does not exist to avoid + // displaying broken thumbnails in the repository. + $thumbnailsource = $OUTPUT->image_url(file_extension_icon($p['title'], 90))->out(false); + } $ret['list'][] = array( - 'title'=>$p['title'], - 'source'=>$p['id'], - 'id'=>$p['id'], - 'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), - 'date'=>'', - 'size'=>'unknown', - 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id'], - 'haslicense'=>true, - 'hasauthor'=>true + 'title' => $p['title'], + 'source' => $p['id'], + 'id' => $p['id'], + 'thumbnail' => $thumbnailsource, + 'date' => '', + 'size' => 'unknown', + 'url' => 'http://www.flickr.com/photos/' . $p['owner'] . '/' . $p['id'], + 'haslicense' => true, + 'hasauthor' => true ); } } @@ -452,13 +461,8 @@ class repository_flickr_public extends repository { */ public function get_file($photoid, $file = '') { global $CFG; + $info = $this->flickr->photos_getInfo($photoid); - if ($info['owner']['realname']) { - $author = $info['owner']['realname']; - } else { - $author = $info['owner']['username']; - } - $copyright = get_string('author', 'repository') . ': ' . $author; // If we can read the original secret, it means that we have access to the original picture. if (isset($info['originalsecret'])) { @@ -466,6 +470,17 @@ class repository_flickr_public extends repository { } else { $source = $this->build_photo_url($photoid); } + // Make sure the source image exists. + if (!@getimagesize($source)) { + throw new moodle_exception('cannotdownload', 'repository'); + } + + if ($info['owner']['realname']) { + $author = $info['owner']['realname']; + } else { + $author = $info['owner']['username']; + } + $copyright = get_string('author', 'repository') . ': ' . $author; $result = parent::get_file($source, $file); $path = $result['path']; From e8017491d1594dfc749bec1d6432bfd523708866 Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Tue, 9 Jun 2020 12:36:59 +0800 Subject: [PATCH 2/2] MDL-68954 repository_flickr_public: Return only photo media --- repository/flickr_public/lib.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/repository/flickr_public/lib.php b/repository/flickr_public/lib.php index 1dd58b77e04..ad12ee28d9e 100644 --- a/repository/flickr_public/lib.php +++ b/repository/flickr_public/lib.php @@ -296,13 +296,15 @@ class repository_flickr_public extends repository { $text = !empty($SESSION->{$this->sess_text}) ? $SESSION->{$this->sess_text} : null; $nsid = !empty($this->nsid) ? $this->nsid : null; - $photos = $this->flickr->photos_search(array( - 'tags'=>$tag, - 'page'=>$page, - 'per_page'=>24, - 'user_id'=>$nsid, - 'license'=>$licenses, - 'text'=>$text + $photos = $this->flickr->photos_search( + array( + 'tags' => $tag, + 'page' => $page, + 'per_page' => 24, + 'user_id' => $nsid, + 'license' => $licenses, + 'text' => $text, + 'media' => 'photos' ) ); $ret['total'] = $photos['total'];