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-18 17:52:04 +00:00
|
|
|
use OCA\Memories\Errors;
|
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
|
|
|
{
|
|
|
|
// Get ids from body
|
|
|
|
$files = $this->request->getParam('files');
|
|
|
|
if (null === $files || !\is_array($files)) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::MissingParameter('files');
|
2022-12-06 19:02:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 19:38:57 +00:00
|
|
|
// Return id
|
2022-12-06 19:53:31 +00:00
|
|
|
$handle = self::createHandle('memories', $files);
|
2022-12-06 19:53:43 +00:00
|
|
|
|
2022-12-06 19:53:31 +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
|
|
|
|
{
|
|
|
|
// Get ids from request
|
|
|
|
$session = \OC::$server->get(ISession::class);
|
2022-12-06 19:53:31 +00:00
|
|
|
$key = "memories_download_{$handle}";
|
|
|
|
$info = $session->get($key);
|
2022-12-06 19:02:37 +00:00
|
|
|
$session->remove($key);
|
|
|
|
|
2022-12-06 19:53:31 +00:00
|
|
|
if (null === $info) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotFound('handle');
|
2022-12-06 19:02:37 +00:00
|
|
|
}
|
2023-03-18 17:52:04 +00:00
|
|
|
|
2022-12-06 19:53:43 +00:00
|
|
|
$name = $info[0].'-'.date('YmdHis');
|
2022-12-06 19:53:31 +00:00
|
|
|
$fileIds = $info[1];
|
2022-12-06 19:02:37 +00:00
|
|
|
|
|
|
|
/** @var int[] $fileIds */
|
|
|
|
$fileIds = array_filter(array_map('intval', $fileIds), function (int $id): bool {
|
|
|
|
return $id > 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check if we have any valid ids
|
|
|
|
if (0 === \count($fileIds)) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotFound('file IDs');
|
2022-12-06 19:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Download single file
|
|
|
|
if (1 === \count($fileIds)) {
|
|
|
|
return $this->one($fileIds[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download multiple files
|
2022-12-06 19:53:31 +00:00
|
|
|
$this->multiple($name, $fileIds); // exits
|
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-01-18 03:55:17 +00:00
|
|
|
public function one(int $fileid): Http\Response
|
2022-12-06 19:02:37 +00:00
|
|
|
{
|
|
|
|
$file = $this->getUserFile($fileid);
|
|
|
|
if (null === $file) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotFoundFile($fileid);
|
2023-01-21 16:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the owner's root folder
|
|
|
|
$owner = $file->getOwner()->getUID();
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($owner);
|
|
|
|
|
|
|
|
// Get the file in the context of the owner
|
|
|
|
$ownerFile = $userFolder->getById($fileid);
|
|
|
|
if (0 === \count($ownerFile)) {
|
|
|
|
// This should never happen, since the file was already found earlier
|
|
|
|
// Except if it was deleted in the meantime ...
|
|
|
|
return new JSONResponse([
|
|
|
|
'message' => 'File not found in owner\'s root folder',
|
|
|
|
], Http::STATUS_INTERNAL_SERVER_ERROR);
|
2022-12-06 19:02:37 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 16:53:45 +00:00
|
|
|
// Get DAV path of file relative to owner's root folder
|
|
|
|
$path = $userFolder->getRelativePath($ownerFile[0]->getPath());
|
|
|
|
if (null === $path) {
|
|
|
|
return new JSONResponse([
|
|
|
|
'message' => 'File path not found in owner\'s root folder',
|
|
|
|
], Http::STATUS_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
2023-01-18 03:55:17 +00:00
|
|
|
|
|
|
|
// Setup filesystem for owner
|
|
|
|
\OC_Util::tearDownFS();
|
2023-01-21 16:53:45 +00:00
|
|
|
\OC_Util::setupFS($owner);
|
2023-01-18 03:55:17 +00:00
|
|
|
|
|
|
|
// HEAD and RANGE support
|
|
|
|
$server_params = ['head' => 'HEAD' === $this->request->getMethod()];
|
|
|
|
if (isset($_SERVER['HTTP_RANGE'])) {
|
|
|
|
$server_params['range'] = $this->request->getHeader('Range');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write file to output and exit
|
|
|
|
\OC_Files::get(\dirname($path), basename($path), $server_params);
|
2022-12-06 19:02:37 +00:00
|
|
|
|
2023-01-18 03:55:17 +00:00
|
|
|
exit;
|
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
|
|
|
*/
|
2022-12-06 19:53:31 +00:00
|
|
|
private function multiple(string $name, array &$fileIds)
|
2022-12-06 19:02:37 +00:00
|
|
|
{
|
|
|
|
// Disable time limit
|
|
|
|
$executionTime = (int) \OC::$server->get(IniGetWrapper::class)->getNumeric('max_execution_time');
|
|
|
|
@set_time_limit(0);
|
|
|
|
|
|
|
|
// Ensure we can abort the request if user stops it
|
|
|
|
ignore_user_abort(true);
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
|
|
|
// Create a zip file
|
2022-12-06 19:53:31 +00:00
|
|
|
$streamer->sendHeaders($name);
|
2022-12-06 19:02:37 +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
|
|
|
/** @var ITempManager for clearing temp files */
|
|
|
|
$tempManager = \OC::$server->get(ITempManager::class);
|
|
|
|
|
2022-12-06 19:02:37 +00:00
|
|
|
// Send each file
|
|
|
|
foreach ($fileIds as $fileId) {
|
|
|
|
if (connection_aborted()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var bool|resource */
|
|
|
|
$handle = false;
|
|
|
|
|
|
|
|
/** @var ?File */
|
|
|
|
$file = null;
|
|
|
|
|
2022-12-06 19:38:57 +00:00
|
|
|
/** @var ?string */
|
|
|
|
$name = (string) $fileId;
|
|
|
|
|
2022-12-06 19:02:37 +00:00
|
|
|
try {
|
|
|
|
// This checks permissions
|
|
|
|
$file = $this->getUserFile($fileId);
|
|
|
|
if (null === $file) {
|
|
|
|
throw new \Exception('File not found');
|
|
|
|
}
|
2022-12-06 19:38:57 +00:00
|
|
|
$name = $file->getName();
|
2022-12-06 19:02:37 +00:00
|
|
|
|
|
|
|
// Open file
|
|
|
|
$handle = $file->fopen('rb');
|
|
|
|
if (false === $handle) {
|
|
|
|
throw new \Exception('Failed to open file');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle duplicate names
|
|
|
|
if (isset($nameCounts[$name])) {
|
2022-12-06 19:38:57 +00:00
|
|
|
++$nameCounts[$name];
|
2022-12-06 19:02:37 +00:00
|
|
|
|
|
|
|
// add count before extension
|
|
|
|
$extpos = strrpos($name, '.');
|
|
|
|
if (false === $extpos) {
|
|
|
|
$name .= " ({$nameCounts[$name]})";
|
|
|
|
} else {
|
|
|
|
$name = substr($name, 0, $extpos)." ({$nameCounts[$name]})".substr($name, $extpos);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$nameCounts[$name] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()),
|
2022-12-06 19:38:57 +00:00
|
|
|
time(),
|
2022-12-06 19:02:37 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// close the dummy file
|
|
|
|
fclose($dummy);
|
|
|
|
} finally {
|
|
|
|
if (false !== $handle) {
|
|
|
|
fclose($handle);
|
|
|
|
}
|
2023-02-01 03:14:52 +00:00
|
|
|
|
|
|
|
// Clear any temp files
|
|
|
|
$tempManager->clean();
|
2022-12-06 19:02:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore time limit
|
|
|
|
@set_time_limit($executionTime);
|
|
|
|
|
|
|
|
// Done
|
|
|
|
$streamer->finalize();
|
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|