Path: blob/master/src/applications/files/document/PhabricatorImageDocumentEngine.php
12241 views
<?php12final class PhabricatorImageDocumentEngine3extends PhabricatorDocumentEngine {45const ENGINEKEY = 'image';67public function getViewAsLabel(PhabricatorDocumentRef $ref) {8return pht('View as Image');9}1011protected function getDocumentIconIcon(PhabricatorDocumentRef $ref) {12return 'fa-file-image-o';13}1415protected function getByteLengthLimit() {16return (1024 * 1024 * 64);17}1819public function canDiffDocuments(20PhabricatorDocumentRef $uref = null,21PhabricatorDocumentRef $vref = null) {2223// For now, we can only render a rich image diff if the documents have24// their data stored in Files already.2526if ($uref && !$uref->getFile()) {27return false;28}2930if ($vref && !$vref->getFile()) {31return false;32}3334return true;35}3637public function newEngineBlocks(38PhabricatorDocumentRef $uref = null,39PhabricatorDocumentRef $vref = null) {4041if ($uref) {42$u_blocks = $this->newDiffBlocks($uref);43} else {44$u_blocks = array();45}4647if ($vref) {48$v_blocks = $this->newDiffBlocks($vref);49} else {50$v_blocks = array();51}5253return id(new PhabricatorDocumentEngineBlocks())54->addBlockList($uref, $u_blocks)55->addBlockList($vref, $v_blocks);56}5758public function newBlockDiffViews(59PhabricatorDocumentRef $uref,60PhabricatorDocumentEngineBlock $ublock,61PhabricatorDocumentRef $vref,62PhabricatorDocumentEngineBlock $vblock) {6364$u_content = $this->newBlockContentView($uref, $ublock);65$v_content = $this->newBlockContentView($vref, $vblock);6667return id(new PhabricatorDocumentEngineBlockDiff())68->setOldContent($u_content)69->addOldClass('diff-image-cell')70->setNewContent($v_content)71->addNewClass('diff-image-cell');72}737475private function newDiffBlocks(PhabricatorDocumentRef $ref) {76$blocks = array();7778$file = $ref->getFile();7980$image_view = phutil_tag(81'div',82array(83'class' => 'differential-image-stage',84),85phutil_tag(86'img',87array(88'alt' => $file->getAltText(),89'src' => $file->getBestURI(),90)));9192$hash = $file->getContentHash();9394$blocks[] = id(new PhabricatorDocumentEngineBlock())95->setBlockKey('1')96->setDifferenceHash($hash)97->setContent($image_view);9899return $blocks;100}101102protected function canRenderDocumentType(PhabricatorDocumentRef $ref) {103$file = $ref->getFile();104if ($file) {105return $file->isViewableImage();106}107108$viewable_types = PhabricatorEnv::getEnvConfig('files.viewable-mime-types');109$viewable_types = array_keys($viewable_types);110111$image_types = PhabricatorEnv::getEnvConfig('files.image-mime-types');112$image_types = array_keys($image_types);113114return115$ref->hasAnyMimeType($viewable_types) &&116$ref->hasAnyMimeType($image_types);117}118119protected function newDocumentContent(PhabricatorDocumentRef $ref) {120$file = $ref->getFile();121if ($file) {122$source_uri = $file->getViewURI();123} else {124// We could use a "data:" URI here. It's not yet clear if or when we'll125// have a ref but no backing file.126throw new PhutilMethodNotImplementedException();127}128129$image = phutil_tag(130'img',131array(132'alt' => $file->getAltText(),133'src' => $source_uri,134));135136$linked_image = phutil_tag(137'a',138array(139'href' => $source_uri,140'rel' => 'noreferrer',141),142$image);143144$container = phutil_tag(145'div',146array(147'class' => 'document-engine-image',148),149$linked_image);150151return $container;152}153154}155156157