Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/Db/SwarmFileMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,33 @@ public function __construct(IDBConnection $db) {
parent::__construct($db, self::TABLE_NAME);
}

/**
* @throws Exception
*/
public function findByFileId(string $fileId): ?SwarmFile {
$qb = $this->db->getQueryBuilder();

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

return $this->findEntities($select)[0] ?? null;
}

/**
* @return SwarmFile[]
*
* @throws Exception
*/
public function findAll(string $fileId): array {
public function findAllByStorageId(int $storageId): array {
$qb = $this->db->getQueryBuilder();

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

return $this->findEntities($select);
Expand Down
6 changes: 6 additions & 0 deletions lib/Sabre/WebDavPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,19 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)

break;

case 'export':
$data = $this->EthswarmService->exportReferences($storage);

break;

default:
throw new NotImplemented('Action not implemented');
}

$response->setBody(json_encode([
'status' => true,
'message' => 'success',
'data' => $data,
]));
} catch (Exception $ex) {
$response->setBody(json_encode([
Expand Down
25 changes: 22 additions & 3 deletions lib/Service/EthswarmService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use Exception;
use OC;
use OCA\Files_External_Ethswarm\Db\SwarmFile;
use OCA\Files_External_Ethswarm\Db\SwarmFileMapper;
use OCA\Files_External_Ethswarm\Storage\BeeSwarm;
use OCP\Files\Storage\IStorage;
Expand Down Expand Up @@ -74,12 +75,12 @@ public function setVisibility(string $fileName, int $storageId, int $visibility)
}

public function getToken(string $fileId): string {
$swarmFile = $this->fileMapper->findAll($fileId);
if (0 === count($swarmFile)) {
$swarmFile = $this->fileMapper->findByFileId($fileId);
if (!$swarmFile) {
throw new StorageNotAvailableException($this->l10n->t('No token found'));
}

return $swarmFile[0]->getToken();
return $swarmFile->getToken();
}

public function archiveNode(string $fileName, IStorage $storage): void {
Expand Down Expand Up @@ -147,4 +148,22 @@ public function rename(string $fileName, string $newName, IStorage $storage): vo
throw new StorageNotAvailableException($this->l10n->t('Failed to rename'));
}
}

public function exportReferences(IStorage $storage): array {
$storageId = $storage->getCache()->getNumericStorageId();
$files = $this->fileMapper->findAllByStorageId($storageId);

try {
return array_map(fn (SwarmFile $file) => [
'path' => $file->getName(),
'reference' => $file->getSwarmReference(),
'visibility' => $file->getVisibility(),
'token' => $file->getToken(),
'mimetype' => $file->getMimetype(),
'size' => $file->getSize(),
], $files);
} catch (Exception $e) {
throw new StorageNotAvailableException($this->l10n->t('Failed to export references'));
}
}
}
50 changes: 50 additions & 0 deletions src/app/files/actions/export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { FileAction, registerFileAction } from "@nextcloud/files";
import axios from "@nextcloud/axios";
import { showError, showSuccess } from "@nextcloud/dialogs";
import { emit } from "@nextcloud/event-bus";
import DownloadSvg from "@material-design-icons/svg/filled/download.svg";
import FilesHelper from "@/util/FilesHelper";
import SvgHelper from "@/util/SvgHelper";

registerFileAction(
new FileAction({
id: "exportAction",
displayName(nodes) {
return t(
"files_external_ethswarm",
"Export"
);
},
iconSvgInline(nodes) {
return SvgHelper.convert(DownloadSvg);
},
enabled(files) {
if (files[0].attributes["ethswarm-node"]) {
return FilesHelper.isRoot(files);
}
return false;
},
async exec(node) {
await axios({
method: "post",
url: node.encodedSource,
headers: {
"Hejbit-Action": "export",
},
}).then((response) => {
if (response.data.status === true) {
const blob = new Blob([JSON.stringify(response.data.data)], {
type: "application/json",
});
const storageName = FilesHelper.getStoragePath(node.path);
const date = new Date().toISOString().split("T")[0];
FilesHelper.downloadFile(blob, `hejbit-export-${storageName}-${date}.json`);
showSuccess("Exported references successfully");
} else {
console.error("Error while exporting references", response);
showError(response.data.message);
}
});
},
})
);
8 changes: 8 additions & 0 deletions src/util/FilesHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ const FilesHelper = {
)
.build()
.pick(),
downloadFile: (blob, filename) => {
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
},
};

function getSwarmRef(nodes) {
Expand Down