Skip to content

Commit

Permalink
Feature/#833 disconnect reconnect storage (#82)
Browse files Browse the repository at this point in the history
* Migration for option of working with multiple tables (swarm_files & swarm_file_tokens)

* Version using swarm file table adding token column fully functional

* Adding token to directory

* Everytime the test function is used all files from the tested storage are copied from swarm table to nc cache

* Fixing index name to avoid colision from token_index hejbit_token_index

* FIxing issue of root folder not found on cache
  • Loading branch information
JoaoSRaposo authored Nov 19, 2024
1 parent 44b069d commit 1f2eb02
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/Db/SwarmFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
* @method int|null getStorage()
* @method void setVisibility(int $visibility)
* @method int getVisibility()
* @method void setToken(string $token)
* @method int getToken()
*/
class SwarmFile extends Entity {

Expand Down Expand Up @@ -81,6 +83,9 @@ class SwarmFile extends Entity {
/** @var int */
protected $visibility;

/** @var string */
protected $token;

public function __construct() {
$this->addType('fileid', 'int');
$this->addType('name', 'string');
Expand All @@ -92,5 +97,6 @@ public function __construct() {
$this->addType('encryptionkey', 'string');
$this->addType('storage', 'int');
$this->addType('visibility', 'int');
$this->addType('token', 'string');
}
}
29 changes: 28 additions & 1 deletion lib/Db/SwarmFileMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ public function findExists(string $name, int $storage): int {
return count($this->findEntities($select));
}

public function createDirectory(string $path, int $storage): SwarmFile {
public function createDirectory(string $path, int $storage, string $token): SwarmFile {
$swarm = new SwarmFile();
$swarm->setName($path);
$swarm->setMimetype(\OC::$server->get(IMimeTypeLoader::class)->getId("httpd/unix-directory"));
$swarm->setSize(1);
$swarm->setStorageMtime(time());
$swarm->setStorage($storage);
$swarm->setToken($token);
return $this->insert($swarm);
}

Expand All @@ -108,6 +109,7 @@ public function createFile(array $filearray): SwarmFile {
$swarm->setSize($filearray["size"]);
$swarm->setStorageMtime($filearray["storage_mtime"]);
$swarm->setStorage($filearray["storage"]);
$swarm->setToken($filearray["token"]);
return $this->insert($swarm);
}

Expand Down Expand Up @@ -146,4 +148,29 @@ public function updatePath(string $path1, string $path2, int $storage): int {
return $qb->executeStatement();
}

/**
* @return SwarmFile[]
*/
public function findAllWithToken(string $token): array {
$qb = $this->db->getQueryBuilder();

$select = $qb
->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('token', $qb->createNamedParameter($token)));

return $this->findEntities($select);
}


public function updateStorageIds(string $token,string $storageid): int {

foreach ($this->findAllWithToken($token) as $swarmFile) {
$swarmFile->setStorage($storageid);
$this->update($swarmFile);
};

return 1;
}

}
91 changes: 91 additions & 0 deletions lib/Migration/Version0005Date202411081430.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2022, MetaProvide Holding EKF
*
* @author Ron Trevor <ecoron@proton.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_External_Ethswarm\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use OCP\DB\Types;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

class Version0005Date202411081430 extends SimpleMigrationStep {
private $db;
public const _TABLENAME = "files_swarm";


public function __construct(IDBConnection $db) {
$this->db = $db;
}


/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$table = $schema->getTable(self::_TABLENAME);

if (!$table->hasColumn('token')) {
$table->addColumn('token', Types::STRING, [
'notnull' => true,
'length' => 64,
'default' => 'none',
]);
$table->addIndex(['token'], 'hejbit_token_index');

}
return $schema;
}

public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
$gqbNI = $this->db->getQueryBuilder();
$updateQb = $this->db->getQueryBuilder();

// Get all the numeric_id's of the swarm storages

$resultNI = $gqbNI->select('numeric_id', 'id')
->from('storages')
->where($gqbNI->expr()->like('id', $gqbNI->createNamedParameter('ethswarm::%')))
->executeQuery();

while ($row = $resultNI->fetch()) {
// Get all the files on each swarm storage
$numeric_id = $row['numeric_id'];
$token_id = $row['id'];

$result = $updateQb->update(self::_TABLENAME)
->set('token', $updateQb->createNamedParameter($token_id))
->where($updateQb->expr()->eq('storage', $updateQb->createNamedParameter($numeric_id)))
->executeStatement();
}

}}
85 changes: 83 additions & 2 deletions lib/Storage/BeeSwarm.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class BeeSwarm extends Common
/** @var string */
protected string $id;

/** @var string */
private string $token;

public function __construct($params)
{
/** @var IL10NFactory $l10nFactory */
Expand All @@ -91,6 +94,7 @@ public function __construct($params)
$this->parseParams($params);
$this->id = 'ethswarm::'.$this->access_key;
$this->storageId = $this->getStorageCache()->getNumericId();
$this->token = $this->getStorageCache()->getStorageId($this->storageId);

// Load handlers
$dbConnection = \OC::$server->get(IDBConnection::class);
Expand Down Expand Up @@ -118,6 +122,7 @@ public function __construct($params)
}
}
}
$this->cacheHandler = new Cache($this);
}

public static function checkDependencies()
Expand All @@ -139,7 +144,82 @@ public function getId(): string
*/
public function test(): bool
{
return $this->checkConnection();
if (!$this->checkConnection()){
return false;
}

$this->filemapper->updateStorageIds($this->token,$this->storageId);
$this->add_rootfolder_cache();
$this->add_token_files_cache();
return true;
}

public function add_rootfolder_cache(): bool
{

$fileData = [
'storage' => $this->storageId,
'path' => '',
'path_hash' => md5(''),
'name' => '',
'mimetype' => 'httpd/unix-directory',
'size' => 1,
'etag' => uniqid(),
'storage_mtime' => time(),
// 2024-11-14 - We still don't support edit, so file is never updated.
'mtime' => time(),
'permissions' => (Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE),
'parent' => -1,
];


$fileId = $this->cacheHandler->put($fileData['path'], $fileData);
return true;
}

/**
* @return bool
* @throws Exception
*/
public function add_file_cache($file): bool
{

$fileData = [
'storage' => $file->getStorage(),
'path' => $file->getName(),
'path_hash' => md5($file->getName()),
'name' => basename($file->getName()),
'mimetype' => $this->mimeTypeHandler->getMimetypeById($file->getMimetype()),
'size' => $file->getSize(),
'etag' => uniqid(),
'storage_mtime' => $file->getStorageMtime(),
// 2024-11-14 - We still don't support edit, so file is never updated.
'mtime' => $file->getStorageMtime(),
];

if ($file->getMimetype() == $this->mimeTypeHandler->getId('httpd/unix-directory'))
$fileData['permissions'] = (Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE);
else
$fileData['permissions'] = (Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE - Constants::PERMISSION_UPDATE);

$fileId = $this->cacheHandler->put($fileData['path'], $fileData);
return true;
}

/**
* @return bool
* @throws Exception
*/
public function add_token_files_cache(): bool
{

foreach ($this->filemapper->findAllWithToken($this->token) as $file) {

$this->add_file_cache($file);
}

return true;

}

/**
Expand Down Expand Up @@ -198,7 +278,7 @@ public function needsPartFile()

public function mkdir($path): bool
{
$this->filemapper->createDirectory($path, $this->storageId);
$this->filemapper->createDirectory($path, $this->storageId,$this->token);
return true;
}

Expand Down Expand Up @@ -497,6 +577,7 @@ public function writeStream(string $path, $stream, int $size = null): int
"etag" => null,
"reference" => $reference,
"storage" => $this->storageId,
"token" => $this->token,
];
$this->filemapper->createFile($uploadfiles);

Expand Down

0 comments on commit 1f2eb02

Please sign in to comment.