GoogledriveuploadpdfController.php 3.81 KB
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers;
use App\pdfModel;
use URL;
use Storage;
use Response;
use File;
Use PDF;
use Illuminate\Support\Facades\Input;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Facade;
use League\Flysystem\Filesystem;
use Curl\Curl;
use Google_Client;
use Google_Service_Drive;
use Imagick;




class GoogledriveuploadpdfController extends Controller
{

	public function google_drive_upload($folder, $filename,$PdfId,$cust_id)
	{
		session_start();
		$pdffile = new  FileEntryController();
		$pdfFile = $pdffile->getPDF($folder, $filename);

		$user = new pdfModel();
		$user = $user->getGoogleUserOath($cust_id)->get();;



		$client = new \Google_Client();
		$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
		$client->setClientId($user[0]->google_client_id);
		$client->setClientSecret($user[0]->google_client_secret);
		$client->setRedirectUri($user[0]->redirect_uri);
		$client->setAccessType('offline');




      // error_log(print_r($client,true));



			$_SESSION['access_token'] = $client->getAccessToken();


		$client->refreshToken($user[0]->refresh_token);

		$_SESSION['access_token']= $client->getAccessToken();

		$dr_service = new \Google_Service_Drive($client);
		//error_log(print_r($dr_service ,true));



		$file = new \Google_Service_Drive_DriveFile();

		$parent = new \Google_Service_Drive_ParentReference();
		$parent->setId($user[0]->save_folder);
		$file->setParents(array($parent));


		$mimetype = 'application/pdf';
		$uploadType = 'media';
		$filetitle = $filename;
		//$filedescription = 'Taskew ' . $model['discussion_title'];

		$datetime = date('d-m-y-h:s');

		$file->setTitle($datetime . $filetitle);
		$file->setDescription('');
		$file->setMimeType($mimetype);

		$data = $pdfFile;

		$createdFile = $dr_service->files->insert($file, array(
			'data' => $data,
			'mimeType' => $mimetype, //text/plain',
			'uploadType' => $uploadType
		));
		$GoogleId = $createdFile->getId();
		$pdfId = new pdfModel();
		$pdfId = $pdfId->setPdfGoogleId($GoogleId,$PdfId);


	}



	public function GoogleToken()
	{

		session_start();



		$client = new \Google_Client();
		$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
		$client->setClientId('206523860143-kgs80emhfm1sof79nggd48gnhbl1j6ei.apps.googleusercontent.com');
		$client->setClientSecret('ZzEE02Dqz7AKJLSklmL30LNd');
		$client->setRedirectUri('http://localhost:8888/pdf-customizer/public/auth/google/tokenCallback');

		$client->setAccessType('offline');
	
        $client->setApprovalPrompt('force');

		if (isset($_GET['code'])) {
			$client->authenticate($_GET['code']);
			$_SESSION['token'] = $client->getAccessToken();
			$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
			header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
			return;
		}

		if (isset($_SESSION['token'])) {
			$client->setAccessToken($_SESSION['token']);
		}

		if (isset($_REQUEST['logout'])) {
			unset($_SESSION['token']);
			//$client->revokeToken();
		}
		?>
		<!doctype html>
		<html>
		<head>
			<meta charset="utf-8">
		</head>
		<body>
		<header><h1>Get Token</h1></header>
		<?php
		if ($client->getAccessToken()) {
			$_SESSION['token'] = $client->getAccessToken();
			$token = json_decode($_SESSION['token']);
			echo "Access Token = " . $token->access_token . '<br/>';
			echo "Refresh Token = " . $token->refresh_token . '<br/>';
			echo "Token type = " . $token->token_type . '<br/>';
			echo "Expires in = " . $token->expires_in . '<br/>';
			//echo "ID Token = " . $token->id_token . '<br/>';
			echo "Created = " . $token->created . '<br/>';
			echo "<a class='logout' href='?logout'>Logout</a>";
		} else {
			$authUrl = $client->createAuthUrl();
			print "<a class='login' href='$authUrl'>Connect Me!</a>";
		}

	}

}