/**
* Include $filepath in the response, for viewing and downloading
*/
- static function sendFile(string $filepath, int $filesize) {
+ static function sendFile(string $filepath, $filesize) {
if (common_config('site', 'use_x_sendfile')) {
header('X-Sendfile: ' . $filepath);
} else {
public function showPage()
{
- // Checks file exists or throws FileNotFoundException
- $size = $this->attachment->size ?: 0;
// Returns a File_thumbnail object or throws exception if not available
try {
$this->clientError(_('No such attachment'), 404);
}
- $filepath = $file->getFileOrThumbnailPath();
- $mimetype = $file->getFileOrThumbnailMimetype();
+ // Checks file exists or throws FileNotFoundException
+ $filepath = $file->getFileOrThumbnailPath($thumbnail);
+ $filesize = $this->attachment->getFileOrThumbnailSize($thumbnail);
+ $mimetype = $file->getFileOrThumbnailMimetype($thumbnail);
$filename = MediaFile::getDisplayName($file);
// Disable errors, to not mess with the file contents (suppress errors in case access to this
* @throws InvalidFilenameException
* @throws ServerException
*/
- public function getFileOrThumbnailPath() : string
+ public function getFileOrThumbnailPath($thumbnail = null) : string
{
+ if (!empty($thumbnail)) {
+ return $thumbnail->getPath();
+ }
if (!empty($this->filename)) {
$filepath = self::path($this->filename);
if (file_exists($filepath)) {
* @throws ServerException
* @throws UnsupportedMediaException
*/
- public function getFileOrThumbnailMimetype() : string
+ public function getFileOrThumbnailMimetype($thumbnail = null) : string
{
- if (empty($this->filename)) {
+ if (!empty($thumbnail)) {
+ $filepath = $thumbnail->getPath();
+ } elseif (empty($this->filename)) {
$filepath = File_thumbnail::byFile($this)->getPath();
- $info = @getimagesize($filepath);
- if ($info !== false) {
- return $info['mime'];
- } else {
- throw new UnsupportedMediaException(_("Thumbnail is not an image."));
- }
} else {
return $this->mimetype;
}
+
+ $info = @getimagesize($filepath);
+ if ($info !== false) {
+ return $info['mime'];
+ } else {
+ throw new UnsupportedMediaException(_("Thumbnail is not an image."));
+ }
+ }
+
+ /**
+ * Return the size of the thumbnail if we have it, or, if not, of the File
+ * @return int
+ * @throws FileNotFoundException
+ * @throws NoResultException
+ * @throws ServerException
+ */
+ public function getFileOrThumbnailSize($thumbnail = null) : int
+ {
+ if (!empty($thumbnail)) {
+ return filesize($thumbnail->getPath());
+ } elseif (!empty($this->filename)) {
+ return $this->size;
+ } else {
+ return filesize(File_thumbnail::byFile($this)->getPath());
+ }
}
public function getAttachmentUrl()