2022-12-06 19:02:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2022 Varun Patil <radialapps@gmail.com>
|
|
|
|
* @author Varun Patil <radialapps@gmail.com>
|
|
|
|
* @license AGPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Memories\Controller;
|
|
|
|
|
|
|
|
use bantu\IniGetWrapper\IniGetWrapper;
|
2023-03-23 20:32:23 +00:00
|
|
|
use OCA\Memories\Exceptions;
|
|
|
|
use OCA\Memories\Util;
|
2022-12-06 19:02:37 +00:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\ISession;
|
2023-02-01 03:14:52 +00:00
|
|
|
use OCP\ITempManager;
|
2022-12-06 19:02:37 +00:00
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
|
2023-03-22 18:40:56 +00:00
|
|
|
class DownloadController extends GenericApiController
|
2022-12-06 19:02:37 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
*
|
2022-12-08 22:40:31 +00:00
|
|
|
* @UseSession
|
|
|
|
*
|
2022-12-06 19:02:37 +00:00
|
|
|
* Request to download one or more files
|
|
|
|
*/
|
2023-03-19 03:38:37 +00:00
|
|
|
public function request(): Http\Response
|
2022-12-06 19:02:37 +00:00
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () {
|
|
|
|
// Get ids from body
|
|
|
|
$files = $this->request->getParam('files');
|
|
|
|
if (null === $files || !\is_array($files)) {
|
|
|
|
throw Exceptions::MissingParameter('files');
|
|
|
|
}
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
// Return id
|
|
|
|
$handle = self::createHandle('memories', $files);
|
2022-12-06 19:53:43 +00:00
|
|
|
|
2023-03-23 20:32:23 +00:00
|
|
|
return new JSONResponse(['handle' => $handle]);
|
|
|
|
});
|
2022-12-06 19:38:57 +00:00
|
|
|
}
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2022-12-06 19:38:57 +00:00
|
|
|
/**
|
|
|
|
* Get a handle for downloading files.
|
|
|
|
*
|
2022-12-08 22:40:31 +00:00
|
|
|
* The calling controller must have the UseSession annotation.
|
|
|
|
*
|
2022-12-06 19:53:43 +00:00
|
|
|
* @param string $name Name of zip file
|
|
|
|
* @param int[] $files
|
2022-12-06 19:38:57 +00:00
|
|
|
*/
|
2022-12-06 19:53:31 +00:00
|
|
|
public static function createHandle(string $name, array $files): string
|
2022-12-06 19:38:57 +00:00
|
|
|
{
|
2022-12-06 19:02:37 +00:00
|
|
|
$handle = \OC::$server->get(ISecureRandom::class)->generate(16, ISecureRandom::CHAR_ALPHANUMERIC);
|
2022-12-06 19:53:31 +00:00
|
|
|
\OC::$server->get(ISession::class)->set("memories_download_{$handle}", [$name, $files]);
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2022-12-06 19:38:57 +00:00
|
|
|
return $handle;
|
2022-12-06 19:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* Download one or more files
|
|
|
|
*/
|
|
|
|
public function file(string $handle): Http\Response
|
|
|
|
{
|
2023-03-23 20:32:23 +00:00
|
|
|
return Util::guardEx(function () use ($handle) {
|
|
|
|
// Get ids from request
|
|
|
|
$session = \OC::$server->get(ISession::class);
|
|
|
|
$key = "memories_download_{$handle}";
|
|
|
|
$info = $session->get($key);
|
|
|
|
$session->remove($key);
|
|
|
|
|
|
|
|
if (null === $info) {
|
|
|
|
return Exceptions::NotFound('handle');
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = $info[0].'-'.date('YmdHis');
|
|
|
|
$fileIds = $info[1];
|
|
|
|
|
|
|
|
/** @var int[] $fileIds */
|
|
|
|
$fileIds = array_filter(array_map('intval', $fileIds), fn ($id) => $id > 0);
|
|
|
|
|
|
|
|
// Check if we have any valid ids
|
|
|
|
if (0 === \count($fileIds)) {
|
|
|
|
return Exceptions::NotFound('file IDs');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download single file
|
|
|
|
if (1 === \count($fileIds)) {
|
2023-04-04 00:41:01 +00:00
|
|
|
return $this->one($fileIds[0], false);
|
2023-03-23 20:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Download multiple files
|
2023-04-20 18:33:54 +00:00
|
|
|
return $this->multiple($name, $fileIds);
|
2022-12-06 19:02:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-18 03:55:17 +00:00
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @PublicPage
|
2022-12-06 19:02:37 +00:00
|
|
|
*/
|
2023-04-04 01:17:15 +00:00
|
|
|
public function one(int $fileid, bool $resumable = true, int $numChunks = 0): Http\Response
|
2022-12-06 19:02:37 +00:00
|
|
|
{
|
2023-04-20 18:33:54 +00:00
|
|
|
return Util::guardExDirect(function (Http\IOutput $out) use ($fileid, $resumable, $numChunks) {
|
2023-03-23 21:45:56 +00:00
|
|
|
$file = $this->fs->getUserFile($fileid);
|
2023-03-23 20:32:23 +00:00
|
|
|
|
2023-04-04 00:41:01 +00:00
|
|
|
// check if http_range is sent by browser
|
|
|
|
$range = $this->request->getHeader('Range');
|
|
|
|
if (!empty($range)) {
|
2023-04-19 23:32:29 +00:00
|
|
|
[$sizeUnit, $rangeOrig] = Util::explode_exact('=', $range, 2);
|
2023-04-04 00:41:01 +00:00
|
|
|
if ('bytes' === $sizeUnit) {
|
|
|
|
// http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt
|
2023-04-19 23:32:29 +00:00
|
|
|
[$range, $extra] = Util::explode_exact(',', $rangeOrig, 2);
|
2023-04-04 00:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not resumable, discard the range
|
|
|
|
if (!$resumable) {
|
|
|
|
$range = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get file reading parameters
|
|
|
|
$size = $file->getSize();
|
2023-04-19 23:32:29 +00:00
|
|
|
[$seekStart, $seekEnd] = Util::explode_exact('-', $range, 2);
|
2023-04-04 00:41:01 +00:00
|
|
|
$seekEnd = (empty($seekEnd)) ? ($size - 1) : min(abs((int) $seekEnd), $size - 1);
|
|
|
|
$seekStart = (empty($seekStart) || $seekEnd < abs((int) $seekStart)) ? 0 : max(abs((int) $seekStart), 0);
|
|
|
|
|
|
|
|
// If the client knows about ranges, only send a maximum of X KB
|
|
|
|
// This way we don't read the whole file for no reason
|
|
|
|
if (!empty($range)) {
|
|
|
|
// Default to 64MB
|
|
|
|
$maxLen = 64 * 1024 * 1024;
|
|
|
|
|
2023-04-04 01:17:15 +00:00
|
|
|
// For videos, use a max of 8MB
|
2023-04-04 00:41:01 +00:00
|
|
|
if ('video' === $this->request->getHeader('Sec-Fetch-Dest')) {
|
2023-04-04 01:17:15 +00:00
|
|
|
$maxLen = 8 * 1024 * 1024;
|
2023-04-04 00:41:01 +00:00
|
|
|
}
|
2023-03-23 20:32:23 +00:00
|
|
|
|
2023-04-04 01:17:15 +00:00
|
|
|
// Check if the client sent a hint for the chunk size
|
|
|
|
if ($numChunks) {
|
|
|
|
$maxLen = min(ceil($size / $numChunks), $maxLen * 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No less than 1MB; this is just wasteful
|
|
|
|
$maxLen = max($maxLen, 1024 * 1024);
|
|
|
|
|
2023-04-04 00:41:01 +00:00
|
|
|
$seekEnd = min($seekEnd, $seekStart + $maxLen);
|
2023-03-23 20:32:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 00:41:01 +00:00
|
|
|
// Only send partial content header if downloading a piece of the file
|
|
|
|
if ($seekStart > 0 || $seekEnd < ($size - 1)) {
|
2023-04-20 18:33:54 +00:00
|
|
|
$out->setHeader('HTTP/1.1 206 Partial Content');
|
2023-03-23 20:32:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 00:41:01 +00:00
|
|
|
// Set headers
|
2023-04-20 18:33:54 +00:00
|
|
|
$out->setHeader('Accept-Ranges: bytes');
|
|
|
|
$out->setHeader("Content-Range: bytes {$seekStart}-{$seekEnd}/{$size}");
|
|
|
|
$out->setHeader('Content-Length: '.($seekEnd - $seekStart + 1));
|
|
|
|
$out->setHeader('Content-Type: '.$file->getMimeType());
|
2023-03-23 20:32:23 +00:00
|
|
|
|
2023-04-04 00:41:01 +00:00
|
|
|
// Make sure the browser downloads the file
|
2023-04-20 18:33:54 +00:00
|
|
|
$out->setHeader('Content-Disposition: attachment; filename="'.$file->getName().'"');
|
2023-04-04 00:41:01 +00:00
|
|
|
|
|
|
|
// Open file to send
|
|
|
|
$res = $file->fopen('rb');
|
|
|
|
|
|
|
|
// Seek to start if not zero
|
|
|
|
if ($seekStart > 0) {
|
|
|
|
fseek($res, $seekStart);
|
2023-03-23 20:32:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 00:41:01 +00:00
|
|
|
// Handle aborts manually
|
|
|
|
ignore_user_abort(true);
|
|
|
|
|
|
|
|
// Send 1MB at a time
|
|
|
|
// But send 256KB initially in case loading metadata only
|
|
|
|
$chunkRead = 0;
|
|
|
|
|
|
|
|
// Start output buffering
|
|
|
|
ob_start();
|
|
|
|
|
|
|
|
while (!feof($res) && $seekStart <= $seekEnd) {
|
2023-04-19 23:44:19 +00:00
|
|
|
$lenLeft = $seekEnd - $seekStart + 1;
|
|
|
|
$buffer = fread($res, min(1024 * 1024, $lenLeft));
|
2023-04-04 00:41:01 +00:00
|
|
|
if (false === $buffer) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$seekStart += \strlen($buffer);
|
|
|
|
$chunkRead += \strlen($buffer);
|
|
|
|
|
|
|
|
// Send buffer
|
2023-04-20 18:33:54 +00:00
|
|
|
$out->setOutput($buffer);
|
2023-04-04 00:41:01 +00:00
|
|
|
|
|
|
|
// Flush output if chunk is large enough
|
|
|
|
if ($chunkRead > 1024 * 512) {
|
|
|
|
// Check if client disconnected
|
|
|
|
if (CONNECTION_NORMAL !== connection_status() || connection_aborted()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush output
|
|
|
|
ob_flush();
|
|
|
|
$chunkRead = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush remaining output
|
|
|
|
ob_end_flush();
|
|
|
|
|
|
|
|
// Close file
|
|
|
|
fclose($res);
|
2023-03-23 20:32:23 +00:00
|
|
|
});
|
2022-12-06 19:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download multiple files.
|
|
|
|
*
|
2022-12-06 19:53:43 +00:00
|
|
|
* @param string $name Name of zip file
|
|
|
|
* @param int[] $fileIds
|
2022-12-06 19:02:37 +00:00
|
|
|
*/
|
2023-04-20 18:33:54 +00:00
|
|
|
private function multiple(string $name, array $fileIds): Http\Response
|
2022-12-06 19:02:37 +00:00
|
|
|
{
|
2023-04-20 18:33:54 +00:00
|
|
|
return Util::guardExDirect(function ($out) use ($name, $fileIds) {
|
|
|
|
// Disable time limit
|
|
|
|
$executionTime = (int) \OC::$server->get(IniGetWrapper::class)->getNumeric('max_execution_time');
|
|
|
|
@set_time_limit(0);
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
// Ensure we can abort the request if user stops it
|
|
|
|
ignore_user_abort(true);
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
// Pretend the size is huge so forced zip64
|
|
|
|
// Lookup the constructor of \OC\Streamer for more info
|
|
|
|
$size = \count($fileIds) * 1024 * 1024 * 1024 * 8;
|
|
|
|
$streamer = new \OC\Streamer($this->request, $size, \count($fileIds));
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
// Create a zip file
|
|
|
|
$streamer->sendHeaders($name);
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
// Multiple files might have the same name
|
|
|
|
// So we need to add a number to the end of the name
|
|
|
|
$nameCounts = [];
|
2023-02-01 03:14:52 +00:00
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
/** @var ITempManager for clearing temp files */
|
|
|
|
$tempManager = \OC::$server->get(ITempManager::class);
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
// Send each file
|
|
|
|
foreach ($fileIds as $fileId) {
|
|
|
|
if (connection_aborted()) {
|
|
|
|
break;
|
|
|
|
}
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
/** @var bool|resource */
|
|
|
|
$handle = false;
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
/** @var ?File */
|
|
|
|
$file = null;
|
2022-12-06 19:38:57 +00:00
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
/** @var ?string */
|
|
|
|
$name = (string) $fileId;
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
try {
|
|
|
|
// This checks permissions
|
|
|
|
$file = $this->fs->getUserFile($fileId);
|
|
|
|
$name = $file->getName();
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
// Open file
|
|
|
|
$handle = $file->fopen('rb');
|
|
|
|
if (false === $handle) {
|
|
|
|
throw new \Exception('Failed to open file');
|
|
|
|
}
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
// Handle duplicate names
|
|
|
|
if (isset($nameCounts[$name])) {
|
|
|
|
++$nameCounts[$name];
|
|
|
|
|
|
|
|
// add count before extension
|
|
|
|
$extpos = strrpos($name, '.');
|
|
|
|
if (false === $extpos) {
|
|
|
|
$name .= " ({$nameCounts[$name]})";
|
|
|
|
} else {
|
|
|
|
$name = substr($name, 0, $extpos)." ({$nameCounts[$name]})".substr($name, $extpos);
|
|
|
|
}
|
2022-12-06 19:02:37 +00:00
|
|
|
} else {
|
2023-04-20 18:33:54 +00:00
|
|
|
$nameCounts[$name] = 0;
|
2022-12-06 19:02:37 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
// Add file to zip
|
|
|
|
if (!$streamer->addFileFromStream(
|
|
|
|
$handle,
|
|
|
|
$name,
|
|
|
|
$file->getSize(),
|
|
|
|
$file->getMTime(),
|
|
|
|
)) {
|
|
|
|
throw new \Exception('Failed to add file to zip');
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// create a dummy memory file with the error message
|
|
|
|
$dummy = fopen('php://memory', 'rw+');
|
|
|
|
fwrite($dummy, $e->getMessage());
|
|
|
|
rewind($dummy);
|
|
|
|
|
|
|
|
$streamer->addFileFromStream(
|
|
|
|
$dummy,
|
|
|
|
"{$name}_error.txt",
|
|
|
|
\strlen($e->getMessage()),
|
|
|
|
time(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// close the dummy file
|
|
|
|
fclose($dummy);
|
|
|
|
} finally {
|
|
|
|
if (false !== $handle) {
|
|
|
|
fclose($handle);
|
|
|
|
}
|
2023-02-01 03:14:52 +00:00
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
// Clear any temp files
|
|
|
|
$tempManager->clean();
|
|
|
|
}
|
2022-12-06 19:02:37 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
// Restore time limit
|
|
|
|
@set_time_limit($executionTime);
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2023-04-20 18:33:54 +00:00
|
|
|
// Done
|
|
|
|
$streamer->finalize();
|
|
|
|
});
|
2022-12-06 19:02:37 +00:00
|
|
|
}
|
|
|
|
}
|