Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/document/PhabricatorHexdumpDocumentEngine.php
<?php | <?php | ||||
final class PhabricatorHexdumpDocumentEngine | final class PhabricatorHexdumpDocumentEngine | ||||
extends PhabricatorDocumentEngine { | extends PhabricatorDocumentEngine { | ||||
const ENGINEKEY = 'hexdump'; | const ENGINEKEY = 'hexdump'; | ||||
public function getViewAsLabel(PhabricatorDocumentRef $ref) { | public function getViewAsLabel(PhabricatorDocumentRef $ref) { | ||||
return pht('View as Hexdump'); | return pht('View as Hexdump'); | ||||
} | } | ||||
protected function getDocumentIconIcon(PhabricatorDocumentRef $ref) { | protected function getDocumentIconIcon(PhabricatorDocumentRef $ref) { | ||||
return 'fa-microchip'; | return 'fa-microchip'; | ||||
} | } | ||||
protected function getContentScore() { | protected function getByteLengthLimit() { | ||||
return (1024 * 1024 * 1); | |||||
} | |||||
protected function getContentScore(PhabricatorDocumentRef $ref) { | |||||
return 500; | return 500; | ||||
} | } | ||||
protected function canRenderDocumentType(PhabricatorDocumentRef $ref) { | protected function canRenderDocumentType(PhabricatorDocumentRef $ref) { | ||||
return true; | return true; | ||||
} | } | ||||
protected function canRenderPartialDocument(PhabricatorDocumentRef $ref) { | |||||
return true; | |||||
} | |||||
protected function newDocumentContent(PhabricatorDocumentRef $ref) { | protected function newDocumentContent(PhabricatorDocumentRef $ref) { | ||||
$content = $ref->loadData(); | $limit = $this->getByteLengthLimit(); | ||||
$length = $ref->getByteLength(); | |||||
$is_partial = false; | |||||
if ($limit) { | |||||
if ($length > $limit) { | |||||
$is_partial = true; | |||||
$length = $limit; | |||||
} | |||||
} | |||||
$content = $ref->loadData(null, $length); | |||||
$output = array(); | $output = array(); | ||||
$offset = 0; | $offset = 0; | ||||
$lines = str_split($content, 16); | $lines = str_split($content, 16); | ||||
foreach ($lines as $line) { | foreach ($lines as $line) { | ||||
$output[] = sprintf( | $output[] = sprintf( | ||||
'%08x %- 23s %- 23s %- 16s', | '%08x %- 23s %- 23s %- 16s', | ||||
Show All 9 Lines | protected function newDocumentContent(PhabricatorDocumentRef $ref) { | ||||
$container = phutil_tag( | $container = phutil_tag( | ||||
'div', | 'div', | ||||
array( | array( | ||||
'class' => 'document-engine-hexdump PhabricatorMonospaced', | 'class' => 'document-engine-hexdump PhabricatorMonospaced', | ||||
), | ), | ||||
$output); | $output); | ||||
return $container; | $message = null; | ||||
if ($is_partial) { | |||||
$message = $this->newMessage( | |||||
pht( | |||||
'This document is too large to be completely rendered inline. The '. | |||||
'first %s bytes are shown.', | |||||
new PhutilNumber($limit))); | |||||
} | |||||
return array( | |||||
$message, | |||||
$container, | |||||
); | |||||
} | } | ||||
private function renderHex($bytes) { | private function renderHex($bytes) { | ||||
$length = strlen($bytes); | $length = strlen($bytes); | ||||
$output = array(); | $output = array(); | ||||
for ($ii = 0; $ii < $length; $ii++) { | for ($ii = 0; $ii < $length; $ii++) { | ||||
$output[] = sprintf('%02x', ord($bytes[$ii])); | $output[] = sprintf('%02x', ord($bytes[$ii])); | ||||
Show All 24 Lines |