Simplify the attachment handling

This commit is contained in:
Michael 2021-07-06 08:57:49 +00:00
parent b7a4ef0ec1
commit 8bfa15cf23
2 changed files with 7 additions and 23 deletions

View file

@ -618,10 +618,11 @@ class BBCode
* @param integer $simplehtml * @param integer $simplehtml
* @param bool $tryoembed * @param bool $tryoembed
* @param array $data * @param array $data
* @param int $uriid
* @return string * @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function convertAttachment($text, $simplehtml = self::INTERNAL, $tryoembed = true, array $data = []) public static function convertAttachment($text, $simplehtml = self::INTERNAL, $tryoembed = true, array $data = [], $uriid = 0)
{ {
$data = $data ?: self::getAttachmentData($text); $data = $data ?: self::getAttachmentData($text);
if (empty($data) || empty($data['url'])) { if (empty($data) || empty($data['url'])) {
@ -658,12 +659,12 @@ class BBCode
if (!empty($data['title']) && !empty($data['url'])) { if (!empty($data['title']) && !empty($data['url'])) {
if (!empty($data['image']) && empty($data['text']) && ($data['type'] == 'photo')) { if (!empty($data['image']) && empty($data['text']) && ($data['type'] == 'photo')) {
$return .= sprintf('<a href="%s" target="_blank" rel="noopener noreferrer"><img src="%s" alt="" title="%s" class="attachment-image" /></a>', $data['url'], self::proxyUrl($data['image'], $simplehtml), $data['title']); $return .= sprintf('<a href="%s" target="_blank" rel="noopener noreferrer"><img src="%s" alt="" title="%s" class="attachment-image" /></a>', $data['url'], self::proxyUrl($data['image'], $simplehtml, $uriid), $data['title']);
} else { } else {
if (!empty($data['image'])) { if (!empty($data['image'])) {
$return .= sprintf('<a href="%s" target="_blank" rel="noopener noreferrer"><img src="%s" alt="" title="%s" class="attachment-image" /></a><br>', $data['url'], self::proxyUrl($data['image'], $simplehtml), $data['title']); $return .= sprintf('<a href="%s" target="_blank" rel="noopener noreferrer"><img src="%s" alt="" title="%s" class="attachment-image" /></a><br>', $data['url'], self::proxyUrl($data['image'], $simplehtml, $uriid), $data['title']);
} elseif (!empty($data['preview'])) { } elseif (!empty($data['preview'])) {
$return .= sprintf('<a href="%s" target="_blank" rel="noopener noreferrer"><img src="%s" alt="" title="%s" class="attachment-preview" /></a><br>', $data['url'], self::proxyUrl($data['preview'], $simplehtml), $data['title']); $return .= sprintf('<a href="%s" target="_blank" rel="noopener noreferrer"><img src="%s" alt="" title="%s" class="attachment-preview" /></a><br>', $data['url'], self::proxyUrl($data['preview'], $simplehtml, $uriid), $data['title']);
} }
$return .= sprintf('<h4><a href="%s">%s</a></h4>', $data['url'], $data['title']); $return .= sprintf('<h4><a href="%s">%s</a></h4>', $data['url'], $data['title']);
} }
@ -1411,16 +1412,7 @@ class BBCode
} elseif (!in_array($simple_html, [self::INTERNAL, self::EXTERNAL, self::CONNECTORS])) { } elseif (!in_array($simple_html, [self::INTERNAL, self::EXTERNAL, self::CONNECTORS])) {
$text = self::removeAttachment($text, true); $text = self::removeAttachment($text, true);
} else { } else {
$data = self::getAttachmentData($text); $text = self::convertAttachment($text, $simple_html, $try_oembed, [], $uriid);
if (!empty($data['image'])) {
$data['image'] = Post\Link::getByLink($uriid, $data['image']);
}
if (!empty($data['preview'])) {
$data['preview'] = Post\Link::getByLink($uriid, $data['preview']);
}
$text = self::convertAttachment($text, $simple_html, $try_oembed, $data);
} }
$nosmile = strpos($text, '[nosmile]') !== false; $nosmile = strpos($text, '[nosmile]') !== false;

View file

@ -3052,16 +3052,8 @@ class Item
} }
} }
if (!empty($data['image'])) {
$data['image'] = Post\Link::getByLink($uriid, $data['image']);
}
if (!empty($data['preview'])) {
$data['preview'] = Post\Link::getByLink($uriid, $data['preview']);
}
// @todo Use a template // @todo Use a template
$rendered = BBCode::convertAttachment('', BBCode::INTERNAL, false, $data); $rendered = BBCode::convertAttachment('', BBCode::INTERNAL, false, $data, $uriid);
} elseif (!self::containsLink($content, $data['url'], Post\Media::HTML)) { } elseif (!self::containsLink($content, $data['url'], Post\Media::HTML)) {
$rendered = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/link.tpl'), [ $rendered = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/link.tpl'), [
'$url' => $data['url'], '$url' => $data['url'],