|
Server : Apache/2.4.41 (Ubuntu) System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64 User : www-data ( 33) PHP Version : 8.2.12 Disable Function : NONE Directory : /var/www/parassaas.edukrypt.in/app/Http/Controllers/ |
Upload File : |
<?php
namespace App\Http\Controllers;
// ini_set('memory_limit', -1);
use File;
use AWS\S3\S3Client;
use App\Models\Ebook;
use App\Models\Product;
use App\Models\Category;
use App\Models\Pdf_test;
use App\Models\S3bucket;
use App\Models\Videotag;
use Aws\S3\PostObjectV4;
use Illuminate\Http\Request;
use Aws\Exception\AwsException;
use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client as S3S3Client;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Aws\CognitoIdentity\CognitoIdentityClient;
class EbookController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$data['mainMenu'] = 'ebookManagement';
$data['subMenu'] = 'ebookList';
$data['folder_name'] = env('AWS_EBOOK_FD');
if (isset($_GET['query']) && strlen($_GET['query']) > 1) {
$search_text = $_GET['query'];
$data['products'] = DB::table('ebooks')->where('filename', 'LIKE', '%' . $search_text . '%')->paginate(10);
} else {
$data['products'] = DB::table('ebooks')->paginate(10);
}
return view('admin.ebook.pdf-list', $data);
}
public function mapEbookList()
{
$data['mainMenu'] = 'ebookManagement';
$data['subMenu'] = 'ebookmapList';
$data['folder_name'] = env('AWS_EBOOK_FD');
$data['tags'] = Videotag::get();
if (isset($_GET['query']) && strlen($_GET['query']) > 1) {
$search_text = $_GET['query'];
$data['products'] = DB::table('ebooks_mapping')->where('filename', 'LIKE', '%' . $search_text . '%')->paginate(10);
} else {
$data['products'] = DB::table('ebooks_mapping')->paginate(10);
}
return view('admin.ebook.map-pdf-list', $data);
}
public function addMapEbookList()
{
$data['mainMenu'] = 'ebookManagement';
$data['subMenu'] = 'ebookmapList';
$data['folder_name'] = env('AWS_EBOOK_FD');
if (isset($_GET['query']) && strlen($_GET['query']) > 1) {
$search_text = $_GET['query'];
$data['ebooks'] = DB::table('ebooks')->where('filename', 'LIKE', '%' . $search_text . '%');
//dd($data['ebooks']);
} else {
$data['ebooks'] = DB::table('ebooks')->whereNotIn('etag', function ($query) {
// DB::table('ebooks_mapping')->select('etag')->get();
$query->select('etag')->from('ebooks_mapping');
})->get();
//dd($data['ebooks']);
}
return view('admin.ebook.mapping-pdf', $data);
}
public function saveMapEbook(Request $request)
{
$data['mainMenu'] = 'ebookManagement';
$data['subMenu'] = 'ebookmapList';
$data['folder_name'] = env('AWS_EBOOK_FD');
$arrVal = array();
foreach ($request->ebook_id as $key => $value) {
$customName = $request->customname;
$dataEbook = Ebook::where(['etag' => $value])->first();
if (!empty($dataEbook)) {
$val = array(
'filename' => $dataEbook->filename,
'public_name' => $customName[$key],
'path' => $dataEbook->path,
'etag' => $value,
'size' => $dataEbook->size,
'type' => $dataEbook->type,
'admin_id' => $dataEbook->admin_id,
'created_at' => date('Y-m-d H:i:s')
);
array_push($arrVal, $val);
}
}
$insertEbook = DB::table('ebooks_mapping')->insert($arrVal);
if ($insertEbook) {
return back()->with('success', 'Ebook Mapping Saved!');
} else {
return back()->with('error', 'Something went wrong! Try again.');
}
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$data['mainMenu'] = 'ebookManagement';
$data['subMenu'] = 'ebookList';
$data['folder_name'] = env('AWS_EBOOK_FD');
return view('admin.ebook.upload-pdf', $data);
}
public function aws_temp_url(Request $request)
{
if (empty($request->file)) {
echo json_encode(['code' => 201, 'status' => false]);
exit;
}
$accessKey = env('AWS_S3_ACCESS_KEY');
$secretKey = env('AWS_S3_SECRET_KEY');
$region = env('AWS_REGION');
$host = env('AWS_HOST');
$bucket = env('AWS_BUCKET');
$s3 = new S3S3Client([
'version' => 'latest',
'region' => $region,
'endpoint' => $host,
'credentials' => [
'key' => $accessKey,
'secret' => $secretKey
]
]);
$s3->putBucketCors([
'Bucket' => $bucket, // REQUIRED
'CORSConfiguration' => [ // REQUIRED
'CORSRules' => [ // REQUIRED
[
'AllowedMethods' => ['POST', 'GET', 'HEAD', 'DELETE', 'PUT'], // REQUIRED
'AllowedHeaders' => ['*'],
'AllowedOrigins' => ['*'], // REQUIRED
'ExposeHeaders' => ['ETag'],
'MaxAgeSeconds' => 0
],
],
]
]);
$folderName = env('AWS_EBOOK_FD');
$fileName = $request->file;
$formInputs = ['acl' => 'private'];
$fileType = "application/octet-stream";
$fileKeyVal = $folderName . '/' . $fileName;
$options = [
['acl' => 'private'],
['bucket' => $bucket],
['starts-with', '$key', $fileKeyVal],
['success_action_status' => '201'],
['x-amz-expires' => '3600'],
['Content-Type' => $fileType]
];
$expires = '+2 hours';
$postObject = new PostObjectV4($s3, $bucket, $formInputs, $options, $expires);
$formAttributes = $postObject->getFormAttributes();
$formInputs = $postObject->getFormInputs();
return json_encode(['code' => 200, 'status' => true, 'formAttributes' => $formAttributes, 'formInputs' => $formInputs]);
}
public function listObjects(Request $request)
{
$admin_id = $request->session()->get('loggedIn')['id'];
$accessKey = env('AWS_S3_ACCESS_KEY');
$secretKey = env('AWS_S3_SECRET_KEY');
$region = env('AWS_REGION');
$host = env('AWS_HOST');
$bucket = env('AWS_BUCKET');
$folderName = $_GET['foldername'];
$s3 = new S3S3Client([
'version' => 'latest',
'region' => $region,
'endpoint' => $host,
'credentials' => [
'key' => $accessKey,
'secret' => $secretKey
]
]);
$s3->putBucketCors([
'Bucket' => $bucket, // REQUIRED
'CORSConfiguration' => [ // REQUIRED
'CORSRules' => [ // REQUIRED
[
'AllowedMethods' => ['POST', 'GET', 'HEAD', 'DELETE', 'PUT'], // REQUIRED
'AllowedHeaders' => ['*'],
'AllowedOrigins' => ['*'], // REQUIRED
'ExposeHeaders' => ['ETag'],
'MaxAgeSeconds' => 0
],
],
]
]);
$batchInsert = array();
try {
$videos = $s3->getPaginator('ListObjects', [
'Bucket' => $bucket,
'Prefix' => $folderName
]);
foreach ($videos as $video) {
//dd($video);
foreach ($video['Contents'] as $object) {
$folder = explode('/', $object['Key']);
if ($folder[0] == $folderName && ($object['Size'] > 20)) {
$aInsert = array(
'filename' => basename($object['Key']),
'path' => $object['Key'],
'size' => $object['Size'],
'etag' => trim($object['ETag'], '"'),
'type' => 'pdf',
'admin_id' => $admin_id,
'date' => $object['LastModified']->format(\DateTime::ISO8601),
);
array_push($batchInsert, $aInsert);
}
}
}
if (!empty($batchInsert)) {
Ebook::where(['admin_id' => $admin_id])->delete();
}
Ebook::insert($batchInsert);
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
}
public function deleteAllObject(Request $request)
{
if ($request->method() == 'POST') {
$len = count($request->post('ebookids'));
if ($len > 0) {
$values = $request->post('ebookids');
foreach ($values as $value) {
$this->deleteObject($value);
}
return true;
} else {
return false;
}
}
}
public function deleteObject($etag)
{
// dd($path);
// $jsonPost = file_get_contents('php://input');
// $arrayPost = json_decode($jsonPost);
if (!empty($etag)) {
$videos = Ebook::where(['etag' => $etag])->first();
$path = $videos->path;
$accessKey = env('AWS_S3_ACCESS_KEY');
$secretKey = env('AWS_S3_SECRET_KEY');
$region = env('AWS_REGION');
$host = env('AWS_HOST');
$bucket = env('AWS_BUCKET');
$s3 = new S3S3Client([
'version' => 'latest',
'region' => $region,
'endpoint' => $host,
'credentials' => [
'key' => $accessKey,
'secret' => $secretKey
]
]);
$s3->putBucketCors([
'Bucket' => $bucket, // REQUIRED
'CORSConfiguration' => [ // REQUIRED
'CORSRules' => [ // REQUIRED
[
'AllowedMethods' => ['POST', 'GET', 'HEAD', 'DELETE', 'PUT'], // REQUIRED
'AllowedHeaders' => ['*'],
'AllowedOrigins' => ['*'], // REQUIRED
'ExposeHeaders' => ['ETag'],
'MaxAgeSeconds' => 0
],
],
]
]);
try {
$result = $s3->deleteObject([
'Bucket' => $bucket,
'Key' => $path
]);
// echo json_encode([['code' => 200], ['status' => true]]);
Ebook::where(['etag' => $etag])->delete();
return back()->with('error', 'You Select Category Type!');
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
}
}
// Map delete
public function deleteObjectMap($etag)
{
if (!empty($etag)) {
$ebookData = DB::table('ebooks_mapping')->where(['etag' => $etag])->delete();
if (!empty($ebookData)) {
return back()->with('success', 'Deleted successfully');
} else {
return back()->with('error', 'Something went wrong! try again');
}
}
}
public function deleteAllObjectMap(Request $request)
{
if ($request->method() == 'POST') {
$len = count($request->post('ebookids'));
if ($len > 0) {
$values = $request->post('ebookids');
DB::table('ebooks_mapping')->whereIn('id', $values)->delete();
return true;
} else {
return false;
}
}
}
public function assign_multiple_tags(Request $request)
{
$ebook_ids = $request->ebook_ids;
$tags = $request->tags;
// dd($tags);
if (!empty($ebook_ids)) {
$commaTags = NULL;
if (!empty($tags)) {
$commaTags = implode(',', $tags);
}
$data = array(
'tags' => $commaTags
);
DB::table('ebooks_mapping')->whereIn('id', $ebook_ids)->update($data);
Session::flash('success', 'Tags successfully updated in ebooks!');
return true;
}
return false;
}
}