Conversie codeigniter 4
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Projects;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
|
||||
class DocumentsController extends BaseController
|
||||
{
|
||||
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
|
||||
{
|
||||
parent::initController($request, $response, $logger);
|
||||
|
||||
if (!defined('AUTHENTICATED') || AUTHENTICATED === false) {
|
||||
die('NO ACCESS');
|
||||
}
|
||||
}
|
||||
|
||||
public function getDocuments(int $foreignID): ResponseInterface
|
||||
{
|
||||
$documents = \Document::getDocuments($foreignID);
|
||||
|
||||
return $this->json($documents);
|
||||
}
|
||||
|
||||
public function downloadDocument(int $documentID): ResponseInterface
|
||||
{
|
||||
$document = \Document::find($documentID);
|
||||
|
||||
$filePath = ROOTPATH . 'documents/' . $document->storagename;
|
||||
|
||||
if (!file_exists($filePath)) {
|
||||
return $this->json(['error' => 'File not found']);
|
||||
}
|
||||
|
||||
$data = file_get_contents($filePath);
|
||||
|
||||
return $this->response->download($document->originalname, $data);
|
||||
}
|
||||
|
||||
public function removeDocument(int $documentID): ResponseInterface
|
||||
{
|
||||
$document = \Document::find($documentID);
|
||||
|
||||
$filePath = ROOTPATH . 'documents/' . $document->storagename;
|
||||
|
||||
if (file_exists($filePath)) {
|
||||
unlink($filePath);
|
||||
}
|
||||
|
||||
\Document::whereId($documentID)->delete();
|
||||
|
||||
return $this->json(['success' => true]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user