Skip to content

Commit

Permalink
federated mountpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed May 3, 2021
1 parent a1941d1 commit ed589ed
Show file tree
Hide file tree
Showing 8 changed files with 507 additions and 105 deletions.
2 changes: 1 addition & 1 deletion lib/Command/SharesFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private function displayShares(

foreach ($shareWrappers as $share) {
if (!$filterRecipient) {
$recipient = $share->getInheritedBy();
$recipient = $share->getInitiator();
$sharedTo = $recipient->getDisplayName();
if (!$this->configService->isLocalInstance($recipient->getInstance())) {
$sharedTo .= '@' . $recipient->getInstance();
Expand Down
7 changes: 4 additions & 3 deletions lib/Db/MountRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,17 @@ class MountRequest extends MountRequestBuilder {
/**
* @param Mount $mount
*/
public function create(Mount $mount): void {
public function save(Mount $mount): void {
$hash = $this->token();
$qb = $this->getMountInsertSql();
$qb->setValue('circle_id', $qb->createNamedParameter($mount->getCircleId()))
->setValue('owner', $qb->createNamedParameter($mount->getMember()))
// ->setValue('instance', $qb->createNamedParameter($mount->getInstance()))
->setValue('mount_id', $qb->createNamedParameter($mount->getMountId()))
->setValue('single_id', $qb->createNamedParameter($mount->getOwner()->getSingleId()))
->setValue('token', $qb->createNamedParameter($mount->getToken()))
->setValue('parent', $qb->createNamedParameter($mount->getParent()))
->setValue('mountpoint', $qb->createNamedParameter($mount->getMountPoint()))
->setValue('mountpoint_hash', $qb->createNamedParameter($hash));

$qb->execute();
}

Expand Down
189 changes: 189 additions & 0 deletions lib/FederatedItems/FileShare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

declare(strict_types=1);


/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <[email protected]>
* @copyright 2021
* @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\Circles\FederatedItems;


use daita\MySmallPhpTools\Exceptions\InvalidItemException;
use daita\MySmallPhpTools\Exceptions\UnknownTypeException;
use daita\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22Logger;
use daita\MySmallPhpTools\Traits\TStringTools;
use OCA\Circles\Db\MountRequest;
use OCA\Circles\Exceptions\CircleNotFoundException;
use OCA\Circles\IFederatedItem;
use OCA\Circles\IFederatedItemAsyncProcess;
use OCA\Circles\IFederatedItemMemberEmpty;
use OCA\Circles\Model\Federated\FederatedEvent;
use OCA\Circles\Model\Mount;
use OCA\Circles\Model\ShareWrapper;
use OCA\Circles\Service\ConfigService;


/**
* Class FileShare
*
* @package OCA\Circles\FederatedItems
*/
class FileShare implements
IFederatedItem,
IFederatedItemAsyncProcess,
IFederatedItemMemberEmpty {


use TStringTools;
use TNC22Logger;


/** @var MountRequest */
private $mountRequest;

/** @var ConfigService */
private $configService;


/**
* FileShare constructor.
*
* @param MountRequest $mountRequest
* @param ConfigService $configService
*/
public function __construct(MountRequest $mountRequest, ConfigService $configService) {
$this->mountRequest = $mountRequest;
$this->configService = $configService;
}


/**
* @param FederatedEvent $event
*/
public function verify(FederatedEvent $event): void {
// TODO: check and improve
// TODO: Could we use a share lock ?
}


/**
* @param FederatedEvent $event
*
* @throws InvalidItemException
* @throws UnknownTypeException
* @throws CircleNotFoundException
*/
public function manage(FederatedEvent $event): void {
if ($this->configService->isLocalInstance($event->getSource())) {
return;
}

/** @var ShareWrapper $wrappedShare */
$wrappedShare = $event->getData()->gObj('wrappedShare', ShareWrapper::class);
$mount = new Mount();
$mount->fromShare($wrappedShare);
$mount->setMountId($this->token(15));

$this->mountRequest->save($mount);

// $this->mountRequest->create($mount);
// $circle = $event->getDeprecatedCircle();
//
// // if event is not local, we create a federated file to the right instance of Nextcloud, using the right token
// if (!$this->configService->isLocalInstance($event->getSource())) {
// try {
// $share = $this->getShareFromData($event->getData());
// } catch (Exception $e) {
// return;
// }
//
// $data = $event->getData();
// $token = $data->g('gs_federated');
// $filename = $data->g('gs_filename');
//
// $gsShare = new GSShare($share->getSharedWith(), $token);
// $gsShare->setOwner($share->getShareOwner());
// $gsShare->setInstance($event->getSource());
// $gsShare->setParent(-1);
// $gsShare->setMountPoint($filename);
//
// $this->gsSharesRequest->create($gsShare);
// } else {
// // if the event is local, we send mail to mail-as-members
// $members = $this->membersRequest->forceGetMembers(
// $circle->getUniqueId(), DeprecatedMember::LEVEL_MEMBER, DeprecatedMember::TYPE_MAIL, true
// );
//
// foreach ($members as $member) {
// $this->sendShareToContact($event, $circle, $member->getMemberId(), [$member->getUserId()]);
// }
// }
//
// // we also fill the event's result for further things, like contact-as-members
// $members = $this->membersRequest->forceGetMembers(
// $circle->getUniqueId(), DeprecatedMember::LEVEL_MEMBER, DeprecatedMember::TYPE_CONTACT, true
// );
//
// $accounts = [];
// foreach ($members as $member) {
// if ($member->getInstance() === '') {
// $accounts[] = $this->miscService->getInfosFromContact($member);
// }
// }
//
// $event->setResult(new SimpleDataStore(['contacts' => $accounts]));
}


/**
* @param FederatedEvent $event
* @param array $results
*/
public function result(FederatedEvent $event, array $results): void {
// $event = null;
// $contacts = [];
// foreach (array_keys($events) as $instance) {
// $event = $events[$instance];
// $contacts = array_merge(
// $contacts, $event->getResult()
// ->gArray('contacts')
// );
// }
//
// if ($event === null || !$event->hasCircle()) {
// return;
// }
//
// $circle = $event->getDeprecatedCircle();
//
// foreach ($contacts as $contact) {
// $this->sendShareToContact($event, $circle, $contact['memberId'], $contact['emails']);
// }
}

}

43 changes: 39 additions & 4 deletions lib/Model/FileCacheWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@
namespace OCA\Circles\Model;

use daita\MySmallPhpTools\Db\Nextcloud\nc22\INC22QueryRow;
use daita\MySmallPhpTools\Exceptions\InvalidItemException;
use daita\MySmallPhpTools\IDeserializable;
use daita\MySmallPhpTools\Traits\TArrayTools;
use JsonSerializable;
use OCA\Circles\Db\CoreQueryBuilder;
use OCA\Circles\Exceptions\FileCacheNotFoundException;


/**
* Class ShareWrapper
* Class FileCacheWrapper
*
* @package OCA\Circles\Model
*/
class FileCacheWrapper extends ManagedModel implements INC22QueryRow, JsonSerializable {
class FileCacheWrapper extends ManagedModel implements INC22QueryRow, IDeserializable, JsonSerializable {


use TArrayTools;
Expand Down Expand Up @@ -423,12 +425,12 @@ public function getParent(): int {
/**
* @return array
*/
public function getData(): array {
public function toCache(): array {
return [
'fileid' => $this->getId(),
'path' => $this->getPath(),
'permissions' => $this->getPermissions(),
'storage' => $this->getStorageId(), // strange, is it not !?
'storage' => $this->getStorageId(), // strange, it is not !?
'path_hash' => $this->getPathHash(),
'parent' => $this->getParent(),
'name' => $this->getName(),
Expand Down Expand Up @@ -461,6 +463,39 @@ public function isAccessible(): bool {
}


/**
* @param array $data
*
* @return self
* @throws InvalidItemException
*/
public function import(array $data): IDeserializable {
if ($this->getInt('id', $data) === 0) {
throw new InvalidItemException();
}

$this->setId($this->getInt('id', $data))
->setPath($this->get('path', $data))
->setPermissions($this->getInt('permissions', $data))
->setStorage($this->get('storage', $data))
->setStorageId($this->getInt('storage', $data))
->setPathHash($this->get('pathHash', $data))
->setParent($this->getInt('parent', $data))
->setName($this->get('name', $data))
->setMimeType($this->getInt('mimeType', $data))
->setMimePart($this->getInt('mimePart', $data))
->setSize($this->getInt('size', $data))
->setMTime($this->getInt('mTime', $data))
->setStorageMTime($this->getInt('storageMTime', $data))
->setEncrypted($this->getBool('encrypted', $data))
->setUnencryptedSize($this->getInt('unencryptedSize', $data))
->setEtag($this->get('etag', $data))
->setChecksum($this->get('checksum', $data));

return $this;
}


/**
* @param array $data
* @param string $prefix
Expand Down
6 changes: 3 additions & 3 deletions lib/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private function importIntoShareWrapper(
try {
$initiator = new Member();
$initiator->importFromDatabase($data, $prefix);
$shareWrapper->setInheritedBy($initiator);
$shareWrapper->setInitiator($initiator);
} catch (MemberNotFoundException $e) {
}
break;
Expand All @@ -348,7 +348,7 @@ private function importIntoShareWrapper(
try {
$inheritedBy = new Member();
$inheritedBy->importFromDatabase($data, $prefix);
$shareWrapper->setInheritedBy($inheritedBy);
$shareWrapper->setInitiator($inheritedBy);
} catch (MemberNotFoundException $e) {
}
break;
Expand Down Expand Up @@ -382,7 +382,7 @@ private function importIntoMount(
try {
$member = new Member();
$member->importFromDatabase($data, $prefix);
$mount->setMember($member);
$mount->setOwner($member);
} catch (MemberNotFoundException $e) {
}
break;
Expand Down
Loading

0 comments on commit ed589ed

Please sign in to comment.