Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64
Your IP : 18.116.24.148
<?php
require_once(__DIR__."/../aws/aws-autoloader.php");
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
header("Access-Control-Allow-Origin: *");
header('Content-Type: multipart/form-data');
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, x-auth-origin, x-auth-token, x-auth-type");
$ds = DIRECTORY_SEPARATOR;
$date = date("dmYhis");
$file_resp = "";
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
die();
}
$req_body = file_get_contents('php://input');
$req_body = json_decode($req_body, true);
$body =$req_body;
$data = $body["img64"];
$name = $body["name"];
$univcode = $body["univcode"];
if($data=="" || $name == "" || $univcode == "") {
$arr['msg'] = "Invalid Params";
$arr['error_code'] = -1;
echo json_encode($arr);
die();
}
if (preg_match('/^data:image\/(\w+);base64,/', $data, $type)) {
$file_name = $name;
$data = substr($data, strpos($data, ',') + 1);
$type = strtolower($type[1]); // jpg, png, gif
if (!in_array($type, [ 'jpg', 'jpeg', 'png', 'pdf' ])) {
$arr['msg'] = "Invalid Image formats";
$arr['error_code'] = -1;
echo json_encode($arr);
die();
}
$data = base64_decode($data);
if ($data === false) {
$arr['msg'] = "base64_decode failed";
$arr['error_code'] = -1;
echo json_encode($arr);
die();
}
$S3filepath = $univcode.'/'.date('Y-m-d-H:i:s', time()).'-'.$file_name;
$s3 = S3Client::factory(
array(
'credentials' => array(
'key' => IAM_KEY,
'secret' => IAM_SECRET
),
'version' => "latest",
'region' => 'ap-south-1'
)
);
$result = $s3->putObject([
'Bucket' => 'university-group-attachment',
'Key' => $S3filepath,
'Body' => $data,
'ContentType' => 'image/' . $type,
]);
$arr['filename'] = $S3filepath;
$arr['error_code'] = 0;
echo json_encode($arr);
die();
} else {
$bin = base64_decode($data, true);
// var_dump($bin);
$file_name = $name;
if (strpos($bin, '%PDF') !== 0) {
$arr['msg'] = "did not match data URI with image data";
$arr['error_code'] = -1;
echo json_encode($arr);
die();
}
$S3filepath = $univcode.'/'.date('Y-m-d-H:i:s', time()).'-'.$file_name;
$s3 = S3Client::factory(
array(
'credentials' => array(
'key' => IAM_KEY,
'secret' => IAM_SECRET
),
'version' => "latest",
'region' => 'ap-south-1'
)
);
$result = $s3->putObject([
'Bucket' => 'university-group-attachment',
'Key' => $S3filepath,
'Body' => $bin,
'ContentType' => 'application/pdf',
]);
$arr['filename'] = $S3filepath;
$arr['error_code'] = 0;
echo json_encode($arr);
die();
}
|