diff --git a/filesystem/File.php b/filesystem/File.php index 6712ee6f4..d8846fe7f 100644 --- a/filesystem/File.php +++ b/filesystem/File.php @@ -393,7 +393,6 @@ class File extends DataObject { return $fields; } - /** * Returns a category based on the file extension. * This can be useful when grouping files by type, @@ -402,13 +401,22 @@ class File extends DataObject { * * @return String */ - public function appCategory() { - $ext = strtolower($this->Extension); + public static function get_app_category($ext) { + $ext = strtolower($ext); foreach(self::$app_categories as $category => $exts) { if(in_array($ext, $exts)) return $category; } return false; } + + /** + * Returns a category based on the file extension. + * + * @return String + */ + public function appCategory() { + return self::get_app_category($this->Extension); + } function CMSThumbnail() { return ''; diff --git a/forms/HtmlEditorField.php b/forms/HtmlEditorField.php index 45a56d9f1..3afcaef81 100644 --- a/forms/HtmlEditorField.php +++ b/forms/HtmlEditorField.php @@ -515,7 +515,8 @@ class HtmlEditorField_Toolbar extends RequestHandler { } // Instanciate file wrapper and get fields based on its type - if($file && $file->appCategory() == 'image') { + // Check if appCategory is an image and exists on the local system, otherwise use oEmbed to refference a remote image + if($file && $file->appCategory() == 'image' && Director::is_site_url($url)) { $fileWrapper = new HtmlEditorField_Image($url, $file); } elseif(!Director::is_site_url($url)) { $fileWrapper = new HtmlEditorField_Embed($url, $file); diff --git a/oembed/Oembed.php b/oembed/Oembed.php index 7017a3d24..bdca6ac50 100644 --- a/oembed/Oembed.php +++ b/oembed/Oembed.php @@ -131,6 +131,13 @@ class Oembed { // Build the url manually - we gave all needed information. $oembedUrl = Controller::join_links($endpoint, '?format=json&url=' . rawurlencode($url)); } + + // If autodescovery failed the resource might be a direct link to a file + if(!$oembedUrl) { + if(File::get_app_category(File::get_file_extension($url)) == "image") { + return new Oembed_Result($url, $url, $type, $options); + } + } if($oembedUrl) { // Inject the options into the Oembed URL. @@ -233,7 +240,20 @@ class Oembed_Result extends ViewableData { $body = $body->getBody(); $data = json_decode($body, true); if(!$data) { + // if the response is no valid JSON we might have received a binary stream to an image $data = array(); + $image = @imagecreatefromstring($body); + if($image !== FALSE) { + preg_match("/^(http:\/\/)?([^\/]+)/i", $this->url, $matches); + $protocoll = $matches[1]; + $host = $matches[2]; + $data['type'] = "photo"; + $data['title'] = basename($this->url) . " ($host)"; + $data['url'] = $this->url; + $data['provider_url'] = $protocoll.$host; + $data['width'] = imagesx($image); + $data['height'] = imagesy($image); + } } // Convert all keys to lowercase