-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow app to register a download provider
This allows to provide a file content for a given dav path. Signed-off-by: Louis Chemineau <louis@chmn.me>
- Loading branch information
Showing
9 changed files
with
281 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2022 Louis Chmn <louis@chmn.me> | ||
* | ||
* @author Louis Chmn <louis@chmn.me> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* 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\Files\Controller; | ||
|
||
use OC\AppFramework\Bootstrap\Coordinator; | ||
use OCP\AppFramework\Http\ZipResponse; | ||
use OCP\AppFramework\Controller; | ||
use OCP\Files\File; | ||
use OCP\Files\Folder; | ||
use OCP\Files\IFileDownloadProvider; | ||
use OCP\Files\Node; | ||
use OCP\IRequest; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class DownloadController extends Controller { | ||
private Coordinator $coordinator; | ||
private LoggerInterface $logger; | ||
|
||
public function __construct( | ||
string $appName, | ||
IRequest $request, | ||
Coordinator $coordinator, | ||
LoggerInterface $logger | ||
) { | ||
parent::__construct($appName, $request); | ||
|
||
$this->request = $request; | ||
$this->coordinator = $coordinator; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* @NoCSRFRequired | ||
* @PublicPage | ||
*/ | ||
public function index(string $files): ZipResponse { | ||
$response = new ZipResponse($this->request, 'download'); | ||
|
||
/** @var string[] */ | ||
$files = json_decode($files); | ||
|
||
if (count($files) === 0) { | ||
return $response; | ||
} | ||
|
||
$commonPrefix = $files[0]; | ||
foreach ($files as $filePath) { | ||
$commonPrefix = $this->getCommonPrefix($filePath, $commonPrefix); | ||
} | ||
|
||
$context = $this->coordinator->getRegistrationContext(); | ||
if ($context === null) { | ||
throw new \Exception("Can't get download providers"); | ||
} | ||
$providerRegistrations = $context->getFileDownloadProviders(); | ||
|
||
foreach ($files as $filePath) { | ||
$node = null; | ||
|
||
foreach ($providerRegistrations as $registration) { | ||
try { | ||
/** @var IFileDownloadProvider */ | ||
$provider = \OCP\Server::get($registration->getService()); | ||
$node = $provider->getNode($filePath); | ||
if ($node !== null) { | ||
break; | ||
} | ||
} catch (\Throwable $ex) { | ||
$providerClass = $provider::class; | ||
$this->logger->warning("Error while getting file content from $providerClass", ['exception' => $ex]); | ||
} | ||
} | ||
|
||
if ($node === null) { | ||
continue; | ||
} | ||
|
||
$this->addNode($response, $node, str_replace($commonPrefix, '', $filePath)); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
private function getCommonPrefix(string $str1, string $str2): string { | ||
$mbStr1 = mb_str_split($str1); | ||
$mbStr2 = mb_str_split($str2); | ||
|
||
for ($i = 0; $i < count($mbStr1); $i++) { | ||
if ($mbStr1[$i] !== $mbStr2[$i]) { | ||
$i--; | ||
break; | ||
} | ||
} | ||
|
||
if ($i < 0) { | ||
return ''; | ||
} else { | ||
return join(array_slice($mbStr1, 0, $i)); | ||
} | ||
} | ||
|
||
private function addNode(ZipResponse $response, Node $node, string $path) { | ||
if ($node instanceof File) { | ||
$response->addResource($node->fopen('r'), $path.$node->getName(), $node->getSize()); | ||
} | ||
|
||
if ($node instanceof Folder) { | ||
foreach ($node->getDirectoryListing() as $subnode) { | ||
$this->addNode($response, $subnode, $path.$node->getName()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2022 Louis Chmn <louis@chmn.me> | ||
* | ||
* @author Louis Chmn <louis@chmn.me> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* 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\Files\Provider; | ||
|
||
use OCP\Files\Folder; | ||
use OCP\Files\Node; | ||
use OCP\Files\IFileDownloadProvider; | ||
|
||
class FileDownloadProvider implements IFileDownloadProvider { | ||
private ?Folder $userFolder; | ||
|
||
public function __construct( | ||
?Folder $userFolder, | ||
) { | ||
$this->userFolder = $userFolder; | ||
} | ||
|
||
public function getNode(string $davPath): ?Node { | ||
if (!str_starts_with($davPath, "/files/")) { | ||
return null; | ||
} | ||
|
||
if ($this->userFolder === null) { | ||
return null; | ||
} | ||
|
||
/** @var ?string */ | ||
$userId = explode('/', $davPath)[2]; | ||
if (is_null($userId) || $userId !== $this->userFolder->getOwner()->getUID()) { | ||
return null; | ||
} | ||
|
||
$filePath = str_replace("/files/$userId", '', $davPath); | ||
|
||
return $this->userFolder->get($filePath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2022 Louis Chmn <louis@chmn.me> | ||
* | ||
* @author Louis Chmn <louis@chmn.me> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* 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 OCP\Files; | ||
|
||
/** | ||
* This interface defines how to provide a file given a dav path. | ||
* | ||
* @since 26.0.0 | ||
*/ | ||
interface IFileDownloadProvider { | ||
/** | ||
* @since 26.0.0 | ||
*/ | ||
public function getNode(string $davPath): ?Node; | ||
} |