diff --git a/lib/private/Avatar.php b/lib/private/Avatar.php index 9e8bd0136c200..72cfd16a0d855 100644 --- a/lib/private/Avatar.php +++ b/lib/private/Avatar.php @@ -29,8 +29,8 @@ namespace OC; use OC\User\User; -use OCP\Files\Folder; -use OCP\Files\File; +use OCP\Files\SimpleFS\Folder; +use OCP\Files\SimpleFS\File; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\IAvatar; @@ -98,7 +98,8 @@ public function get ($size = 64) { * @return bool */ public function exists() { - return $this->folder->nodeExists('avatar.jpg') || $this->folder->nodeExists('avatar.png'); + + return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); } /** @@ -170,7 +171,7 @@ public function getFile($size) { } try { - $file = $this->folder->get($path); + $file = $this->folder->getFile($path); } catch (NotFoundException $e) { if ($size <= 0) { throw new NotFoundException; @@ -178,7 +179,7 @@ public function getFile($size) { $avatar = new OC_Image(); /** @var File $file */ - $file = $this->folder->get('avatar.' . $ext); + $file = $this->folder->getFile('avatar.' . $ext); $avatar->loadFromData($file->getContent()); if ($size !== -1) { $avatar->resize($size); @@ -201,9 +202,9 @@ public function getFile($size) { * @throws NotFoundException */ private function getExtension() { - if ($this->folder->nodeExists('avatar.jpg')) { + if ($this->folder->fileExists('avatar.jpg')) { return 'jpg'; - } elseif ($this->folder->nodeExists('avatar.png')) { + } elseif ($this->folder->fileExists('avatar.png')) { return 'png'; } throw new NotFoundException; diff --git a/lib/private/AvatarManager.php b/lib/private/AvatarManager.php index 0eabc3a17543c..3456f4e0590e0 100644 --- a/lib/private/AvatarManager.php +++ b/lib/private/AvatarManager.php @@ -27,13 +27,12 @@ namespace OC; -use OCP\Files\Folder; +use OCP\Files\IAppData; use OCP\Files\NotFoundException; use OCP\IAvatarManager; use OCP\IConfig; use OCP\ILogger; use OCP\IUserManager; -use OCP\Files\IRootFolder; use OCP\IL10N; /** @@ -44,8 +43,8 @@ class AvatarManager implements IAvatarManager { /** @var IUserManager */ private $userManager; - /** @var IRootFolder */ - private $rootFolder; + /** @var IAppData */ + private $appData; /** @var IL10N */ private $l; @@ -60,19 +59,19 @@ class AvatarManager implements IAvatarManager { * AvatarManager constructor. * * @param IUserManager $userManager - * @param IRootFolder $rootFolder + * @param IAppData $appData * @param IL10N $l * @param ILogger $logger * @param IConfig $config */ public function __construct( IUserManager $userManager, - IRootFolder $rootFolder, + IAppData $appData, IL10N $l, ILogger $logger, IConfig $config) { $this->userManager = $userManager; - $this->rootFolder = $rootFolder; + $this->appData = $appData; $this->l = $l; $this->logger = $logger; $this->config = $config; @@ -92,14 +91,7 @@ public function getAvatar($userId) { throw new \Exception('user does not exist'); } - /* - * Fix for #22119 - * Basically we do not want to copy the skeleton folder - */ - \OC\Files\Filesystem::initMountPoints($userId); - $dir = '/' . $userId; - /** @var Folder $folder */ - $folder = $this->rootFolder->get($dir); + $folder = $this->appData->getFolder($userId); return new Avatar($folder, $this->l, $user, $this->logger, $this->config); } diff --git a/lib/private/Files/AppData/AppData.php b/lib/private/Files/AppData/AppData.php new file mode 100644 index 0000000000000..0f0c364c64a22 --- /dev/null +++ b/lib/private/Files/AppData/AppData.php @@ -0,0 +1,31 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ + +namespace OC\Files\AppData; + +use OC\Files\SimpleFS\Root; +use OCP\Files\IAppData; + +class AppData extends Root implements IAppData { + +} diff --git a/lib/private/Files/AppData/Factory.php b/lib/private/Files/AppData/Factory.php new file mode 100644 index 0000000000000..04f8ca64ae4fe --- /dev/null +++ b/lib/private/Files/AppData/Factory.php @@ -0,0 +1,97 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +namespace OC\Files\AppData; + +use OC\SystemConfig; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; +use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; + +class Factory { + + /** @var IRootFolder */ + private $rootFolder; + + /** @var Folder */ + private $appDataFolder; + + /** @var SystemConfig */ + private $config; + + public function __construct(IRootFolder $rootFolder, + SystemConfig $systemConfig) { + + $this->rootFolder = $rootFolder; + $this->config = $systemConfig; + } + + /** + * @return Folder + */ + private function getAppDataFolder() { + if ($this->appDataFolder === null) { + $instanceId = $this->config->getValue('instanceid', null); + if ($instanceId === null) { + throw new \RuntimeException('no instance id!'); + } + + /** @var string $instanceId */ + try { + $appDataFolder = $this->rootFolder->get($instanceId); + } catch (NotFoundException $e) { + try { + $appDataFolder = $this->rootFolder->newFolder($instanceId); + } catch (NotPermittedException $e) { + // Log + return null; + } + } + + $this->appDataFolder = $appDataFolder; + } + + return $this->appDataFolder; + } + + /** + * @param string $appId + * @return AppData + */ + public function get($appId) { + $appDataFolder = $this->getAppDataFolder(); + + try { + $appFolder = $appDataFolder->get($appId); + } catch (NotFoundException $e) { + try { + $appFolder = $appDataFolder->newFolder($appId); + } catch (NotPermittedException $e) { + // Log + return null; + } + } + + return new AppData($appFolder); + } +} diff --git a/lib/private/Files/SimpleFS/File.php b/lib/private/Files/SimpleFS/File.php new file mode 100644 index 0000000000000..7881902fc87c0 --- /dev/null +++ b/lib/private/Files/SimpleFS/File.php @@ -0,0 +1,113 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +namespace OC\Files\SimpleFS; + +use OCP\Files\NotPermittedException; + +class File implements \OCP\Files\SimpleFS\File { + + /** @var \OCP\Files\File $file */ + private $file; + + /** + * File constructor. + * + * @param \OCP\Files\File $file + */ + public function __construct(\OCP\Files\File $file) { + $this->file = $file; + } + + /** + * Get the name + * + * @return string + */ + public function getName() { + return $this->file->getName(); + } + + /** + * Get the size in bytes + * + * @return int + */ + public function getSize() { + return $this->file->getSize(); + } + + /** + * Get the ETag + * + * @return string + */ + public function getETag() { + return $this->file->getEtag(); + } + + /** + * Get the last modification time + * + * @return int + */ + public function getMTime() { + return $this->file->getMTime(); + } + + /** + * Get the content + * + * @return string + */ + public function getContent() { + return $this->file->getContent(); + } + + /** + * Overwrite the file + * + * @param string $data + * @throws NotPermittedException + */ + public function putContent($data) { + $this->file->putContent($data); + } + + /** + * Delete the file + * + * @throws NotPermittedException + */ + public function delete() { + $this->file->delete(); + } + + /** + * Get the MimeType + * + * @return string + */ + public function getMimeType() { + return $this->file->getMimeType(); + } +} diff --git a/lib/private/Files/SimpleFS/Folder.php b/lib/private/Files/SimpleFS/Folder.php new file mode 100644 index 0000000000000..de08d6fa09966 --- /dev/null +++ b/lib/private/Files/SimpleFS/Folder.php @@ -0,0 +1,74 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +namespace OC\Files\SimpleFS; + +use OCP\Files\Node; + +class Folder implements \OCP\Files\SimpleFS\Folder { + + /** @var \OCP\Files\Folder */ + private $folder; + + /** + * Folder constructor. + * + * @param \OCP\Files\Folder $folder + */ + public function __construct(\OCP\Files\Folder $folder) { + $this->folder = $folder; + } + + public function getName() { + return $this->folder->getName(); + } + + public function getDirectoryListing() { + $listing = $this->folder->getDirectoryListing(); + + $fileListing = array_map(function(Node $file) { + return new File($file); + }, $listing); + + return $fileListing; + } + + public function delete() { + $this->folder->delete(); + } + + public function fileExists($name) { + return $this->folder->nodeExists($name); + } + + public function getFile($name) { + $file = $this->folder->get($name); + + return new File($file); + } + + public function newFile($name) { + $file = $this->folder->newFile($name); + + return new File($file); + } +} diff --git a/lib/private/Files/SimpleFS/Root.php b/lib/private/Files/SimpleFS/Root.php new file mode 100644 index 0000000000000..1f2d8fc678389 --- /dev/null +++ b/lib/private/Files/SimpleFS/Root.php @@ -0,0 +1,69 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +namespace OC\Files\SimpleFS; + +use OCP\Files\Node; + +class Root implements \OCP\Files\SimpleFS\Root { + + /** @var \OCP\Files\Folder */ + private $folder; + + /** + * Root constructor. + * + * @param \OCP\Files\Folder $folder + */ + public function __construct(\OCP\Files\Folder $folder) { + $this->folder = $folder; + } + + /** + * @inheritdoc + */ + public function getFolder($name) { + $node = $this->folder->get($name); + + /** @var \OCP\Files\Folder $node */ + return new Folder($node); + } + + /** + * @inheritdoc + */ + public function newFolder($name) { + $folder = $this->folder->newFolder($name); + + return new Folder($folder); + } + + public function getDirectoryListing() { + $listing = $this->folder->getDirectoryListing(); + + $fileListing = array_map(function(Node $file) { + return new Folder($file); + }, $listing); + + return $fileListing; + } +} diff --git a/lib/private/Server.php b/lib/private/Server.php index 494387ab6ca65..cd2cce5cb0bcb 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -359,7 +359,7 @@ public function __construct($webRoot, \OC\Config $config) { $this->registerService('AvatarManager', function (Server $c) { return new AvatarManager( $c->getUserManager(), - $c->getRootFolder(), + $c->getAppDataDir('avatar'), $c->getL10N('lib'), $c->getLogger(), $c->getConfig() @@ -742,6 +742,12 @@ public function __construct($webRoot, \OC\Config $config) { ); return $manager; }); + $this->registerService(\OC\Files\AppData\Factory::class, function (Server $c) { + return new \OC\Files\AppData\Factory( + $c->getRootFolder(), + $c->getSystemConfig() + ); + }); } /** @@ -1456,4 +1462,13 @@ public function getLDAPProvider() { public function getSettingsManager() { return $this->query('SettingsManager'); } + + /** + * @return \OCP\Files\IAppData + */ + public function getAppDataDir($app) { + /** @var \OC\Files\AppData\Factory $factory */ + $factory = $this->query(\OC\Files\AppData\Factory::class); + return $factory->get($app); + } } diff --git a/lib/public/AppFramework/Http/FileDisplayResponse.php b/lib/public/AppFramework/Http/FileDisplayResponse.php index 22171e2b3797c..f48f4c2b840e2 100644 --- a/lib/public/AppFramework/Http/FileDisplayResponse.php +++ b/lib/public/AppFramework/Http/FileDisplayResponse.php @@ -23,7 +23,6 @@ namespace OCP\AppFramework\Http; use OCP\AppFramework\Http; -use OCP\Files\File; /** * Class FileDisplayResponse @@ -33,18 +32,18 @@ */ class FileDisplayResponse extends Response implements ICallbackResponse { - /** @var File */ + /** @var \OCP\Files\File|\OCP\Files\SimpleFS\File */ private $file; /** * FileDisplayResponse constructor. * - * @param File $file + * @param \OCP\Files\File|\OCP\Files\SimpleFS\File $file * @param int $statusCode * @param array $headers * @since 9.2.0 */ - public function __construct(File $file, $statusCode=Http::STATUS_OK, + public function __construct($file, $statusCode=Http::STATUS_OK, $headers=[]) { $this->file = $file; $this->setStatus($statusCode); diff --git a/lib/public/Files/IAppData.php b/lib/public/Files/IAppData.php new file mode 100644 index 0000000000000..e5b673ddb04d3 --- /dev/null +++ b/lib/public/Files/IAppData.php @@ -0,0 +1,29 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +namespace OCP\Files; + +use OCP\Files\SimpleFS\Root; + +interface IAppData extends Root { + +} diff --git a/lib/public/Files/SimpleFS/File.php b/lib/public/Files/SimpleFS/File.php new file mode 100644 index 0000000000000..5e38aef408093 --- /dev/null +++ b/lib/public/Files/SimpleFS/File.php @@ -0,0 +1,85 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +namespace OCP\Files\SimpleFS; + +use OCP\Files\NotPermittedException; + +interface File { + + /** + * Get the name + * + * @return string + */ + public function getName(); + + /** + * Get the size in bytes + * + * @return int + */ + public function getSize(); + + /** + * Get the ETag + * + * @return string + */ + public function getETag(); + + /** + * Get the last modification time + * + * @return int + */ + public function getMTime(); + + /** + * Get the content + * + * @return string + */ + public function getContent(); + + /** + * Overwrite the file + * + * @param string $data + * @throws NotPermittedException + */ + public function putContent($data); + + /** + * Delete the file + * + * @throws NotPermittedException + */ + public function delete(); + + /** + * Get the MimeType + * + * @return string + */ + public function getMimeType(); +} diff --git a/lib/public/Files/SimpleFS/Folder.php b/lib/public/Files/SimpleFS/Folder.php new file mode 100644 index 0000000000000..bf160d20f0950 --- /dev/null +++ b/lib/public/Files/SimpleFS/Folder.php @@ -0,0 +1,75 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +namespace OCP\Files\SimpleFS; + +use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; + +interface Folder { + /** + * Get all the files in a folder + * + * @return File[] + */ + public function getDirectoryListing(); + + /** + * Check if a file with $name exists + * + * @param string $name + * @return bool + */ + public function fileExists($name); + + /** + * Get the file named $name from the folder + * + * @param string $name + * @return File + * @throws NotFoundException + */ + public function getFile($name); + + /** + * Creates a new file with $name in the folder + * + * @param string $name + * @return File + * @throws NotPermittedException + */ + public function newFile($name); + + /** + * Remove the folder and all the files in it + * + * @throws NotPermittedException + */ + public function delete(); + + /** + * Get the folder name + * + * @return string + */ + public function getName(); +} diff --git a/lib/public/Files/SimpleFS/Root.php b/lib/public/Files/SimpleFS/Root.php new file mode 100644 index 0000000000000..98b378fdf4629 --- /dev/null +++ b/lib/public/Files/SimpleFS/Root.php @@ -0,0 +1,51 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ +namespace OCP\Files\SimpleFS; + +use OCP\Files\NotFoundException; + +interface Root { + /** + * Get the folder with name $name + * + * @param string $name + * @return Folder + * @throws NotFoundException + */ + public function getFolder($name); + + /** + * Get all the Folders + * + * @return Folder[] + */ + public function getDirectoryListing(); + + /** + * Create a new folder named $name + * + * @param string $name + * @return Folder + */ + public function newFolder($name); +}