Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/markup/PhabricatorEmbedFileRemarkupRule.php
Show All 37 Lines | protected function renderObjectEmbed( | ||||
$options) { | $options) { | ||||
$options = $this->getFileOptions($options) + array( | $options = $this->getFileOptions($options) + array( | ||||
'name' => $object->getName(), | 'name' => $object->getName(), | ||||
); | ); | ||||
$is_viewable_image = $object->isViewableImage(); | $is_viewable_image = $object->isViewableImage(); | ||||
$is_audio = $object->isAudio(); | $is_audio = $object->isAudio(); | ||||
$is_video = $object->isVideo(); | |||||
$force_link = ($options['layout'] == 'link'); | $force_link = ($options['layout'] == 'link'); | ||||
$options['viewable'] = ($is_viewable_image || $is_audio); | // If a file is both audio and video, as with "application/ogg" by default, | ||||
// render it as video but allow the user to specify `media=audio` if they | |||||
// want to force it to render as audio. | |||||
if ($is_audio && $is_video) { | |||||
$media = $options['media']; | |||||
if ($media == 'audio') { | |||||
$is_video = false; | |||||
} else { | |||||
$is_audio = false; | |||||
} | |||||
} | |||||
$options['viewable'] = ($is_viewable_image || $is_audio || $is_video); | |||||
if ($is_viewable_image && !$force_link) { | if ($is_viewable_image && !$force_link) { | ||||
return $this->renderImageFile($object, $handle, $options); | return $this->renderImageFile($object, $handle, $options); | ||||
} else if ($is_video && !$force_link) { | |||||
return $this->renderVideoFile($object, $handle, $options); | |||||
} else if ($is_audio && !$force_link) { | } else if ($is_audio && !$force_link) { | ||||
return $this->renderAudioFile($object, $handle, $options); | return $this->renderAudioFile($object, $handle, $options); | ||||
} else { | } else { | ||||
return $this->renderFileLink($object, $handle, $options); | return $this->renderFileLink($object, $handle, $options); | ||||
} | } | ||||
} | } | ||||
private function getFileOptions($option_string) { | private function getFileOptions($option_string) { | ||||
$options = array( | $options = array( | ||||
'size' => null, | 'size' => null, | ||||
'layout' => 'left', | 'layout' => 'left', | ||||
'float' => false, | 'float' => false, | ||||
'width' => null, | 'width' => null, | ||||
'height' => null, | 'height' => null, | ||||
'alt' => null, | 'alt' => null, | ||||
'media' => null, | |||||
'autoplay' => null, | |||||
'loop' => null, | |||||
); | ); | ||||
if ($option_string) { | if ($option_string) { | ||||
$option_string = trim($option_string, ', '); | $option_string = trim($option_string, ', '); | ||||
$parser = new PhutilSimpleOptions(); | $parser = new PhutilSimpleOptions(); | ||||
$options = $parser->parse($option_string) + $options; | $options = $parser->parse($option_string) + $options; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 121 Lines • ▼ Show 20 Lines | return phutil_tag( | ||||
), | ), | ||||
$embed); | $embed); | ||||
} | } | ||||
private function renderAudioFile( | private function renderAudioFile( | ||||
PhabricatorFile $file, | PhabricatorFile $file, | ||||
PhabricatorObjectHandle $handle, | PhabricatorObjectHandle $handle, | ||||
array $options) { | array $options) { | ||||
return $this->renderMediaFile('audio', $file, $handle, $options); | |||||
} | |||||
private function renderVideoFile( | |||||
PhabricatorFile $file, | |||||
PhabricatorObjectHandle $handle, | |||||
array $options) { | |||||
return $this->renderMediaFile('video', $file, $handle, $options); | |||||
} | |||||
private function renderMediaFile( | |||||
$tag, | |||||
PhabricatorFile $file, | |||||
PhabricatorObjectHandle $handle, | |||||
array $options) { | |||||
$is_video = ($tag == 'video'); | |||||
if (idx($options, 'autoplay')) { | if (idx($options, 'autoplay')) { | ||||
$preload = 'auto'; | $preload = 'auto'; | ||||
$autoplay = 'autoplay'; | $autoplay = 'autoplay'; | ||||
} else { | } else { | ||||
// If we don't preload video, the user can't see the first frame and | |||||
// has no clue what they're looking at, so always preload. | |||||
if ($is_video) { | |||||
$preload = 'auto'; | |||||
} else { | |||||
$preload = 'none'; | $preload = 'none'; | ||||
} | |||||
$autoplay = null; | $autoplay = null; | ||||
} | } | ||||
return $this->newTag( | return $this->newTag( | ||||
'audio', | $tag, | ||||
array( | array( | ||||
'controls' => 'controls', | 'controls' => 'controls', | ||||
'preload' => $preload, | 'preload' => $preload, | ||||
'autoplay' => $autoplay, | 'autoplay' => $autoplay, | ||||
'loop' => idx($options, 'loop') ? 'loop' : null, | 'loop' => idx($options, 'loop') ? 'loop' : null, | ||||
'alt' => $options['alt'], | |||||
'class' => 'phabricator-media', | |||||
), | ), | ||||
$this->newTag( | $this->newTag( | ||||
'source', | 'source', | ||||
array( | array( | ||||
'src' => $file->getBestURI(), | 'src' => $file->getBestURI(), | ||||
'type' => $file->getMimeType(), | 'type' => $file->getMimeType(), | ||||
))); | ))); | ||||
} | } | ||||
Show All 25 Lines |