2011-07-15 23:17:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group markup
|
|
|
|
*/
|
2012-03-14 00:21:04 +01:00
|
|
|
final class PhabricatorRemarkupRuleEmbedFile
|
2011-07-15 23:17:55 +02:00
|
|
|
extends PhutilRemarkupRule {
|
|
|
|
|
2012-10-23 04:06:56 +02:00
|
|
|
const KEY_RULE_EMBED_FILE = 'rule.embed.file';
|
2013-01-25 02:23:05 +01:00
|
|
|
const KEY_EMBED_FILE_PHIDS = 'phabricator.embedded-file-phids';
|
2012-10-23 04:06:56 +02:00
|
|
|
|
2011-07-15 23:17:55 +02:00
|
|
|
public function apply($text) {
|
2013-02-22 01:13:55 +01:00
|
|
|
return preg_replace_callback(
|
2012-01-16 19:07:21 +01:00
|
|
|
"@{F(\d+)([^}]+?)?}@",
|
2011-07-15 23:17:55 +02:00
|
|
|
array($this, 'markupEmbedFile'),
|
|
|
|
$text);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function markupEmbedFile($matches) {
|
|
|
|
|
|
|
|
$file = null;
|
|
|
|
if ($matches[1]) {
|
|
|
|
// TODO: This is pretty inefficient if there are a bunch of files.
|
2013-09-30 18:38:13 +02:00
|
|
|
// TODO: (T603) This isn't policy-aware and should be extending
|
|
|
|
// PhabricatorRemarkupRuleObject.
|
2011-07-15 23:17:55 +02:00
|
|
|
$file = id(new PhabricatorFile())->load($matches[1]);
|
|
|
|
}
|
|
|
|
|
2012-01-16 19:07:21 +01:00
|
|
|
if (!$file) {
|
2011-07-15 23:17:55 +02:00
|
|
|
return $matches[0];
|
|
|
|
}
|
2012-10-23 04:06:56 +02:00
|
|
|
|
|
|
|
$engine = $this->getEngine();
|
2013-03-23 01:33:36 +01:00
|
|
|
|
|
|
|
if ($engine->isTextMode()) {
|
|
|
|
return $engine->storeText($file->getBestURI());
|
|
|
|
}
|
|
|
|
|
|
|
|
$phid = $file->getPHID();
|
|
|
|
|
2012-10-23 04:06:56 +02:00
|
|
|
$token = $engine->storeText('');
|
|
|
|
$metadata_key = self::KEY_RULE_EMBED_FILE;
|
|
|
|
$metadata = $engine->getTextMetadata($metadata_key, array());
|
|
|
|
$bundle = array('token' => $token);
|
2012-01-16 19:07:21 +01:00
|
|
|
|
|
|
|
$options = array(
|
|
|
|
'size' => 'thumb',
|
|
|
|
'layout' => 'left',
|
|
|
|
'float' => false,
|
2012-03-23 23:32:07 +01:00
|
|
|
'name' => null,
|
2012-01-16 19:07:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!empty($matches[2])) {
|
2012-02-18 01:22:02 +01:00
|
|
|
$matches[2] = trim($matches[2], ', ');
|
2012-11-29 15:05:35 +01:00
|
|
|
$parser = new PhutilSimpleOptions();
|
|
|
|
$options = $parser->parse($matches[2]) + $options;
|
2012-01-16 19:07:21 +01:00
|
|
|
}
|
2012-03-23 23:32:07 +01:00
|
|
|
$file_name = coalesce($options['name'], $file->getName());
|
2012-10-23 04:06:56 +02:00
|
|
|
$options['name'] = $file_name;
|
2012-03-23 23:32:07 +01:00
|
|
|
|
2013-02-06 22:43:14 +01:00
|
|
|
$is_viewable_image = $file->isViewableImage();
|
2013-09-27 19:51:25 +02:00
|
|
|
$is_audio = $file->isAudio();
|
2013-02-06 22:43:14 +01:00
|
|
|
|
2012-10-08 22:26:10 +02:00
|
|
|
$attrs = array();
|
2013-02-06 22:43:14 +01:00
|
|
|
if ($is_viewable_image) {
|
|
|
|
switch ((string)$options['size']) {
|
|
|
|
case 'full':
|
|
|
|
$attrs['src'] = $file->getBestURI();
|
|
|
|
$options['image_class'] = null;
|
|
|
|
$file_data = $file->getMetadata();
|
|
|
|
$height = idx($file_data, PhabricatorFile::METADATA_IMAGE_HEIGHT);
|
|
|
|
if ($height) {
|
|
|
|
$attrs['height'] = $height;
|
|
|
|
}
|
|
|
|
$width = idx($file_data, PhabricatorFile::METADATA_IMAGE_WIDTH);
|
|
|
|
if ($width) {
|
|
|
|
$attrs['width'] = $width;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'thumb':
|
|
|
|
default:
|
|
|
|
$attrs['src'] = $file->getPreview220URI();
|
|
|
|
$dimensions =
|
|
|
|
PhabricatorImageTransformer::getPreviewDimensions($file, 220);
|
|
|
|
$attrs['width'] = $dimensions['sdx'];
|
|
|
|
$attrs['height'] = $dimensions['sdy'];
|
|
|
|
$options['image_class'] = 'phabricator-remarkup-embed-image';
|
|
|
|
break;
|
|
|
|
}
|
2012-01-16 19:07:21 +01:00
|
|
|
}
|
2012-10-23 04:06:56 +02:00
|
|
|
$bundle['attrs'] = $attrs;
|
|
|
|
$bundle['options'] = $options;
|
|
|
|
|
|
|
|
$bundle['meta'] = array(
|
|
|
|
'phid' => $file->getPHID(),
|
2013-02-06 22:43:14 +01:00
|
|
|
'viewable' => $is_viewable_image,
|
2013-09-27 19:51:25 +02:00
|
|
|
'audio' => $is_audio,
|
2012-10-23 04:06:56 +02:00
|
|
|
'uri' => $file->getBestURI(),
|
|
|
|
'dUri' => $file->getDownloadURI(),
|
|
|
|
'name' => $options['name'],
|
2013-09-27 19:51:25 +02:00
|
|
|
'mime' => $file->getMimeType(),
|
2012-10-23 04:06:56 +02:00
|
|
|
);
|
2013-09-27 19:51:25 +02:00
|
|
|
|
|
|
|
if ($is_audio) {
|
|
|
|
$bundle['meta'] += array(
|
|
|
|
'autoplay' => idx($options, 'autoplay'),
|
|
|
|
'loop' => idx($options, 'loop'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-10-23 04:06:56 +02:00
|
|
|
$metadata[$phid][] = $bundle;
|
|
|
|
$engine->setTextMetadata($metadata_key, $metadata);
|
2012-01-16 19:07:21 +01:00
|
|
|
|
2012-10-23 04:06:56 +02:00
|
|
|
return $token;
|
|
|
|
}
|
2012-01-16 19:07:21 +01:00
|
|
|
|
2012-10-23 04:06:56 +02:00
|
|
|
public function didMarkupText() {
|
|
|
|
$engine = $this->getEngine();
|
2012-01-16 19:07:21 +01:00
|
|
|
|
2012-10-23 04:06:56 +02:00
|
|
|
$metadata_key = self::KEY_RULE_EMBED_FILE;
|
|
|
|
$metadata = $engine->getTextMetadata($metadata_key, array());
|
2012-01-16 19:07:21 +01:00
|
|
|
|
2012-10-23 04:06:56 +02:00
|
|
|
if (!$metadata) {
|
|
|
|
return;
|
2012-01-16 19:07:21 +01:00
|
|
|
}
|
|
|
|
|
2013-01-25 02:23:05 +01:00
|
|
|
$file_phids = array();
|
2012-10-23 04:06:56 +02:00
|
|
|
foreach ($metadata as $phid => $bundles) {
|
|
|
|
foreach ($bundles as $data) {
|
|
|
|
|
|
|
|
$options = $data['options'];
|
|
|
|
$meta = $data['meta'];
|
|
|
|
|
2013-09-27 19:51:25 +02:00
|
|
|
$is_image = idx($meta, 'viewable');
|
|
|
|
$is_audio = idx($meta, 'audio');
|
|
|
|
|
|
|
|
if ((!$is_image && !$is_audio) || $options['layout'] == 'link') {
|
2012-10-23 04:06:56 +02:00
|
|
|
$link = id(new PhabricatorFileLinkView())
|
|
|
|
->setFilePHID($meta['phid'])
|
|
|
|
->setFileName($meta['name'])
|
|
|
|
->setFileDownloadURI($meta['dUri'])
|
|
|
|
->setFileViewURI($meta['uri'])
|
|
|
|
->setFileViewable($meta['viewable']);
|
|
|
|
$embed = $link->render();
|
|
|
|
$engine->overwriteStoredText($data['token'], $embed);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-09-27 19:51:25 +02:00
|
|
|
if ($is_audio) {
|
|
|
|
if (idx($options, 'autoplay')) {
|
|
|
|
$preload = 'auto';
|
|
|
|
$autoplay = 'autoplay';
|
|
|
|
} else {
|
|
|
|
$preload = 'none';
|
|
|
|
$autoplay = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$link = phutil_tag(
|
|
|
|
'audio',
|
|
|
|
array(
|
|
|
|
'controls' => 'controls',
|
|
|
|
'preload' => $preload,
|
|
|
|
'autoplay' => $autoplay,
|
|
|
|
'loop' => idx($options, 'loop') ? 'loop' : null,
|
|
|
|
),
|
|
|
|
phutil_tag(
|
|
|
|
'source',
|
|
|
|
array(
|
|
|
|
'src' => $meta['uri'],
|
|
|
|
'type' => $meta['mime'],
|
|
|
|
)));
|
|
|
|
$engine->overwriteStoredText($data['token'], $link);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-10-23 04:06:56 +02:00
|
|
|
require_celerity_resource('lightbox-attachment-css');
|
2013-01-18 03:39:02 +01:00
|
|
|
$img = phutil_tag('img', $data['attrs']);
|
2012-10-23 04:06:56 +02:00
|
|
|
|
2013-01-25 21:57:17 +01:00
|
|
|
$embed = javelin_tag(
|
2012-10-23 04:06:56 +02:00
|
|
|
'a',
|
|
|
|
array(
|
2012-11-22 17:25:13 +01:00
|
|
|
'href' => $meta['uri'],
|
2012-10-23 04:06:56 +02:00
|
|
|
'class' => $options['image_class'],
|
|
|
|
'sigil' => 'lightboxable',
|
|
|
|
'mustcapture' => true,
|
|
|
|
'meta' => $meta,
|
|
|
|
),
|
|
|
|
$img);
|
|
|
|
|
|
|
|
$layout_class = null;
|
|
|
|
switch ($options['layout']) {
|
|
|
|
case 'right':
|
|
|
|
case 'center':
|
|
|
|
case 'inline':
|
|
|
|
case 'left':
|
|
|
|
$layout_class = 'phabricator-remarkup-embed-layout-'.
|
|
|
|
$options['layout'];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$layout_class = 'phabricator-remarkup-embed-layout-left';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options['float']) {
|
|
|
|
switch ($options['layout']) {
|
|
|
|
case 'center':
|
|
|
|
case 'inline':
|
|
|
|
break;
|
|
|
|
case 'right':
|
|
|
|
$layout_class .= ' phabricator-remarkup-embed-float-right';
|
|
|
|
break;
|
|
|
|
case 'left':
|
|
|
|
default:
|
|
|
|
$layout_class .= ' phabricator-remarkup-embed-float-left';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($layout_class) {
|
2013-01-18 09:32:58 +01:00
|
|
|
$embed = phutil_tag(
|
2012-10-23 04:06:56 +02:00
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'class' => $layout_class,
|
|
|
|
),
|
|
|
|
$embed);
|
|
|
|
}
|
|
|
|
|
|
|
|
$engine->overwriteStoredText($data['token'], $embed);
|
|
|
|
}
|
2013-01-25 02:23:05 +01:00
|
|
|
$file_phids[] = $phid;
|
2012-10-23 04:06:56 +02:00
|
|
|
}
|
2013-01-25 02:23:05 +01:00
|
|
|
$engine->setTextMetadata(self::KEY_EMBED_FILE_PHIDS, $file_phids);
|
2012-10-23 04:06:56 +02:00
|
|
|
$engine->setTextMetadata($metadata_key, array());
|
2011-07-15 23:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|