Skip to content

Commit 0ff1aed

Browse files
authored
Merge pull request #1856 from hashtopolis/686-rebuild-chunk-cache-helper
added helpers 'rebuildChunkCache' and 'rescanGlobalFiles'
2 parents 831d43d + 4f6acef commit 0ff1aed

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

ci/apiv2/test_chunk.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,13 @@ def test_helper_purge_task(self):
3939

4040
chunks = Chunk.objects.filter(taskId=retval['task'].id)
4141
self.assertEqual(len(chunks), 0)
42+
43+
def test_helper_rebuild_chunk_cache(self):
44+
# Note: it is currently only tested that the call to the helper works, but not that it would fix anything correctly.
45+
# The problem is, that we cannot set the values of chunks and hashlists to "wrong" values via the API.
46+
47+
self.create_test_object()
48+
49+
helper = Helper()
50+
response = helper.rebuild_chunk_cache()
51+
self.assertEqual({"Rebuild": "Success", "correctedChunks": 0, "correctedHashlists": 0}, response)

ci/apiv2/test_file.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,14 @@ def test_range_request_get_file(self):
5757
def test_bulk_delete(self):
5858
files = [self.create_test_object(delete=False) for i in range(5)]
5959
File.objects.delete_many(files)
60+
61+
def test_helper_rescan_global_files(self):
62+
model_obj1 = self.create_test_object()
63+
model_obj2 = self.create_test_object()
64+
65+
helper = Helper()
66+
data = helper.rescan_global_files()
67+
self.assertEqual(data, {"Rescan": "Success"})
68+
69+
check_obj1 = File.objects.get(fileId=model_obj1.id)
70+
self.assertEqual(3, check_obj1.lineCount)

src/api/v2/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,9 @@ public static function addCORSheaders(Request $request, $response) {
357357
require __DIR__ . "/../../inc/apiv2/helper/importCrackedHashes.routes.php";
358358
require __DIR__ . "/../../inc/apiv2/helper/importFile.routes.php";
359359
require __DIR__ . "/../../inc/apiv2/helper/purgeTask.routes.php";
360+
require __DIR__ . "/../../inc/apiv2/helper/rebuildChunkCache.routes.php";
360361
require __DIR__ . "/../../inc/apiv2/helper/recountFileLines.routes.php";
362+
require __DIR__ . "/../../inc/apiv2/helper/rescanGlobalFiles.routes.php";
361363
require __DIR__ . "/../../inc/apiv2/helper/resetChunk.routes.php";
362364
require __DIR__ . "/../../inc/apiv2/helper/resetUserPassword.routes.php";
363365
require __DIR__ . "/../../inc/apiv2/helper/searchHashes.routes.php";
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use DBA\File;
4+
use DBA\Config;
5+
use Psr\Container\ContainerExceptionInterface;
6+
use Psr\Container\NotFoundExceptionInterface;
7+
8+
require_once(dirname(__FILE__) . "/../common/AbstractHelperAPI.class.php");
9+
10+
class RebuildChunkCacheHelperAPI extends AbstractHelperAPI {
11+
public static function getBaseUri(): string {
12+
return "/api/v2/helper/rebuildChunkCache";
13+
}
14+
15+
public static function getAvailableMethods(): array {
16+
return ['POST'];
17+
}
18+
19+
public function getRequiredPermissions(string $method): array {
20+
return [Config::PERM_UPDATE];
21+
}
22+
23+
public function getFormFields(): array {
24+
return [];
25+
}
26+
27+
public static function getResponse(): array {
28+
return ["Rebuild" => "Success"];
29+
}
30+
31+
/**
32+
* Endpoint to recount files for when there is size mismatch
33+
* @param $data
34+
* @return object|array|null
35+
* @throws ContainerExceptionInterface
36+
* @throws NotFoundExceptionInterface
37+
*/
38+
public function actionPost($data): object|array|null {
39+
$result = ConfigUtils::rebuildCache();
40+
$response = $this->getResponse();
41+
$response["correctedChunks"] = $result[0];
42+
$response["correctedHashlists"] = $result[1];
43+
return $response;
44+
}
45+
}
46+
47+
RebuildChunkCacheHelperAPI::register($app);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use DBA\File;
4+
use DBA\Config;
5+
use Psr\Container\ContainerExceptionInterface;
6+
use Psr\Container\NotFoundExceptionInterface;
7+
8+
require_once(dirname(__FILE__) . "/../common/AbstractHelperAPI.class.php");
9+
10+
class RescanGlobalFilesHelperAPI extends AbstractHelperAPI {
11+
public static function getBaseUri(): string {
12+
return "/api/v2/helper/rescanGlobalFiles";
13+
}
14+
15+
public static function getAvailableMethods(): array {
16+
return ['POST'];
17+
}
18+
19+
public function getRequiredPermissions(string $method): array {
20+
return [Config::PERM_UPDATE];
21+
}
22+
23+
public function getFormFields(): array {
24+
return [];
25+
}
26+
27+
public static function getResponse(): array {
28+
return ["Rescan" => "Success"];
29+
}
30+
31+
/**
32+
* Endpoint to recount files for when there is size mismatch
33+
* @param $data
34+
* @return object|array|null
35+
* @throws ContainerExceptionInterface
36+
* @throws NotFoundExceptionInterface
37+
* @throws HTMessages
38+
*/
39+
public function actionPost($data): object|array|null {
40+
ConfigUtils::scanFiles();
41+
return $this->getResponse();
42+
}
43+
}
44+
45+
RescanGlobalFilesHelperAPI::register($app);

0 commit comments

Comments
 (0)