kevacoin-cli/cli/put.php

181 lines
2.8 KiB
PHP
Raw Normal View History

2023-11-16 13:26:01 +02:00
<?php
2024-06-03 22:46:52 +03:00
// Init version
define(
'_CLITOR_IS_',
'1.3.0'
);
2023-11-16 13:26:01 +02:00
// Init helper
function _exec(
string $processor,
string $command
): mixed
{
if (false !== exec(sprintf('%s %s', $processor, $command), $output))
{
$rows = [];
foreach($output as $row)
{
$rows[] = $row;
}
if ($result = @json_decode(implode(PHP_EOL, $rows)))
{
return $result;
}
}
return false;
}
2024-06-03 22:46:52 +03:00
// Check processor exits
if (empty($argv[1]))
{
print(
'processor required' . PHP_EOL
);
exit;
}
if (!file_exists($argv[1]))
{
print(
'processor does not exist' . PHP_EOL
);
exit;
}
2023-11-16 22:17:37 +02:00
// Check file exits
2024-06-03 22:46:52 +03:00
if (empty($argv[2]))
2023-11-16 22:17:37 +02:00
{
2024-06-03 22:46:52 +03:00
print(
'filename required' . PHP_EOL
);
exit;
2023-11-16 22:17:37 +02:00
}
2024-06-03 22:46:52 +03:00
if (!file_exists($argv[2]))
{
print(
'filename does not exist' . PHP_EOL
);
exit;
}
2023-11-16 22:17:37 +02:00
2023-12-22 11:31:04 +02:00
// Get file name
2024-06-03 22:46:52 +03:00
$basename = basename(
2023-12-22 11:31:04 +02:00
$argv[2]
);
// Check filename not longer of protocol
2024-06-03 22:46:52 +03:00
if (mb_strlen($basename) > 255)
2023-12-22 11:31:04 +02:00
{
2024-06-03 22:46:52 +03:00
// Get file hash sum
$basename = md5_file(
$argv[2]
);
2023-12-22 11:31:04 +02:00
2024-06-03 22:46:52 +03:00
// Dump event
print(
'Filename to long, used md5_file as key...' . PHP_EOL
);
}
2023-11-16 22:17:37 +02:00
2024-06-03 22:46:52 +03:00
// Split content to smaller parts (according to the protocol limits)
2023-11-16 22:17:37 +02:00
$pieces = str_split(
base64_encode(
file_get_contents(
$argv[2]
)
),
2024-06-03 22:46:52 +03:00
3072
2023-11-16 22:17:37 +02:00
);
// Count total pieces
$total = count(
$pieces
);
// Create namespace to collect there data pieces
$ns = _exec(
2023-11-16 13:26:01 +02:00
$argv[1],
sprintf(
2024-06-03 22:46:52 +03:00
"keva_namespace '%s'",
str_replace(
"'",
"\'",
$basename
)
2023-11-16 13:26:01 +02:00
)
);
2024-06-03 22:46:52 +03:00
// Validate namespace created
if (empty($ns->namespaceId))
{
print(
'could not create namespace record' . PHP_EOL
);
exit;
}
2023-11-16 13:26:01 +02:00
2024-06-03 22:46:52 +03:00
// Create meta record
2023-11-16 22:17:37 +02:00
print_r(
_exec(
$argv[1],
sprintf(
2024-06-03 22:46:52 +03:00
'keva_put %s %s %s', $ns->namespaceId, '_CLITOR_IS_', _CLITOR_IS_
2023-11-16 22:17:37 +02:00
)
)
);
2023-11-16 13:26:01 +02:00
2023-11-16 22:17:37 +02:00
// Begin pieces saving
foreach ($pieces as $key => $value)
{
print_r(
_exec(
2023-11-16 13:26:01 +02:00
$argv[1],
sprintf(
2023-11-16 22:17:37 +02:00
"%s '%s' '%s' '%s'",
2023-11-16 13:26:01 +02:00
'keva_put',
2023-11-16 22:17:37 +02:00
$ns->namespaceId,
2023-11-16 13:26:01 +02:00
$key,
$value
)
2023-11-16 22:17:37 +02:00
)
);
2023-11-16 14:17:19 +02:00
2023-11-16 22:17:37 +02:00
// Apply delays to prevent too-long-mempool-chain reject
$delay = isset($argv[4]) && $argv[4] > 0 ? (int) $argv[4] : 60;
2024-06-03 22:46:52 +03:00
printf(
2023-11-16 22:17:37 +02:00
'%s/%s sent, waiting %s seconds...' . PHP_EOL,
$key + 1,
$total,
$delay
);
2023-11-16 22:17:37 +02:00
2024-06-03 22:46:52 +03:00
sleep(
$delay
);
2023-11-16 22:17:37 +02:00
}
2024-06-03 22:46:52 +03:00
// Done
printf(
'data successfully sent to namespace "%s"' . PHP_EOL,
$ns->namespaceId
);
printf(
"run to extract: /usr/bin/php '%s/get.php' '%s' '%s' '/path/to/%s'" . PHP_EOL,
2023-11-22 15:33:00 +02:00
__DIR__,
2023-11-16 22:17:37 +02:00
$argv[1],
2024-06-03 22:46:52 +03:00
$ns->namespaceId,
$basename
2023-11-16 22:17:37 +02:00
);