1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-20 09:18:48 +02:00

Support a file data iteration interface for large files

Summary: Ref T7149. A couple diffs down the line, this will let us emit chunked files without doing all the work up front or holding the entire file in RAM.

Test Plan:
(Some newlines added for clarity.)

```
$ ./bin/files cat F942
ABCDEFGHIJKLMNOPQRSTUVWXYZ
$ ./bin/files cat F942 --begin 1
BCDEFGHIJKLMNOPQRSTUVWXYZ
$ ./bin/files cat F942 --end 10
ABCDEFGHIJ
$ ./bin/files cat F942 --begin 3 --end 5
DE
$
```

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: joshuaspence, epriestley

Maniphest Tasks: T7149

Differential Revision: https://secure.phabricator.com/D12071
This commit is contained in:
epriestley 2015-03-14 08:28:59 -07:00
parent 32d8d67535
commit 2aefb43843
2 changed files with 42 additions and 1 deletions

View file

@ -10,6 +10,16 @@ final class PhabricatorFilesManagementCatWorkflow
pht('Print the contents of a file.'))
->setArguments(
array(
array(
'name' => 'begin',
'param' => 'bytes',
'help' => pht('Begin printing at a specific offset.'),
),
array(
'name' => 'end',
'param' => 'bytes',
'help' => pht('End printing at a specific offset.'),
),
array(
'name' => 'names',
'wildcard' => true,
@ -31,7 +41,13 @@ final class PhabricatorFilesManagementCatWorkflow
$file = head($this->loadFilesWithNames($names));
echo $file->loadFileData();
$begin = $args->getArg('begin');
$end = $args->getArg('end');
$iterator = $file->getFileDataIterator($begin, $end);
foreach ($iterator as $data) {
echo $data;
}
return 0;
}

View file

@ -603,6 +603,31 @@ final class PhabricatorFile extends PhabricatorFileDAO
return $data;
}
/**
* Return an iterable which emits file content bytes.
*
* @param int Offset for the start of data.
* @param int Offset for the end of data.
* @return Iterable Iterable object which emits requested data.
*/
public function getFileDataIterator($begin = null, $end = null) {
// The default implementation is trivial and just loads the entire file
// upfront.
$data = $this->loadFileData();
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);
}
public function getViewURI() {
if (!$this->getPHID()) {
throw new Exception(