From 28e6ad3a4e935891498ecacf6821fc5e7e688280 Mon Sep 17 00:00:00 2001 From: themohooks <81331307+themohooks@users.noreply.github.com> Date: Fri, 5 Jul 2024 07:26:39 +0300 Subject: [PATCH] create api upload photos --- app/Controllers/Api/Images/Upload.php | 52 +++++++++++++++++++++++++++ app/Controllers/ApiController.php | 4 +++ app/Controllers/MainController.php | 6 ++-- app/Core/Routes.php | 4 +++ app/Services/EXIF.php | 25 +++++++++++++ app/Services/Upload.php | 47 ++++++++++++++++++++++++ views/pages/t.php | 0 7 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 app/Controllers/Api/Images/Upload.php create mode 100644 app/Services/EXIF.php create mode 100644 views/pages/t.php diff --git a/app/Controllers/Api/Images/Upload.php b/app/Controllers/Api/Images/Upload.php new file mode 100644 index 0000000..0b70538 --- /dev/null +++ b/app/Controllers/Api/Images/Upload.php @@ -0,0 +1,52 @@ + $postbody, ':userid' => Auth::userid(), ':time' => time(), ':content' => $content, ':photourl' => self::$photourl, ':exif' => $exif, ':country' => $_POST['country'], ':city' => $_POST['city'])); + echo json_encode( + array( + 'errorcode' => 0, + 'error' => 0 + ) + ); + } + public function __construct() + { + + if ($_FILES['filebody']['error'][0] != 4) { + $exif = new EXIF($_FILES['filebody']['tmp_name']); + $upload = new UploadPhoto($_FILES['filebody'], 'cdn/img'); + if ($upload['type'] !== null) { + $content = Json::return( + array( + 'type' => 'none' + ) + ); + self::$photourl = $upload['src']; + self::create($_POST['descr'], $content, $exif); + } + } else { + echo json_encode( + array( + 'errorcode' => 'FILE_NOTSELECTED', + 'error' => 1 + ) + ); + } + } +} \ No newline at end of file diff --git a/app/Controllers/ApiController.php b/app/Controllers/ApiController.php index 589d381..cf0c9cd 100644 --- a/app/Controllers/ApiController.php +++ b/app/Controllers/ApiController.php @@ -5,6 +5,7 @@ use \App\Services\{Router, Auth, DB, Json}; use \App\Controllers\ExceptionRegister; use \App\Core\Page; use \App\Controllers\Api\{Login, Register}; +use \App\Controllers\Api\Images\{Upload}; class ApiController { @@ -15,5 +16,8 @@ class ApiController public static function register() { return new Register(); } + public static function upload() { + return new Upload(); + } } \ No newline at end of file diff --git a/app/Controllers/MainController.php b/app/Controllers/MainController.php index fb1de1c..782b392 100644 --- a/app/Controllers/MainController.php +++ b/app/Controllers/MainController.php @@ -7,12 +7,10 @@ use \App\Core\Page; class MainController { - public function __invoke() + public static function t() { - - + Page::set('t'); } - public static function i() { diff --git a/app/Core/Routes.php b/app/Core/Routes.php index 7999581..c67f955 100644 --- a/app/Core/Routes.php +++ b/app/Core/Routes.php @@ -12,6 +12,7 @@ class Routes public static function init() { Router::get('/', 'MainController@i'); + Router::get('/t', 'MainController@t'); Router::get('/login', 'LoginController@i'); Router::get('/register', 'RegisterController@i'); Router::get('/photo/$id', 'PhotoController@i'); @@ -21,9 +22,12 @@ class Routes + if (Auth::userid() > 0) { Router::get('/lk', 'ProfileController@lk'); Router::get('/lk/upload', 'ProfileController@upload'); + + Router::post('/api/upload', 'ApiController@upload'); } else { Router::redirect('/login?return='.$_SERVER['HTTP_REFERER']); } diff --git a/app/Services/EXIF.php b/app/Services/EXIF.php new file mode 100644 index 0000000..28ec703 --- /dev/null +++ b/app/Services/EXIF.php @@ -0,0 +1,25 @@ + $section) { + foreach ($section as $name => $val) { + $jsonData["$key.$name"] = $val; + } + } + return json_encode($jsonData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); + } + return null; + } +} diff --git a/app/Services/Upload.php b/app/Services/Upload.php index e69de29..6c80b9f 100644 --- a/app/Services/Upload.php +++ b/app/Services/Upload.php @@ -0,0 +1,47 @@ + NGALLERY['root']['storage']['s3']['credentials']['region'], + 'version' => NGALLERY['root']['storage']['s3']['credentials']['version'], + 'credentials' => [ + 'key' => NGALLERY['root']['storage']['s3']['credentials']['key'], + 'secret' => NGALLERY['root']['storage']['s3']['credentials']['secret'], + ], + 'endpoint' => NGALLERY['root']['storage']['s3']['domains']['gateway'], + ]); + $cstrong = True; + $result = $s3->putObject([ + 'Bucket' => NGALLERY['root']['storage']['s3']['credentials']['bucket'], + 'Key' => $folder, + 'SourceFile' => $file['tmp_name'] + ]); + return [ + 'type' => explode('/', $file['type'])[0], + 'src' => NGALLERY['root']['storage']['s3']['domains']['public'].'/'.$location . $filecdn, + 'size' => self::human_filesize(filesize($file['tmp_name'])), + 'name' => $file['name'] + ]; + } +} diff --git a/views/pages/t.php b/views/pages/t.php new file mode 100644 index 0000000..e69de29