Added /file-info path for retrieving file hashes and more in the future

This commit is contained in:
Chris Kankiewicz
2019-12-16 21:23:48 -07:00
parent 47de12c0b9
commit 3b93044062
2 changed files with 36 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Controllers;
use RuntimeException;
use Slim\Psr7\Response;
use SplFileInfo;
class FileInfoController
{
/**
* Invoke the FileInfoController.
*
* @param \App\Http\Response $response
* @param string $path
*/
public function __invoke(Response $response, string $path = '.')
{
if (! is_file($path)) {
throw new RuntimeException('Invalid file path', $path);
}
$file = new SplFileInfo($path);
$response->getBody()->write(json_encode([
'hashes' => [
'md5' => hash('md5', file_get_contents($file->getPathname())),
'sha1' => hash('sha1', file_get_contents($file->getPathname())),
'sha256' => hash('sha256', file_get_contents($file->getPathname())),
]
]));
return $response->withHeader('Content-Type', 'application/json');
}
}

View File

@@ -27,6 +27,7 @@ $container->call(Bootstrap\ViewComposer::class);
$app = Bridge::create($container);
// Register routes
$app->get('/file-info/[{path:.*}]', Controllers\FileInfoController::class);
$app->get('/[{path:.*}]', Controllers\DirectoryController::class);
// Enagage!