Skip to content

Commit

Permalink
implemented helper for recounting the lines in files (#1045)
Browse files Browse the repository at this point in the history
  • Loading branch information
s3inlc authored Feb 24, 2024
1 parent be80815 commit 6a7836f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
8 changes: 8 additions & 0 deletions ci/apiv2/hashtopolis.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,14 @@ def import_cracked_hashes(self, hashlist, source_data, separator):
response = self._helper_request("importCrackedHashes", payload)
return response['data']


def recount_file_lines(self, file):
payload = {
'fileId': file.id,
}
response = self._helper_request("recountFileLines", payload)
return File(**response['data'])

def unassign_agent(self, agent):
payload = {
'agentId': agent.id,
Expand Down
13 changes: 12 additions & 1 deletion ci/apiv2/test_file.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hashtopolis import File
from hashtopolis import File, Helper
from utils import BaseTest


Expand Down Expand Up @@ -28,3 +28,14 @@ def test_expandables(self):
def test_create_binary(self):
model_obj = self.create_test_object(compress=True)
self._test_create(model_obj)

def test_recount_wordlist(self):
# Note: After the object creation, the line count is already updated, but afterward it is immutable on the API.
# There the test just check that the API function is callable and returns the file, but the count is
# already the same beforehand.
model_obj = self.create_test_object()

helper = Helper()
file = helper.recount_file_lines(file=model_obj)

self.assertEqual(file.lineCount, 3)
1 change: 1 addition & 0 deletions src/api/v2/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ public function process(Request $request, RequestHandler $handler): Response {
require __DIR__ . "/../../inc/apiv2/helper/importCrackedHashes.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/importFile.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/purgeTask.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/recountFileLines.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/resetChunk.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/setUserPassword.routes.php";
require __DIR__ . "/../../inc/apiv2/helper/unassignAgent.routes.php";
Expand Down
37 changes: 37 additions & 0 deletions src/inc/apiv2/helper/recountFileLines.routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
use DBA\File;

require_once(dirname(__FILE__) . "/../common/AbstractHelperAPI.class.php");

class RecountFileFilesHelperAPI extends AbstractHelperAPI {
public static function getBaseUri(): string {
return "/api/v2/helper/recountFileLines";
}

public static function getAvailableMethods(): array {
return ['POST'];
}

public function getRequiredPermissions(string $method): array
{
return [File::PERM_UPDATE];
}

public function getFormFields(): array
{
return [
File::FILE_ID => ["type" => "int"],
];
}

public function actionPost($data): array|null {
// first retrieve the file, as fileCountLines does not check any permissions, therfore to be sure call getFile() first, even if it is not required technically
FileUtils::getFile($data[File::FILE_ID], $this->getCurrentUser());

FileUtils::fileCountLines($data[File::FILE_ID]);

return $this->object2Array(FileUtils::getFile($data[File::FILE_ID], $this->getCurrentUser()));
}
}

RecountFileFilesHelperAPI::register($app);

0 comments on commit 6a7836f

Please sign in to comment.