-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrive.php
80 lines (63 loc) · 1.73 KB
/
drive.php
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
<?php
include('config.php');
if (isset($token)) {
$client->setAccessToken($token);
$service = new Google_Service_Drive($client);
$files = [];
if (isset($_GET['id']) && isset($_GET['email'])) {
shareFile($_GET['id'], $_GET['email']);
}
if (isset($_FILES['file'])) {
uploadFile($_FILES['file']);
//echo json_encode($_FILES['file']);
}
if (isset($_GET['deleteId'])) {
$service->files->delete($_GET['deleteId']);
}
echo json_encode(['files' => getTotalFiles()]);
}
function uploadFile($uploadfile) {
global $service;
$file = new Google_Service_Drive_DriveFile();
$file->setName($uploadfile['name']);
$file->setMimeType($uploadfile['type']);
$data = file_get_contents($uploadfile['tmp_name']);
$service->files->create($file, array(
'data' => $data,
'mimeType' => $uploadfile['type'],
'uploadType' => 'multipart'
));
}
function shareFile($id, $email) {
global $service;
$role = 'writer';
$userPermission = new Google_Service_Drive_Permission(array(
'type' => 'user',
'role' => $role,
'emailAddress' => $email
));
$request = $service->permissions->create(
$id, $userPermission, array('fields' => 'id')
);
}
function getTotalFiles() {
global $service, $files;
$optParams = array(
'pageSize' => 100,
'fields' => 'nextPageToken, files(id, name)'
);
$results = $service->files->listFiles($optParams);
if (count($results->getFiles()) == 0) {
echo json_encode(['events' => []]);
die();
} else {
foreach ($results->getFiles() as $file) {
$_file = [
'id' => $file->getId(),
'name' => $file->getName()
];
$files[] = $_file;
}
}
return $files;
}