mirror of
https://github.com/claradex/nativegallery.git
synced 2024-11-15 03:31:10 +03:00
create api upload photos
This commit is contained in:
parent
e0c2c0152e
commit
28e6ad3a4e
7 changed files with 134 additions and 4 deletions
52
app/Controllers/Api/Images/Upload.php
Normal file
52
app/Controllers/Api/Images/Upload.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controllers\Api\Images;
|
||||
|
||||
|
||||
|
||||
use App\Services\{Auth, Router, GenerateRandomStr, DB, Json, EXIF};
|
||||
use App\Services\Upload as UploadPhoto;
|
||||
use \Aws\S3\MultipartUploader;
|
||||
use \Aws\Exception\MultipartUploadException;
|
||||
|
||||
|
||||
class Upload
|
||||
{
|
||||
static $continue;
|
||||
static $photourl;
|
||||
|
||||
public static function create($postbody, $content, $exif)
|
||||
{
|
||||
DB::query('INSERT INTO photos VALUES (\'0\', :userid, :postbody, :photourl, :time, :exif, 0, :content, :country, :city)', array(':postbody' => $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
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -7,13 +7,11 @@ use \App\Core\Page;
|
|||
|
||||
class MainController
|
||||
{
|
||||
public function __invoke()
|
||||
public static function t()
|
||||
{
|
||||
|
||||
|
||||
Page::set('t');
|
||||
}
|
||||
|
||||
|
||||
public static function i()
|
||||
{
|
||||
Page::set('Main');
|
||||
|
|
|
@ -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']);
|
||||
}
|
||||
|
|
25
app/Services/EXIF.php
Normal file
25
app/Services/EXIF.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
|
||||
class EXIF
|
||||
|
||||
{
|
||||
|
||||
public function __construct($file)
|
||||
{
|
||||
$exif = exif_read_data($file, 0, true);
|
||||
$jsonData = [];
|
||||
|
||||
if ($exif !== false) {
|
||||
foreach ($exif as $key => $section) {
|
||||
foreach ($section as $name => $val) {
|
||||
$jsonData["$key.$name"] = $val;
|
||||
}
|
||||
}
|
||||
return json_encode($jsonData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
|
||||
class Upload
|
||||
|
||||
{
|
||||
|
||||
private static function human_filesize($bytes, $dec = 2): string
|
||||
{
|
||||
|
||||
$size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
|
||||
$factor = floor((strlen($bytes) - 1) / 3);
|
||||
if ($factor == 0)
|
||||
$dec = 0;
|
||||
|
||||
|
||||
return sprintf("%.{$dec}f %s", $bytes / (1024 ** $factor), $size[$factor]);
|
||||
}
|
||||
public function __construct($file, $location)
|
||||
{
|
||||
$filecdn = bin2hex(openssl_random_pseudo_bytes(64, $cstrong)) . '.' . pathinfo($file['name'])['extension'];
|
||||
$folder = $location . $filecdn;
|
||||
$s3 = new \Aws\S3\S3Client([
|
||||
'region' => 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']
|
||||
];
|
||||
}
|
||||
}
|
0
views/pages/t.php
Normal file
0
views/pages/t.php
Normal file
Loading…
Reference in a new issue