Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/data/PhabricatorFileRawDataIteratorFuture.php
- This file was added.
<?php | |||||
final class PhabricatorFileRawDataIteratorFuture | |||||
extends FutureProxy { | |||||
private $engine; | |||||
private $file; | |||||
private $begin; | |||||
private $end; | |||||
private $format; | |||||
public function __construct( | |||||
PhabricatorFileStorageEngine $engine, | |||||
PhabricatorFile $file, | |||||
$begin, | |||||
$end, | |||||
PhabricatorFileStorageFormat $format, | |||||
Future $future) { | |||||
parent::__construct($future); | |||||
$this->engine = $engine; | |||||
$this->file = $file; | |||||
$this->begin = $begin; | |||||
$this->end = $end; | |||||
$this->format = $format; | |||||
} | |||||
protected function didReceiveResult($formatted_data) { | |||||
$file = $this->file; | |||||
$engine = $this->engine; | |||||
$begin = $this->begin; | |||||
$end = $this->end; | |||||
$format = $this->format; | |||||
$known_integrity = $file->getIntegrityHash(); | |||||
if ($known_integrity !== null) { | |||||
$new_integrity = $engine->newIntegrityHash($formatted_data, $format); | |||||
if (!phutil_hashes_are_identical($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); | |||||
$data = ''; | |||||
$format_iterator = $format->newReadIterator($formatted_data); | |||||
foreach ($format_iterator as $raw_chunk) { | |||||
$data .= $raw_chunk; | |||||
} | |||||
if ($begin !== null && $end !== null) { | |||||
$data = substr($data, $begin, ($end - $begin)); | |||||
} else if ($begin !== null) { | |||||
$data = substr($data, $begin); | |||||
} else if ($end !== null) { | |||||
$data = substr($data, 0, $end); | |||||
} | |||||
return array($data); | |||||
} | |||||
} |