Skip to content

Commit

Permalink
feat: rudimentary support for range requests when fetching the file c…
Browse files Browse the repository at this point in the history
…ontent

Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliushaertl committed Feb 27, 2024
1 parent d7b79e3 commit b535fa8
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public function checkFileInfo($fileId, $access_token) {
'SupportsLocks' => $this->lockManager->isLockProviderAvailable(),
'IsUserLocked' => $this->permissionManager->userIsFeatureLocked($wopi->getEditorUid()),
'EnableRemoteLinkPicker' => (bool)$wopi->getCanwrite() && !$isPublic && !$wopi->getDirect(),
'HasContentRange' => true,
];

$enableZotero = $this->config->getAppValue(Application::APPNAME, 'zoteroEnabled', 'yes') === 'yes';
Expand Down Expand Up @@ -349,7 +350,32 @@ public function getFile($fileId,
if ($file->getSize() === 0) {
$response = new Http\Response();
} else {
$response = new StreamResponse($file->fopen('rb'));

$filesize = $file->getSize();
if ($this->request->getHeader('Range')) {
$partialContent = true;
preg_match('/bytes=(\d+)-(\d+)?/', $this->request->getHeader('Range'), $matches);

$offset = intval($matches[1] ?? 0);
$length = intval($matches[2] ?? 0) - $offset + 1;
if ($length <= 0) {
$length = $filesize - $offset;
}

$fp = $file->fopen('rb');
$rangeStream = fopen("php://temp", "w+b");
stream_copy_to_stream($fp, $rangeStream, $length, $offset);
fclose($fp);

fseek($rangeStream, 0);
$response = new StreamResponse($rangeStream);
$response->addHeader('Accept-Ranges', 'bytes');
$response->addHeader('Content-Length', $filesize);
$response->setStatus(Http::STATUS_PARTIAL_CONTENT);
$response->addHeader('Content-Range', 'bytes ' . $offset . '-' . ($offset + $length) . '/' . $filesize);
} else {
$response = new StreamResponse($file->fopen('rb'));
}
}
}
$response->addHeader('Content-Disposition', 'attachment');
Expand Down

0 comments on commit b535fa8

Please sign in to comment.