- Introduced `uploadRoute` to handle image uploads with MIME type validation and size restrictions. - Added `upload_store` class for managing file storage and generating presigned URLs for upload/download. - Enhanced file server to differentiate between PDFs and uploaded files, supporting dynamic content delivery. - Integrated OpenAI module for License Plate Recognition (LPR), including API configuration and schema validation. - Updated core structure with new interfaces (`minio_uploads_i`, `openai_i`) and classes (`openai`, `upload_store`). - Adjusted `index.php` and file routes to support dynamic MIME checks and direct link generation.
62 lines
1.9 KiB
PHP
62 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\authentication;
|
|
use classes\openai;
|
|
use classes\upload_store;
|
|
use objects\logs_o;
|
|
use traits\route_t;
|
|
|
|
|
|
class uploadRoute
|
|
{
|
|
use route_t;
|
|
|
|
protected array $allowedFileTypes = [
|
|
'image/jpeg', 'jpg',
|
|
'image/png', 'png',
|
|
'image/gif', 'gif',
|
|
'image/webp', 'webp',
|
|
];
|
|
|
|
public function run(): void
|
|
{
|
|
$this->post('/upload/image', function () {
|
|
global $response;
|
|
// Get the uploaded file
|
|
$file = $_FILES['file'] ?? null;
|
|
if (!$file) {
|
|
$response->error('No file uploaded');
|
|
}
|
|
// Validate the file type
|
|
$fileType = mime_content_type($file['tmp_name']);
|
|
if (!in_array($fileType, $this->allowedFileTypes)) {
|
|
$response->error('Invalid file type');
|
|
}
|
|
// Validate the file size (optional, e.g., max 5MB)
|
|
if ($file['size'] > 5 * 1024 * 1024) {
|
|
$response->error('File size exceeds the limit of 5MB');
|
|
}
|
|
// Generate a unique object key for the file
|
|
$objectKey = uniqid('image_', true) . '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
|
|
$uploads = new upload_store();
|
|
if(!$file = $uploads->uploadFile(
|
|
$objectKey,
|
|
$file
|
|
)) {
|
|
$response->error('Failed to upload file');
|
|
}
|
|
$response->success($uploads->getPresignedUrl($objectKey));
|
|
});
|
|
$this->get('/openai/test', function () {
|
|
global $response;
|
|
// Validate the input parameters
|
|
// Example image URL for testing
|
|
$image = 'TRUCK_WASH_EXAMPLE_5.jpg';
|
|
// Return the response
|
|
$openai = new openai();
|
|
$response->success($openai->lpr($image));
|
|
});
|
|
}
|
|
} |