X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=classes%2FFile.php;h=ce2f9401618286bcfd9485f8f3682add5c8df0f2;hb=561e098b538cfb2c90ed38ec6109ea4b879166bf;hp=029ff487a48e4fe561411f7e4a8dfa04f3c460dc;hpb=17b9614ff8ec0070f6ea8c7ac7d38167f862cde5;p=quix0rs-gnu-social.git diff --git a/classes/File.php b/classes/File.php index 029ff487a4..ce2f940161 100644 --- a/classes/File.php +++ b/classes/File.php @@ -303,7 +303,7 @@ class File extends Managed_DataObject } - if (StatusNet::isHTTPS()) { + if (StatusNet::useHTTPS()) { $sslserver = common_config('attachments', 'sslserver'); @@ -356,7 +356,7 @@ class File extends Managed_DataObject $enclosure->$key = $this->$key; } - $needMoreMetadataMimetypes = array(null, 'text/html', 'application/xhtml+xml'); + $needMoreMetadataMimetypes = array(null, 'application/xhtml+xml'); if (!isset($this->filename) && in_array(common_bare_mime($enclosure->mimetype), $needMoreMetadataMimetypes)) { // This fetches enclosure metadata for non-local links with unset/HTML mimetypes, @@ -382,13 +382,16 @@ class File extends Managed_DataObject * * @return File_thumbnail */ - public function getThumbnail($width=null, $height=null, $crop=false) + public function getThumbnail($width=null, $height=null, $crop=false, $force_still=true) { - if (intval($this->width) < 1 || intval($this->height) < 1) { - // Old files may have 0 until migrated with scripts/upgrade.php - // For any legitimately unrepresentable ones, we could generate our - // own image (like a square with MIME type in text) - throw new UnsupportedMediaException('No image geometry available.'); + // Get some more information about this file through our ImageFile class + $image = ImageFile::fromFileObject($this); + if ($image->animated && !common_config('thumbnail', 'animated')) { + // null means "always use file as thumbnail" + // false means you get choice between frozen frame or original when calling getThumbnail + if (is_null(common_config('thumbnail', 'animated')) || !$force_still) { + throw new UseFileAsThumbnailException($this->id); + } } if ($width === null) { @@ -405,7 +408,6 @@ class File extends Managed_DataObject // Get proper aspect ratio width and height before lookup // We have to do it through an ImageFile object because of orientation etc. // Only other solution would've been to rotate + rewrite uploaded files. - $image = ImageFile::fromFileObject($this); list($width, $height, $x, $y, $w, $h) = $image->scaleToFit($width, $height, $crop); @@ -418,7 +420,7 @@ class File extends Managed_DataObject } // throws exception on failure to generate thumbnail - $outname = "thumb-{$width}x{$height}-" . $this->filename; + $outname = "thumb-{$width}x{$height}-" . $image->filename; $outpath = self::path($outname); // The boundary box for our resizing @@ -429,18 +431,19 @@ class File extends Managed_DataObject // Doublecheck that parameters are sane and integers. if ($box['width'] < 1 || $box['width'] > common_config('thumbnail', 'maxsize') || $box['height'] < 1 || $box['height'] > common_config('thumbnail', 'maxsize') - || $box['w'] < 1 || $box['x'] >= $this->width - || $box['h'] < 1 || $box['y'] >= $this->height) { + || $box['w'] < 1 || $box['x'] >= $image->width + || $box['h'] < 1 || $box['y'] >= $image->height) { // Fail on bad width parameter. If this occurs, it's due to algorithm in ImageFile->scaleToFit - common_debug("Boundary box parameters for resize of {$this->filepath} : ".var_export($box,true)); + common_debug("Boundary box parameters for resize of {$image->filepath} : ".var_export($box,true)); throw new ServerException('Bad thumbnail size parameters.'); } + common_debug(sprintf('Generating a thumbnail of File id==%u of size %ux%u', $this->id, $width, $height)); // Perform resize and store into file $image->resizeTo($outpath, $box); // Avoid deleting the original - if ($image->getPath() != $this->getPath()) { + if ($image->getPath() != self::path($image->filename)) { $image->unlink(); } return File_thumbnail::saveThumbnail($this->id, @@ -453,11 +456,42 @@ class File extends Managed_DataObject { return self::path($this->filename); } + public function getUrl() { + if (!empty($this->filename)) { + // A locally stored file, so let's generate a URL for our instance. + $url = self::url($this->filename); + if ($url != $this->url) { + // 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 + $this->updateUrl($url); + } + return $url; + } + + // No local filename available, return the URL we have stored return $this->url; } + public function updateUrl($url) + { + $file = File::getKV('url', $url); + if ($file instanceof File) { + throw new ServerException('URL already exists in DB'); + } + $sql = 'UPDATE %1$s SET url=%2$s WHERE url=%3$s;'; + $result = $this->query(sprintf($sql, $this->__table, + $this->_quote((string)$url), + $this->_quote((string)$this->url))); + if ($result === false) { + common_log_db_error($this, 'UPDATE', __FILE__); + throw new ServerException("Could not UPDATE {$this->__table}.url"); + } + + return $result; + } + /** * Blow the cache of notices that link to this URL * @@ -541,4 +575,11 @@ class File extends Managed_DataObject // And finally remove the entry from the database return parent::delete($useWhere); } + + public function getTitle() + { + $title = $this->title ?: $this->filename; + + return $title ?: null; + } }