|
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;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Product;
use App\Models\Category;
use App\Models\S3bucket;
use File;
use AWS\S3\S3Client;
use Aws\S3\S3Client as S3S3Client;
use Aws\CognitoIdentity\CognitoIdentityClient;
use Aws\Exception\AwsException;
use Aws\S3\Exception\S3Exception;
use Aws\S3\PostObjectV4;
class VideoController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$data['mainMenu'] = 'video';
$data['subMenu'] = 'videoList';
$data['folder_name'] = env('AWS_VIDEO_FD');
if (isset($_GET['query']) && strlen($_GET['query']) > 1) {
$search_text = $_GET['query'];
$data['products'] = DB::table('s3buckets')->where('filename', 'LIKE', '%' . $search_text . '%')->paginate(10);
return view('admin.video.video-list', $data);
} else {
$data['products'] = DB::table('s3buckets')->paginate(10);
return view('admin.video.video-list', $data);
}
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$data['mainMenu'] = 'video';
$data['subMenu'] = 'videoList';
$data['foldername'] = env('AWS_VIDEO_FD');
return view('admin.video.upload-video', $data);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
// Create URL
public function aws_temp_url(Request $request)
{
//dd($request->file->getMimeType());
if (empty($_POST['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_VIDEO_FD');
$fileName = $request->file;
$fileType = "application/octet-stream";
$formInputs = ['acl' => 'private'];
$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]);
}
// List Videos
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
]);
if (!empty($videos)) {
foreach ($videos as $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' => 'video',
'admin_id' => $admin_id,
'date' => $object['LastModified']->format(\DateTime::ISO8601),
);
array_push($batchInsert, $aInsert);
}
}
}
}
if (!empty($batchInsert)) {
S3bucket::where(['admin_id' => $admin_id])->delete();
}
S3bucket::insert($batchInsert);
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
}
public function deleteAllObject(Request $request)
{
if ($request->method() == 'POST') {
$len = count($request->post('videoids'));
if ($len > 0) {
$values = $request->post('videoids');
foreach ($values as $value) {
$this->deleteObject($value);
}
return true;
} else {
return false;
}
}
}
public function deleteObject($etag)
{
if (!empty($etag)) {
$videos = S3bucket::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]]);
S3bucket::where(['etag' => $etag])->delete();
return back()->with('error', 'You Select Category Type!');
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
}
}
}