Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/engine/PhabricatorFileStorageEngine.php
Show First 20 Lines • Show All 326 Lines • ▼ Show 20 Lines | /* -( Loading Storage Engines )-------------------------------------------- */ | ||||
public function getRawFileDataIterator( | public function getRawFileDataIterator( | ||||
PhabricatorFile $file, | PhabricatorFile $file, | ||||
$begin, | $begin, | ||||
$end, | $end, | ||||
PhabricatorFileStorageFormat $format) { | PhabricatorFileStorageFormat $format) { | ||||
$formatted_data = $this->readFile($file->getStorageHandle()); | $formatted_data = $this->readFile($file->getStorageHandle()); | ||||
$known_integrity = $file->getIntegrityHash(); | |||||
if ($known_integrity !== null) { | |||||
$new_integrity = $this->newIntegrityHash($formatted_data, $format); | |||||
if ($known_integrity !== $new_integrity) { | |||||
throw new PhabricatorFileIntegrityException( | |||||
pht( | |||||
'File data integrity check failed. Dark forces have corrupted '. | |||||
'or tampered with this file. The file data can not be read.')); | |||||
} | |||||
} | |||||
$formatted_data = array($formatted_data); | $formatted_data = array($formatted_data); | ||||
$data = ''; | $data = ''; | ||||
$format_iterator = $format->newReadIterator($formatted_data); | $format_iterator = $format->newReadIterator($formatted_data); | ||||
foreach ($format_iterator as $raw_chunk) { | foreach ($format_iterator as $raw_chunk) { | ||||
$data .= $raw_chunk; | $data .= $raw_chunk; | ||||
} | } | ||||
if ($begin !== null && $end !== null) { | if ($begin !== null && $end !== null) { | ||||
$data = substr($data, $begin, ($end - $begin)); | $data = substr($data, $begin, ($end - $begin)); | ||||
} else if ($begin !== null) { | } else if ($begin !== null) { | ||||
$data = substr($data, $begin); | $data = substr($data, $begin); | ||||
} else if ($end !== null) { | } else if ($end !== null) { | ||||
$data = substr($data, 0, $end); | $data = substr($data, 0, $end); | ||||
} | } | ||||
return array($data); | return array($data); | ||||
} | } | ||||
public function newIntegrityHash( | |||||
$data, | |||||
PhabricatorFileStorageFormat $format) { | |||||
$data_hash = PhabricatorHash::digest($data); | |||||
$format_hash = $format->newFormatIntegrityHash(); | |||||
$full_hash = "{$data_hash}/{$format_hash}"; | |||||
return PhabricatorHash::digest($full_hash); | |||||
} | |||||
} | } |