/**
  * Save embedding information for a File, if applicable.
  *
  * Normally this event is called through File::saveNew()
  *
  * @param File   $file       The abount-to-be-inserted File object.
  *
  * @return boolean success
  */
 public function onStartFileSaveNew(File &$file)
 {
     // save given URL as title if it's a media file this plugin understands
     // which will make it shown in the AttachmentList widgets
     if (isset($file->title) && strlen($file->title) > 0) {
         // Title is already set
         return true;
     }
     if (!isset($file->mimetype)) {
         // Unknown mimetype, it's not our job to figure out what it is.
         return true;
     }
     switch (common_get_mime_media($file->mimetype)) {
         case 'image':
             // Just to set something for now at least...
             $file->title = $file->mimetype;
             break;
     }
     return true;
 }
Example #2
0
 public static function fromFileObject(File $file)
 {
     $imgPath = null;
     $media = common_get_mime_media($file->mimetype);
     if (Event::handle('CreateFileImageThumbnailSource', array($file, &$imgPath, $media))) {
         if (empty($file->filename) && !file_exists($imgPath)) {
             throw new UnsupportedMediaException(_('File without filename could not get a thumbnail source.'));
         }
         // First some mimetype specific exceptions
         switch ($file->mimetype) {
             case 'image/svg+xml':
                 throw new UseFileAsThumbnailException($file->id);
         }
         // And we'll only consider it an image if it has such a media type
         switch ($media) {
             case 'image':
                 $imgPath = $file->getPath();
                 break;
             default:
                 throw new UnsupportedMediaException(_('Unsupported media format.'), $file->getPath());
         }
     }
     if (!file_exists($imgPath)) {
         throw new ServerException(sprintf('Image not available locally: %s', $imgPath));
     }
     try {
         $image = new ImageFile($file->id, $imgPath);
     } catch (UnsupportedMediaException $e) {
         // Avoid deleting the original
         if ($imgPath != $file->getPath()) {
             unlink($imgPath);
         }
         throw $e;
     }
     return $image;
 }
 function showRepresentation()
 {
     if (Event::handle('StartShowAttachmentRepresentation', array($this->out, $this->attachment))) {
         if (!empty($this->attachment->mimetype)) {
             $mediatype = common_get_mime_media($this->attachment->mimetype);
             // FIXME: Get proper mime recognition of Ogg files! If system has 'mediainfo', this should do it:
             // $ mediainfo --inform='General;%InternetMediaType%'
             if ($this->attachment->mimetype === 'application/ogg') {
                 $mediatype = 'video';
                 // because this element can handle Ogg/Vorbis etc. on its own
             }
             switch ($mediatype) {
                 // Anything we understand as an image, if we need special treatment, do it in StartShowAttachmentRepresentation
                 case 'image':
                     try {
                         // Tell getThumbnail that we can show an animated image if it has one (4th arg, "force_still")
                         $thumb = $this->attachment->getThumbnail(null, null, false, false);
                         $this->out->element('img', array('class' => 'u-photo', 'src' => $thumb->getUrl(), 'alt' => ''));
                     } catch (UseFileAsThumbnailException $e) {
                         $this->out->element('img', array('class' => 'u-photo', 'src' => $e->file->getUrl(), 'alt' => $e->file->title));
                     } catch (UnsupportedMediaException $e) {
                         // FIXME: Show a good representation of unsupported/unshowable images
                     }
                     break;
                     // HTML5 media elements
                 // HTML5 media elements
                 case 'audio':
                 case 'video':
                     try {
                         $thumb = $this->attachment->getThumbnail();
                         $poster = $thumb->getUrl();
                         unset($thumb);
                     } catch (Exception $e) {
                         $poster = null;
                     }
                     $this->out->elementStart($mediatype, array('class' => "attachment_player u-{$mediatype}", 'poster' => $poster, 'controls' => 'controls'));
                     $this->out->element('source', array('src' => $this->attachment->getUrl(), 'type' => $this->attachment->mimetype));
                     $this->out->elementEnd($mediatype);
                     break;
                 default:
                     switch ($this->attachment->mimetype) {
                         case 'text/html':
                             if (!empty($this->attachment->filename) && (GNUsocial::isAjax() || common_config('attachments', 'show_html'))) {
                                 // Locally-uploaded HTML. Scrub and display inline.
                                 $this->showHtmlFile($this->attachment);
                                 break;
                             }
                             // Fall through to default if it wasn't a _local_ text/html File object
                         // Fall through to default if it wasn't a _local_ text/html File object
                         default:
                             Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
                     }
             }
         } else {
             Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
         }
     }
     Event::handle('EndShowAttachmentRepresentation', array($this->out, $this->attachment));
 }
Example #4
0
 /**
  * Attempt to identify the content type of a given file.
  * 
  * @param string $filepath filesystem path as string (file must exist)
  * @param string $originalFilename (optional) for extension-based detection
  * @return string
  * 
  * @fixme this seems to tie a front-end error message in, kinda confusing
  * 
  * @throws ClientException if type is known, but not supported for local uploads
  */
 static function getUploadedMimeType($filepath, $originalFilename = false)
 {
     // We only accept filenames to existing files
     $mimelookup = new finfo(FILEINFO_MIME_TYPE);
     $mimetype = $mimelookup->file($filepath);
     // Unclear types are such that we can't really tell by the auto
     // detect what they are (.bin, .exe etc. are just "octet-stream")
     $unclearTypes = array('application/octet-stream', 'application/vnd.ms-office', 'application/zip', 'text/html', 'text/xml');
     $supported = common_config('attachments', 'supported');
     // If we didn't match, or it is an unclear match
     if ($originalFilename && (!$mimetype || in_array($mimetype, $unclearTypes))) {
         try {
             $type = common_supported_ext_to_mime($originalFilename);
             return $type;
         } catch (Exception $e) {
             // Extension not found, so $mimetype is our best guess
         }
     }
     // If $config['attachments']['supported'] equals boolean true, accept any mimetype
     if ($supported === true || array_key_exists($mimetype, $supported)) {
         // FIXME: Don't know if it always has a mimetype here because
         // finfo->file CAN return false on error: http://php.net/finfo_file
         // so if $supported === true, this may return something unexpected.
         return $mimetype;
     }
     // We can conclude that we have failed to get the MIME type
     $media = common_get_mime_media($mimetype);
     if ('application' !== $media) {
         // TRANS: Client exception thrown trying to upload a forbidden MIME type.
         // TRANS: %1$s is the file type that was denied, %2$s is the application part of
         // TRANS: the MIME type that was denied.
         $hint = sprintf(_('"%1$s" is not a supported file type on this server. ' . 'Try using another %2$s format.'), $mimetype, $media);
     } else {
         // TRANS: Client exception thrown trying to upload a forbidden MIME type.
         // TRANS: %s is the file type that was denied.
         $hint = sprintf(_('"%s" is not a supported file type on this server.'), $mimetype);
     }
     throw new ClientException($hint);
 }