Sensitive previews are now blurred

This commit is contained in:
Michael 2024-02-06 16:15:58 +00:00
parent 9b4ade4542
commit 0a6dff0618
3 changed files with 31 additions and 20 deletions

View File

@ -177,15 +177,20 @@ class PostMedia extends BaseEntity
/** /**
* Get preview path for given media id relative to the base URL * Get preview path for given media id relative to the base URL
* *
* @param string $size One of the Proxy::SIZE_* constants * @param string $size One of the Proxy::SIZE_* constants
* @param bool $vlurred If "true", the preview will be blurred
* @return string preview link * @return string preview link
*/ */
public function getPreviewPath(string $size = ''): string public function getPreviewPath(string $size = '', bool $blurred = false): string
{ {
return '/photo/preview/' . $path = '/photo/preview/' .
(Proxy::getPixelsFromSize($size) ? Proxy::getPixelsFromSize($size) . '/' : '') . (Proxy::getPixelsFromSize($size) ? Proxy::getPixelsFromSize($size) . '/' : '') .
$this->id; $this->id;
if ($blurred) {
$path .= '?' . http_build_query(['blur' => true]);
}
return $path;
} }
/** /**

View File

@ -3345,6 +3345,7 @@ class Item
$item['tags'] = $tags['tags']; $item['tags'] = $tags['tags'];
$item['hashtags'] = $tags['hashtags']; $item['hashtags'] = $tags['hashtags'];
$item['mentions'] = $tags['mentions']; $item['mentions'] = $tags['mentions'];
$sensitive = (bool)$item['sensitive'];
if (!$is_preview) { if (!$is_preview) {
$item['body'] = preg_replace("#\s*\[attachment .*?].*?\[/attachment]\s*#ism", "\n", $item['body']); $item['body'] = preg_replace("#\s*\[attachment .*?].*?\[/attachment]\s*#ism", "\n", $item['body']);
@ -3412,11 +3413,11 @@ class Item
$shared_links = array_merge($shared_links, $sharedSplitAttachments['visual']->column('url')); $shared_links = array_merge($shared_links, $sharedSplitAttachments['visual']->column('url'));
$shared_links = array_merge($shared_links, $sharedSplitAttachments['link']->column('url')); $shared_links = array_merge($shared_links, $sharedSplitAttachments['link']->column('url'));
$shared_links = array_merge($shared_links, $sharedSplitAttachments['additional']->column('url')); $shared_links = array_merge($shared_links, $sharedSplitAttachments['additional']->column('url'));
$item['body'] = self::replaceVisualAttachments($sharedSplitAttachments['visual'], $item['body']); $item['body'] = self::replaceVisualAttachments($sharedSplitAttachments['visual'], $item['body'], $sensitive);
} }
$itemSplitAttachments = DI::postMediaRepository()->splitAttachments($item['uri-id'], $shared_links, $item['has-media'] ?? false); $itemSplitAttachments = DI::postMediaRepository()->splitAttachments($item['uri-id'], $shared_links, $item['has-media'] ?? false);
$item['body'] = self::replaceVisualAttachments($itemSplitAttachments['visual'], $item['body'] ?? ''); $item['body'] = self::replaceVisualAttachments($itemSplitAttachments['visual'], $item['body'] ?? '', $sensitive);
self::putInCache($item); self::putInCache($item);
$item['body'] = $body; $item['body'] = $body;
@ -3471,9 +3472,9 @@ class Item
} }
if (!empty($sharedSplitAttachments)) { if (!empty($sharedSplitAttachments)) {
$s = self::addGallery($s, $sharedSplitAttachments['visual'], (bool)$item['sensitive']); $s = self::addGallery($s, $sharedSplitAttachments['visual'], $sensitive);
$s = self::addVisualAttachments($sharedSplitAttachments['visual'], $shared_item, $s, true); $s = self::addVisualAttachments($sharedSplitAttachments['visual'], $shared_item, $s, true, $sensitive);
$s = self::addLinkAttachment($shared_uri_id ?: $item['uri-id'], $sharedSplitAttachments, $body, $s, true, $quote_shared_links); $s = self::addLinkAttachment($shared_uri_id ?: $item['uri-id'], $sharedSplitAttachments, $body, $s, true, $quote_shared_links, $sensitive);
$s = self::addNonVisualAttachments($sharedSplitAttachments['additional'], $item, $s, true); $s = self::addNonVisualAttachments($sharedSplitAttachments['additional'], $item, $s, true);
$body = BBCode::removeSharedData($body); $body = BBCode::removeSharedData($body);
} }
@ -3484,9 +3485,9 @@ class Item
$s = substr($s, 0, $pos); $s = substr($s, 0, $pos);
} }
$s = self::addGallery($s, $itemSplitAttachments['visual'], (bool)$item['sensitive']); $s = self::addGallery($s, $itemSplitAttachments['visual'], $sensitive);
$s = self::addVisualAttachments($itemSplitAttachments['visual'], $item, $s, false); $s = self::addVisualAttachments($itemSplitAttachments['visual'], $item, $s, false, $sensitive);
$s = self::addLinkAttachment($item['uri-id'], $itemSplitAttachments, $body, $s, false, $shared_links); $s = self::addLinkAttachment($item['uri-id'], $itemSplitAttachments, $body, $s, false, $shared_links, $sensitive);
$s = self::addNonVisualAttachments($itemSplitAttachments['additional'], $item, $s, false); $s = self::addNonVisualAttachments($itemSplitAttachments['additional'], $item, $s, false);
$s = self::addQuestions($item, $s); $s = self::addQuestions($item, $s);
@ -3605,9 +3606,10 @@ class Item
* *
* @param PostMedias $PostMedias * @param PostMedias $PostMedias
* @param string $body * @param string $body
* @param bool $sensitive
* @return string modified body * @return string modified body
*/ */
private static function replaceVisualAttachments(PostMedias $PostMedias, string $body): string private static function replaceVisualAttachments(PostMedias $PostMedias, string $body, bool $sensitive): string
{ {
DI::profiler()->startRecording('rendering'); DI::profiler()->startRecording('rendering');
@ -3616,7 +3618,7 @@ class Item
if (DI::baseUrl()->isLocalUri($PostMedia->preview)) { if (DI::baseUrl()->isLocalUri($PostMedia->preview)) {
continue; continue;
} }
$proxy = DI::baseUrl() . $PostMedia->getPreviewPath(Proxy::SIZE_LARGE); $proxy = DI::baseUrl() . $PostMedia->getPreviewPath(Proxy::SIZE_LARGE, $sensitive);
$search = ['[img=' . $PostMedia->preview . ']', ']' . $PostMedia->preview . '[/img]']; $search = ['[img=' . $PostMedia->preview . ']', ']' . $PostMedia->preview . '[/img]'];
$replace = ['[img=' . $proxy . ']', ']' . $proxy . '[/img]']; $replace = ['[img=' . $proxy . ']', ']' . $proxy . '[/img]'];
@ -3625,7 +3627,7 @@ class Item
if (DI::baseUrl()->isLocalUri($PostMedia->url)) { if (DI::baseUrl()->isLocalUri($PostMedia->url)) {
continue; continue;
} }
$proxy = DI::baseUrl() . $PostMedia->getPreviewPath(Proxy::SIZE_LARGE); $proxy = DI::baseUrl() . $PostMedia->getPreviewPath(Proxy::SIZE_LARGE, $sensitive);
$search = ['[img=' . $PostMedia->url . ']', ']' . $PostMedia->url . '[/img]']; $search = ['[img=' . $PostMedia->url . ']', ']' . $PostMedia->url . '[/img]'];
$replace = ['[img=' . $proxy . ']', ']' . $proxy . '[/img]']; $replace = ['[img=' . $proxy . ']', ']' . $proxy . '[/img]'];
@ -3643,10 +3645,11 @@ class Item
* @param array $item * @param array $item
* @param string $content * @param string $content
* @param bool $shared * @param bool $shared
* @param bool $sensitive
* @return string modified content * @return string modified content
* @throws ServiceUnavailableException * @throws ServiceUnavailableException
*/ */
private static function addVisualAttachments(PostMedias $PostMedias, array $item, string $content, bool $shared): string private static function addVisualAttachments(PostMedias $PostMedias, array $item, string $content, bool $shared, bool $sensitive): string
{ {
DI::profiler()->startRecording('rendering'); DI::profiler()->startRecording('rendering');
$leading = ''; $leading = '';
@ -3661,7 +3664,7 @@ class Item
if ($PostMedia->mimetype->type == 'image' || $PostMedia->preview) { if ($PostMedia->mimetype->type == 'image' || $PostMedia->preview) {
$preview_size = Proxy::SIZE_MEDIUM; $preview_size = Proxy::SIZE_MEDIUM;
$preview_url = DI::baseUrl() . $PostMedia->getPreviewPath($preview_size); $preview_url = DI::baseUrl() . $PostMedia->getPreviewPath($preview_size, $sensitive);
} else { } else {
$preview_size = 0; $preview_size = 0;
$preview_url = ''; $preview_url = '';
@ -3746,11 +3749,12 @@ class Item
* @param string $content * @param string $content
* @param bool $shared * @param bool $shared
* @param array $ignore_links A list of URLs to ignore * @param array $ignore_links A list of URLs to ignore
* @param bool $sensitive
* @return string modified content * @return string modified content
* @throws InternalServerErrorException * @throws InternalServerErrorException
* @throws ServiceUnavailableException * @throws ServiceUnavailableException
*/ */
private static function addLinkAttachment(int $uriid, array $attachments, string $body, string $content, bool $shared, array $ignore_links): string private static function addLinkAttachment(int $uriid, array $attachments, string $body, string $content, bool $shared, array $ignore_links, bool $sensitive): string
{ {
DI::profiler()->startRecording('rendering'); DI::profiler()->startRecording('rendering');
// Don't show a preview when there is a visual attachment (audio or video) // Don't show a preview when there is a visual attachment (audio or video)
@ -3793,9 +3797,9 @@ class Item
if ($preview && $attachment->preview) { if ($preview && $attachment->preview) {
if ($attachment->previewWidth >= 500) { if ($attachment->previewWidth >= 500) {
$data['image'] = DI::baseUrl() . $attachment->getPreviewPath(Proxy::SIZE_MEDIUM); $data['image'] = DI::baseUrl() . $attachment->getPreviewPath(Proxy::SIZE_MEDIUM, $sensitive);
} else { } else {
$data['preview'] = DI::baseUrl() . $attachment->getPreviewPath(Proxy::SIZE_MEDIUM); $data['preview'] = DI::baseUrl() . $attachment->getPreviewPath(Proxy::SIZE_MEDIUM, $sensitive);
} }
} }

View File

@ -166,7 +166,9 @@ class Photo extends BaseApi
$stamp = microtime(true); $stamp = microtime(true);
$imgdata = MPhoto::getImageDataForPhoto($photo); if (empty($request['blur']) || empty($photo['blurhash'])) {
$imgdata = MPhoto::getImageDataForPhoto($photo);
}
if (empty($imgdata) && empty($photo['blurhash'])) { if (empty($imgdata) && empty($photo['blurhash'])) {
throw new HTTPException\NotFoundException(); throw new HTTPException\NotFoundException();
} elseif (empty($imgdata) && !empty($photo['blurhash'])) { } elseif (empty($imgdata) && !empty($photo['blurhash'])) {