GoogledriveuploadpdfController.php
3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?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>";
}
}
}