]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/File.php
think I have managed to show oEmbed images better now
[quix0rs-gnu-social.git] / classes / File.php
index 24c4e6a232509d00459d1200e522639c11951757..2390f848de28f420abb06dd211c237ea32467406 100644 (file)
@@ -55,7 +55,7 @@ class File extends Managed_DataObject
                 'title' => array('type' => 'text', 'description' => 'title of resource when available'),
                 'date' => array('type' => 'int', 'description' => 'date of resource according to http query'),
                 'protected' => array('type' => 'int', 'description' => 'true when URL is private (needs login)'),
-                'filename' => array('type' => 'text', 'description' => 'if a local file, name of the file'),
+                'filename' => array('type' => 'text', 'description' => 'if file is stored locally (too) this is the filename'),
                 'width' => array('type' => 'int', 'description' => 'width in pixels, if it can be described as such and data is available'),
                 'height' => array('type' => 'int', 'description' => 'height in pixels, if it can be described as such and data is available'),
 
@@ -356,28 +356,47 @@ class File extends Managed_DataObject
         return $protocol.'://'.$server.$path.$filename;
     }
 
+    static $_enclosures = array();
+
     function getEnclosure(){
+        if (isset(self::$_enclosures[$this->getID()])) {
+            common_debug('Found cached enclosure for file id=='.$this->getID());
+            return self::$_enclosures[$this->getID()];
+        }
+
         $enclosure = (object) array();
         foreach (array('title', 'url', 'date', 'modified', 'size', 'mimetype') as $key) {
             $enclosure->$key = $this->$key;
         }
 
-        $needMoreMetadataMimetypes = array(null, 'application/xhtml+xml');
+        $needMoreMetadataMimetypes = array(null, 'application/xhtml+xml', 'text/html');
 
         if (!isset($this->filename) && in_array(common_bare_mime($enclosure->mimetype), $needMoreMetadataMimetypes)) {
             // This fetches enclosure metadata for non-local links with unset/HTML mimetypes,
             // which may be enriched through oEmbed or similar (implemented as plugins)
             Event::handle('FileEnclosureMetadata', array($this, &$enclosure));
         }
-        if (empty($enclosure->mimetype) || in_array(common_bare_mime($enclosure->mimetype), $needMoreMetadataMimetypes)) {
+        if (empty($enclosure->mimetype)) {
             // This means we either don't know what it is, so it can't
             // be shown as an enclosure, or it is an HTML link which
             // does not link to a resource with further metadata.
             throw new ServerException('Unknown enclosure mimetype, not enough metadata');
         }
+
+        self::$_enclosures[$this->getID()] = $enclosure;
         return $enclosure;
     }
 
+    public function hasThumbnail()
+    {
+        try {
+            $this->getThumbnail();
+        } catch (Exception $e) {
+            return false;
+        }
+        return true;
+    }
+
     /**
      * Get the attachment's thumbnail record, if any.
      * Make sure you supply proper 'int' typed variables (or null).
@@ -416,21 +435,11 @@ class File extends Managed_DataObject
         return $filepath;
     }
 
-    public function getUrl()
+    public function getUrl($prefer_local=true)
     {
-        if (!empty($this->filename)) {
+        if ($prefer_local && !empty($this->filename)) {
             // A locally stored file, so let's generate a URL for our instance.
-            $url = self::url($this->filename);
-            if (self::hashurl($url) !== $this->urlhash) {
-                // For indexing purposes, in case we do a lookup on the 'url' field.
-                // also we're fixing possible changes from http to https, or paths
-                try {
-                       $this->updateUrl($url);
-                } catch (ServerException $e) {
-                       //
-                }      
-            }
-            return $url;
+            return self::url($this->filename);
         }
 
         // No local filename available, return the URL we have stored
@@ -660,4 +669,4 @@ class File extends Managed_DataObject
         echo "DONE.\n";
         echo "Resuming core schema upgrade...";
     }
-}
\ No newline at end of file
+}