mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-09-03 10:52:48 +02:00
Added /file-info path for retrieving file hashes and more in the future
This commit is contained in:
35
app/Controllers/FileInfoController.php
Normal file
35
app/Controllers/FileInfoController.php
Normal 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');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user