1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-26 16:52:41 +01:00

When rendering "{image ...}" images, check the cache and just render a direct "<img />" tag if possible

Summary: Depends on D19193. Ref T13101. Fixes T4190. Before we render a fancy AJAX placeholder, check if we already have a valid cache for the image. If we do, render a direct `<img />` tag. This is a little cleaner and, e.g., avoids flicker in Safari, at least.

Test Plan: Rendered `{image ...}` rules in remarkup with new and existing URIs.

Maniphest Tasks: T13101, T4190

Differential Revision: https://secure.phabricator.com/D19194
This commit is contained in:
epriestley 2018-03-08 06:44:18 -08:00
parent 9d3a722eb1
commit b30535a36f

View file

@ -94,17 +94,69 @@ final class PhabricatorImageRemarkupRule extends PhutilRemarkupRule {
return; return;
} }
// Look for images we've already successfully fetched that aren't about
// to get eaten by the GC. For any we find, we can just emit a normal
// "<img />" tag pointing directly to the file.
// For files which we don't hit in the cache, we emit a placeholder
// instead and use AJAX to actually perform the fetch.
$digests = array();
foreach ($images as $image) {
$uri = $image['args']['uri'];
$digests[] = PhabricatorHash::digestForIndex($uri);
}
$caches = id(new PhabricatorFileExternalRequest())->loadAllWhere(
'uriIndex IN (%Ls) AND isSuccessful = 1 AND ttl > %d',
$digests,
PhabricatorTime::getNow() + phutil_units('1 hour in seconds'));
$file_phids = array();
foreach ($caches as $cache) {
$file_phids[$cache->getFilePHID()] = $cache->getURI();
}
$file_map = array();
if ($file_phids) {
$files = id(new PhabricatorFileQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withPHIDs(array_keys($file_phids))
->execute();
foreach ($files as $file) {
$phid = $file->getPHID();
$file_remote_uri = $file_phids[$phid];
$file_view_uri = $file->getViewURI();
$file_map[$file_remote_uri] = $file_view_uri;
}
}
foreach ($images as $image) { foreach ($images as $image) {
$args = $image['args']; $args = $image['args'];
$uri = $args['uri'];
$src_uri = id(new PhutilURI('/file/imageproxy/')) $direct_uri = idx($file_map, $uri);
->setQueryParam('uri', $args['uri']); if ($direct_uri) {
$img = phutil_tag(
'img',
array(
'src' => $direct_uri,
'alt' => $args['alt'],
'width' => $args['width'],
'height' => $args['height'],
));
} else {
$src_uri = id(new PhutilURI('/file/imageproxy/'))
->setQueryParam('uri', $uri);
$img = id(new PHUIRemarkupImageView()) $img = id(new PHUIRemarkupImageView())
->setURI($src_uri) ->setURI($src_uri)
->setAlt($args['alt']) ->setAlt($args['alt'])
->setWidth($args['width']) ->setWidth($args['width'])
->setHeight($args['height']); ->setHeight($args['height']);
}
$engine->overwriteStoredText($image['token'], $img); $engine->overwriteStoredText($image['token'], $img);
} }