Skip to content

Commit

Permalink
Release 0.5.9: Prs #88, #92, #93, #94 (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
frozenbanana authored Dec 11, 2024
1 parent 1c1a42f commit 0c31797
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 76 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Experience the future of data storage with 5GB of free, decentralized storage on
[Get your free licence here!](https://metaprovide.org/hejbit/start) .
]]></description>
<version>0.5.8</version>
<version>0.5.9</version>
<licence>agpl</licence>
<author>MetaProvide</author>
<namespace>Files_External_Ethswarm</namespace>
Expand Down
92 changes: 60 additions & 32 deletions lib/Sabre/PostPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,77 @@
use Sabre\HTTP\ResponseInterface;
use OCA\Files_External_Ethswarm\Service\EthswarmService;

class PostPlugin extends ServerPlugin {
/** @var Server */
private $server;
class PostPlugin extends ServerPlugin
{
/** @var Server */
private $server;

/** @var EthswarmService */
private $EthswarmService;
/** @var EthswarmService */
private $EthswarmService;

public function __construct(EthswarmService $service) {
$this->EthswarmService = $service;
}
public function __construct(EthswarmService $service)
{
$this->EthswarmService = $service;
}

public function initialize(Server $server) {
$this->server = $server;
$this->server->on('method:POST', [$this, 'httpPost']);
}
public function initialize(Server $server)
{
$this->server = $server;
$this->server->on('method:POST', [$this, 'httpPost']);
}

public function httpPost(RequestInterface $request, ResponseInterface $response)
{
$action = $request->getRawServerValue('HTTP_HEJBIT_ACTION') ?? null;
public function httpPost(RequestInterface $request, ResponseInterface $response)
{
$action = $request->getRawServerValue('HTTP_HEJBIT_ACTION') ?? null;

if ($action === 'unview') {
$path = $request->getPath();
$node = $this->server->tree->getNodeForPath($path);
if (($node instanceof \OCA\DAV\Connector\Sabre\File)) {
$storageid = $node->getFileInfo()->getStorage()->getCache()->getNumericStorageId();
$filename = $node->getFileInfo()->getinternalPath();
}
if ($action === 'hide') {
$path = $request->getPath();
$node = $this->server->tree->getNodeForPath($path);
if (($node instanceof \OCA\DAV\Connector\Sabre\File)) {
$storageid = $node->getFileInfo()->getStorage()->getCache()->getNumericStorageId();
$filename = $node->getFileInfo()->getinternalPath();
}
if (($node instanceof \OCA\DAV\Connector\Sabre\Directory)) {
$storageid = $node->getFileInfo()->getStorage()->getCache()->getNumericStorageId();
$filename = $node->getFileInfo()->getinternalPath();
}
$storageid = $node->getFileInfo()->getStorage()->getCache()->getNumericStorageId();
$filename = $node->getFileInfo()->getinternalPath();
}

$class = $this->EthswarmService;

$class->setVisiblity($filename, $storageid, 0);

$response->setStatus(200);
$response->setHeader('Content-Length', '0');

$class = $this->EthswarmService;
return false;
}
else if ($action === 'unhide') {
$path = $request->getPath();
$node = $this->server->tree->getNodeForPath($path);
if (($node instanceof \OCA\DAV\Connector\Sabre\File)) {
$storageid = $node->getFileInfo()->getStorage()->getCache()->getNumericStorageId();
$filename = $node->getFileInfo()->getinternalPath();
}
if (($node instanceof \OCA\DAV\Connector\Sabre\Directory)) {
$storageid = $node->getFileInfo()->getStorage()->getCache()->getNumericStorageId();
$filename = $node->getFileInfo()->getinternalPath();
}

$class->setVisiblity($filename, $storageid, 0);
$this->EthswarmService->setVisiblity($filename, $storageid, 1);

$response->setStatus(200);
$response->setHeader('Content-Length', '0');
$response->setStatus(200);
$response->setHeader('Content-Length', '0');

return false;
} else {
return false;
}
// No Hejbit-action, allow other plugins to handle the request
else if ($action === null) {
return true;
}
// Invalid Hejbit-action, throw exception
else {
$exMessage = 'There was no plugin in the system that was willing to handle a POST method with this action > '.$action.'.';
throw new \Sabre\DAV\Exception\NotImplemented($exMessage);
}
}
}
}
106 changes: 86 additions & 20 deletions lib/Storage/BeeSwarm.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCP\Constants;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\IMimeTypeLoader;
use OCP\Files\IMimeTypeDetector;
use OCP\IConfig;
use OCP\IDBConnection;
use Sabre\DAV\Exception\BadRequest;
Expand All @@ -42,6 +43,8 @@
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Notification\IManager;
use Psr\Log\LoggerInterface;


class BeeSwarm extends Common
{
Expand Down Expand Up @@ -71,6 +74,9 @@ class BeeSwarm extends Common
/** @var \OCP\Files\IMimeTypeLoader */
private IMimeTypeLoader $mimeTypeHandler;

/** @var \OCP\Files\IMimeTypeDetector */
private IMimeTypeDetector $mimeTypeDetector;

/** @var \OC\Files\Cache\Cache */
private Cache $cacheHandler;

Expand All @@ -83,23 +89,30 @@ class BeeSwarm extends Common
/** @var string */
private string $token;

/** @var LoggerInterface */
private LoggerInterface $logger;

public function __construct($params)
{
/** @var IL10NFactory $l10nFactory */
$l10nFactory = \OC::$server->get(IL10NFactory::class);
$this->l10n = $l10nFactory->get(AppConstants::APP_NAME);

$this->notificationService = new NotificationService( \OC::$server->get(IManager::class), \OC::$server->get(IUserManager::class), \OC::$server->get(IUserSession::class));
$this->notificationService = new NotificationService(\OC::$server->get(IManager::class), \OC::$server->get(IUserManager::class), \OC::$server->get(IUserSession::class));

$this->parseParams($params);
$this->id = 'ethswarm::'.$this->access_key;
$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);
$this->filemapper = new SwarmFileMapper($dbConnection);
$this->mimeTypeHandler = \OC::$server->get(IMimeTypeLoader::class);
$this->mimeTypeDetector = \OC::$server->get(IMimeTypeDetector::class);

// Get logger
$this->logger = \OC::$server->get(LoggerInterface::class);

$mountHandler = \OC::$server->get(IUserMountCache::class);
$storageMounts = $mountHandler->getMountsForStorageId($this->storageId);
Expand Down Expand Up @@ -144,16 +157,57 @@ public function getId(): string
*/
public function test(): bool
{
if (!$this->checkConnection()){
if (!$this->checkConnection()) {
return false;
}

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

public function copy($path1, $path2)
{
try {
// Get the source file from the mapper
$sourceFile = $this->filemapper->find($path1, $this->storageId);
if (!$sourceFile) {
$this->logger->error(
'copy failed: source file not found in mapper ' . $path1,
['app' => AppConstants::APP_NAME]
);
return false;
}

// Prepare the data for the new file
$copyData = [];
$copyData["name"] = $path2;
$copyData["reference"] = $sourceFile->getSwarmReference();
$copyData["etag"] = null;
$copyData["mimetype"] = $sourceFile->getMimetype();
$copyData["size"] = $sourceFile->getSize();
$copyData["storage_mtime"] = time();
$copyData["storage"] = $this->storageId;
$copyData["token"] = $this->token;

// Create the new file entry in the mapper
$newFile = $this->filemapper->createFile($copyData);

if (!$newFile) {
$this->logger->error(
'copy failed: failed to create new file in mapper ' . $path2,
['app' => AppConstants::APP_NAME]
);
return false;
}

return true;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}

public function add_rootfolder_cache(): bool
{

Expand Down Expand Up @@ -278,7 +332,7 @@ public function needsPartFile()

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

Expand Down Expand Up @@ -311,6 +365,8 @@ public function is_dir($path)
$data = $this->getMetaData($path);
if ($data['mimetype'] === 'httpd/unix-directory') {
return true;
} else {
return false;
}
}

Expand Down Expand Up @@ -466,11 +522,11 @@ public function getDirectDownload($path)
}

/* Enabling this function causes a fatal exception "Call to a member function getId() on null /var/www/html/lib/private/Files/Mount/MountPoint.php - line 276: OC\Files\Cache\Wrapper\CacheWrapper->getId("")
public function getCache($path = '', $storage = null)
{
public function getCache($path = '', $storage = null)
{
}
*/
}
*/

/**
* @param string $path
Expand All @@ -489,18 +545,24 @@ public function getMetaData($path)
$data['size'] = 0; //unknown
$data['etag'] = null;
}
// If not in swarm table, assume it's a folder
// If not in swarm table, check if it is a file or directory
$exists = $this->filemapper->findExists($path, $this->storageId) !== 0;
if (!$exists) {
// Create a folder item
$data['name'] = $path;
// Folder permissions should allow renaming so PERMISSION_UPDATE is included.
$data['permissions'] = (Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE);
$data['mimetype'] = 'httpd/unix-directory';
$data['mtime'] = time();
$data['storage_mtime'] = time();
$data['size'] = 1; //unknown
$data['etag'] = uniqid();

// Check if this is likely a file based on extension
$isFile = pathinfo($path, PATHINFO_EXTENSION) !== '';

if ($isFile) {
$data['mimetype'] = $this->mimeTypeDetector->detectPath($path);
$data['size'] = 0;
} else {
// Directory logic
$data['mimetype'] = 'httpd/unix-directory';
$data['size'] = 1;
}
} else {
// Get record from table
$swarmFile = $this->filemapper->find($path, $this->storageId);
Expand All @@ -519,14 +581,18 @@ public function getMetaData($path)
$data['etag'] = uniqid();
$data['swarm_ref'] = $swarmFile->getSwarmReference();
}

return $data;
}

public function getDirectoryContent($path): \Traversable
{
$rows = $this->filemapper->getPathTree($path, $this->storageId,
incSelf: false,
recursive: false);
$rows = $this->filemapper->getPathTree(
$path,
$this->storageId,
incSelf: false,
recursive: false
);
$content = array_map(fn($val) => $this->getMetaData($val->getName()), $rows);

return new \ArrayIterator($content);
Expand All @@ -549,7 +615,7 @@ public function writeStream(string $path, $stream, int $size = null): int
$tmpFilesize = (file_exists($tmpFile) ? filesize($tmpFile) : -1);
$mimetype = mime_content_type($tmpFile);

if (str_ends_with(strtolower($path),'.md' )){
if (str_ends_with(strtolower($path), '.md')) {
$mimetype = "text/markdown";
}

Expand Down
Loading

0 comments on commit 0c31797

Please sign in to comment.