Skip to content

Commit 587638c

Browse files
authored
Feature/export references (#139)
* feat: download file blobs - add: method in file helper * feat: export menu action - add: file action enrty * feat: export swarm references data - add: webdav action case - add: service method - refactor: mapper functions - add: mapper find by storage id method - refactor: findAll to findByFileId and null safe it
1 parent 1cae92f commit 587638c

File tree

5 files changed

+103
-5
lines changed

5 files changed

+103
-5
lines changed

lib/Db/SwarmFileMapper.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,33 @@ public function __construct(IDBConnection $db) {
4141
parent::__construct($db, self::TABLE_NAME);
4242
}
4343

44+
/**
45+
* @throws Exception
46+
*/
47+
public function findByFileId(string $fileId): ?SwarmFile {
48+
$qb = $this->db->getQueryBuilder();
49+
50+
$select = $qb
51+
->select('*')
52+
->from($this->getTableName())
53+
->where($qb->expr()->eq('fileId', $qb->createNamedParameter($fileId)))
54+
;
55+
56+
return $this->findEntities($select)[0] ?? null;
57+
}
58+
4459
/**
4560
* @return SwarmFile[]
4661
*
4762
* @throws Exception
4863
*/
49-
public function findAll(string $fileId): array {
64+
public function findAllByStorageId(int $storageId): array {
5065
$qb = $this->db->getQueryBuilder();
5166

5267
$select = $qb
5368
->select('*')
5469
->from($this->getTableName())
55-
->where($qb->expr()->eq('fileId', $qb->createNamedParameter($fileId)))
70+
->where($qb->expr()->eq('storage', $qb->createNamedParameter($storageId)))
5671
;
5772

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

lib/Sabre/WebDavPlugin.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,19 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)
134134

135135
break;
136136

137+
case 'export':
138+
$data = $this->EthswarmService->exportReferences($storage);
139+
140+
break;
141+
137142
default:
138143
throw new NotImplemented('Action not implemented');
139144
}
140145

141146
$response->setBody(json_encode([
142147
'status' => true,
143148
'message' => 'success',
149+
'data' => $data,
144150
]));
145151
} catch (Exception $ex) {
146152
$response->setBody(json_encode([

lib/Service/EthswarmService.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
use Exception;
2828
use OC;
29+
use OCA\Files_External_Ethswarm\Db\SwarmFile;
2930
use OCA\Files_External_Ethswarm\Db\SwarmFileMapper;
3031
use OCA\Files_External_Ethswarm\Storage\BeeSwarm;
3132
use OCP\Files\Storage\IStorage;
@@ -74,12 +75,12 @@ public function setVisibility(string $fileName, int $storageId, int $visibility)
7475
}
7576

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

82-
return $swarmFile[0]->getToken();
83+
return $swarmFile->getToken();
8384
}
8485

8586
public function archiveNode(string $fileName, IStorage $storage): void {
@@ -147,4 +148,22 @@ public function rename(string $fileName, string $newName, IStorage $storage): vo
147148
throw new StorageNotAvailableException($this->l10n->t('Failed to rename'));
148149
}
149150
}
151+
152+
public function exportReferences(IStorage $storage): array {
153+
$storageId = $storage->getCache()->getNumericStorageId();
154+
$files = $this->fileMapper->findAllByStorageId($storageId);
155+
156+
try {
157+
return array_map(fn (SwarmFile $file) => [
158+
'path' => $file->getName(),
159+
'reference' => $file->getSwarmReference(),
160+
'visibility' => $file->getVisibility(),
161+
'token' => $file->getToken(),
162+
'mimetype' => $file->getMimetype(),
163+
'size' => $file->getSize(),
164+
], $files);
165+
} catch (Exception $e) {
166+
throw new StorageNotAvailableException($this->l10n->t('Failed to export references'));
167+
}
168+
}
150169
}

src/app/files/actions/export.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { FileAction, registerFileAction } from "@nextcloud/files";
2+
import axios from "@nextcloud/axios";
3+
import { showError, showSuccess } from "@nextcloud/dialogs";
4+
import { emit } from "@nextcloud/event-bus";
5+
import DownloadSvg from "@material-design-icons/svg/filled/download.svg";
6+
import FilesHelper from "@/util/FilesHelper";
7+
import SvgHelper from "@/util/SvgHelper";
8+
9+
registerFileAction(
10+
new FileAction({
11+
id: "exportAction",
12+
displayName(nodes) {
13+
return t(
14+
"files_external_ethswarm",
15+
"Export"
16+
);
17+
},
18+
iconSvgInline(nodes) {
19+
return SvgHelper.convert(DownloadSvg);
20+
},
21+
enabled(files) {
22+
if (files[0].attributes["ethswarm-node"]) {
23+
return FilesHelper.isRoot(files);
24+
}
25+
return false;
26+
},
27+
async exec(node) {
28+
await axios({
29+
method: "post",
30+
url: node.encodedSource,
31+
headers: {
32+
"Hejbit-Action": "export",
33+
},
34+
}).then((response) => {
35+
if (response.data.status === true) {
36+
const blob = new Blob([JSON.stringify(response.data.data)], {
37+
type: "application/json",
38+
});
39+
const storageName = FilesHelper.getStoragePath(node.path);
40+
const date = new Date().toISOString().split("T")[0];
41+
FilesHelper.downloadFile(blob, `hejbit-export-${storageName}-${date}.json`);
42+
showSuccess("Exported references successfully");
43+
} else {
44+
console.error("Error while exporting references", response);
45+
showError(response.data.message);
46+
}
47+
});
48+
},
49+
})
50+
);

src/util/FilesHelper.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ const FilesHelper = {
9393
)
9494
.build()
9595
.pick(),
96+
downloadFile: (blob, filename) => {
97+
const url = URL.createObjectURL(blob);
98+
const a = document.createElement("a");
99+
a.href = url;
100+
a.download = filename;
101+
a.click();
102+
URL.revokeObjectURL(url);
103+
},
96104
};
97105

98106
function getSwarmRef(nodes) {

0 commit comments

Comments
 (0)