Skip to content

Commit

Permalink
feat: add config to external storage to auto create user folder
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRuen committed Jul 9, 2024
1 parent caeb458 commit 94c3557
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
41 changes: 19 additions & 22 deletions lib/Event/Listener/UserCreatedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,33 @@ public function handle(Event $event): void
return;
}

// get "Public Filecoin" external storage from config ID
// do this only if public gateway enabled
if ($this->config->getSystemValue('cidgravity_gateway')['is_public_gateway_enabled']) {
$externalStorageID = $this->config->getSystemValue('cidgravity_gateway')['public_external_storage_id'];
$externalStorage = $this->globalStoragesService->getStorage($externalStorageID);
// iterate over all external storages
// if config for external storage autoCreateUserFolder is true, create user folder on it
$externalStorages = $this->globalStoragesService->getStorages();

foreach ($externalStorages as $externalStorage) {
if ($externalStorage->getBackend()->getIdentifier() != "cidgravity") {
$this->logger->error("CIDgravity: unable to create user folder on provided external storage: not an cidgravity external storage");
return;
}

// get associated user and create the folder
$userUID = $event->getUser()->getUID();
$externalStorageMountpoint = $externalStorage->getMountPoint();
$userFolder = $this->rootFolder->getUserFolder($userUID);
$webDavPath = "/{$externalStorageMountpoint}/{$userUID}";
if ($externalStorage->getBackendOption('auto_create_user_folder')) {
$userUID = $event->getUser()->getUID();
$externalStorageMountpoint = $externalStorage->getMountPoint();
$userFolder = $this->rootFolder->getUserFolder($userUID);
$webDavPath = "/{$externalStorageMountpoint}/{$userUID}";

try {
if (!$userFolder->nodeExists($webDavPath)) {
$userFolder->newFolder($webDavPath);
$this->logger->debug("CIDgravity: User folder successfully created on provided external storage");
} else {
$this->logger->debug("CIDgravity: unable to create user folder on provided external storage: already exists");
}
} catch (\Exception $e) {
$this->logger->error("CIDgravity: unable to create user folder on provided external storage: " . $e->getMessage());
try {
if (!$userFolder->nodeExists($webDavPath)) {
$userFolder->newFolder($webDavPath);
$this->logger->debug("CIDgravity: User folder successfully created on provided external storage");
} else {
$this->logger->debug("CIDgravity: unable to create user folder on provided external storage: already exists");
}
} catch (\Exception $e) {
$this->logger->error("CIDgravity: unable to create user folder on provided external storage: " . $e->getMessage());
}
}

} else {
$this->logger->debug("CIDgravity: public gateway not enabled, will not try to create user folder on external storage");
}
}
}
4 changes: 4 additions & 0 deletions lib/Service/BackendService.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public function __construct(IL10N $l, Password $legacyAuth) {
->setType(DefinitionParameter::VALUE_TEXT)
->setDefaultValue('https://ipfs.io/ipfs')
->setTooltip('You can also use your custom gateway or public gateway such as https://dweb.link'),
(new DefinitionParameter('auto_create_user_folder', $l->t('Auto create user folder')))
->setType(DefinitionParameter::VALUE_BOOLEAN)
->setDefaultValue(false)
->setTooltip('Auto create on folder on this external storage when a new user is created'),
])
->addAuthScheme(AuthMechanism::SCHEME_PASSWORD)
->setLegacyAuthMechanism($legacyAuth);
Expand Down

0 comments on commit 94c3557

Please sign in to comment.