Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/markup/PhabricatorImageRemarkupRule.php
| Show First 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | public function didMarkupText() { | ||||
| $metadata_key = self::KEY_RULE_EXTERNAL_IMAGE; | $metadata_key = self::KEY_RULE_EXTERNAL_IMAGE; | ||||
| $images = $engine->getTextMetadata($metadata_key, array()); | $images = $engine->getTextMetadata($metadata_key, array()); | ||||
| $engine->setTextMetadata($metadata_key, array()); | $engine->setTextMetadata($metadata_key, array()); | ||||
| if (!$images) { | if (!$images) { | ||||
| 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']; | |||||
| $direct_uri = idx($file_map, $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/')) | $src_uri = id(new PhutilURI('/file/imageproxy/')) | ||||
| ->setQueryParam('uri', $args['uri']); | ->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); | ||||
| } | } | ||||
| } | } | ||||
| private function isURI($uri_string) { | private function isURI($uri_string) { | ||||
| // Very simple check to make sure it starts with either http or https. | // Very simple check to make sure it starts with either http or https. | ||||
| // If it does, we'll try to treat it like a valid URI | // If it does, we'll try to treat it like a valid URI | ||||
| return preg_match('~^https?\:\/\/.*\z~i', $uri_string); | return preg_match('~^https?\:\/\/.*\z~i', $uri_string); | ||||
| } | } | ||||
| } | } | ||||