Merge pull request #57 from strukturag/any-storage-support

Support abitrary storages when downloading files.
This commit is contained in:
Joachim Bauch 2021-11-23 12:17:32 +01:00 committed by GitHub
commit 26d29bba15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -25,15 +25,18 @@ namespace OCA\Pdfdraw\Controller;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use \Firebase\JWT\JWT; use \Firebase\JWT\JWT;
use OC\Files\Filesystem;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataDownloadResponse; use OCP\AppFramework\Http\DataDownloadResponse;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController; use OCP\AppFramework\OCSController;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\File; use OCP\Files\File;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\IConfig; use OCP\IConfig;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\ILogger;
use OCP\IRequest; use OCP\IRequest;
class ApiController extends OCSController { class ApiController extends OCSController {
@ -48,9 +51,15 @@ class ApiController extends OCSController {
*/ */
private $root; private $root;
/** @var IUserMountCache */
private $userMountCache;
/** @var IConfig */ /** @var IConfig */
private $config; private $config;
/** @var ILogger */
private $logger;
/** /**
* @param string $AppName * @param string $AppName
* @param IRequest $request * @param IRequest $request
@ -60,11 +69,15 @@ class ApiController extends OCSController {
IRequest $request, IRequest $request,
IDBConnection $db, IDBConnection $db,
IRootFolder $root, IRootFolder $root,
IConfig $config) { IUserMountCache $userMountCache,
IConfig $config,
ILogger $logger) {
parent::__construct($AppName, $request); parent::__construct($AppName, $request);
$this->db = $db; $this->db = $db;
$this->root = $root; $this->root = $root;
$this->userMountCache = $userMountCache;
$this->config = $config; $this->config = $config;
$this->logger = $logger;
} }
/** /**
@ -229,46 +242,45 @@ class ApiController extends OCSController {
return new DataResponse([], Http::STATUS_UNAUTHORIZED); return new DataResponse([], Http::STATUS_UNAUTHORIZED);
} }
$query = $this->db->getQueryBuilder(); $mountPoints = $this->userMountCache->getMountsForFileId($fileId);
$query->select('storage') if (empty($mountPoints)) {
->from('filecache')
->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId)));
$result = $query->execute();
$storage = null;
while ($row = $result->fetch()) {
$storage = $row['storage'];
break;
}
if (empty($storage)) {
return new DataResponse([], Http::STATUS_NOT_FOUND); return new DataResponse([], Http::STATUS_NOT_FOUND);
} }
$query = $this->db->getQueryBuilder(); foreach ($mountPoints as $mountPoint) {
$query->select('id') try {
->from('storages') $userId = $mountPoint->getUser()->getUID();
->where($query->expr()->eq('numeric_id', $query->createNamedParameter($storage))); $userFolder = $this->root->getUserFolder($userId);
$result = $query->execute(); if (!Filesystem::$loaded) {
$homeId = null; // Filesystem wasn't loaded for anyone,
while ($row = $result->fetch()) { // so we boot it up in order to make hooks in the View work.
$homeId = $row['id']; Filesystem::init($userId, '/' . $userId . '/files');
break; }
} } catch (\Exception $e) {
if (empty($homeId) || strpos($homeId, 'home::') !== 0) { $this->logger->debug($e->getMessage(), [
return new DataResponse([], Http::STATUS_NOT_FOUND); 'app' => $this->appName,
'exception' => $e,
]);
continue;
}
$files = $userFolder->getById($fileId);
if (empty($files)) {
continue;
}
foreach ($files as $file) {
if ($file->isReadable()) {
return new DataDownloadResponse($file->getContent(), $file->getName(), $file->getMimeType());
}
$this->logger->debug('Mount point ' . ($mountPoint->getMountId() ?? 'null') . ' has access to file ' . $file->getId() . ' but permissions are ' . $file->getPermissions(), [
'app' => $this->appName,
]);
}
} }
$ownerId = substr($homeId, 6); return new DataResponse([], Http::STATUS_NOT_FOUND);
$files = $this->root->getUserFolder($ownerId)->getById($fileId);
if (empty($files)) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
$file = $files[0];
if (!$file instanceof File) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
return new DataDownloadResponse($file->getContent(), $file->getName(), $file->getMimeType());
} }
} }