Skip to content

Commit

Permalink
Rename,Move,Copy,Delete,Encrypt,Edit,Thumb in GD
Browse files Browse the repository at this point in the history
  • Loading branch information
qkqpttgf authored Mar 7, 2021
1 parent 6f9235b commit 1af93af
Showing 1 changed file with 203 additions and 23 deletions.
226 changes: 203 additions & 23 deletions disk/Googledrive.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function __construct($tag) {
}
//$this->oauth_url = 'https://www.googleapis.com/oauth2/v4/';
$this->oauth_url = 'https://accounts.google.com/o/oauth2/';
$this->api_url = 'https://www.googleapis.com/drive/v3/';
$this->api_url = 'https://www.googleapis.com/drive/v3';
$this->scope = 'https://www.googleapis.com/auth/drive';

$this->client_secret = urlencode($this->client_secret);
Expand Down Expand Up @@ -55,7 +55,7 @@ public function list_files($path = '/')

protected function files_format($files)
{
if (isset($files['files'])) {
if (isset($files['files']) || (isset($files['mimeType'])&&$files['mimeType']=='application/vnd.google-apps.folder') ) {
$tmp['type'] = 'folder';
$tmp['id'] = $files['id'];
$tmp['name'] = $files['name'];
Expand Down Expand Up @@ -102,8 +102,8 @@ protected function list_path($path = '/')
if ($path == '') $path = '/';

if (!($files = getcache('path_' . $path, $this->disktag))) {
//$response = curl('GET', $this->api_url . 'drives', '', ['Authorization' => 'Bearer ' . $this->access_token]);
//$response = curl('GET', $this->api_url . 'files?fields=*,files(id,name,mimeType,size,modifiedTime,parents,webContentLink,thumbnailLink),nextPageToken' . (($this->default_drive_id!='')?'&driveId=' . $this->default_drive_id . '&corpora=teamDrive&includeItemsFromAllDrives=true&supportsAllDrives=true':''), '', ['Authorization' => 'Bearer ' . $this->access_token]);
//$response = curl('GET', $this->api_url . '/drives', '', ['Authorization' => 'Bearer ' . $this->access_token]);
//$response = curl('GET', $this->api_url . '/files?fields=*,files(id,name,mimeType,size,modifiedTime,parents,webContentLink,thumbnailLink),nextPageToken' . (($this->default_drive_id!='')?'&driveId=' . $this->default_drive_id . '&corpora=teamDrive&includeItemsFromAllDrives=true&supportsAllDrives=true':''), '', ['Authorization' => 'Bearer ' . $this->access_token]);
if ($path == '/' || $path == '') {
$files = $this->fileList();
//error_log1('root_id' . $files['id']);
Expand Down Expand Up @@ -138,9 +138,9 @@ protected function list_path($path = '/')
//else error_log1('Cant get link:' . json_encode($res, JSON_PRETTY_PRINT));
//}
if ($res['stat']==302) {
$content1 = curl('GET', $weblink, '', ["User-Agent"=>"qkqpttgf/OneManager 3.0.0", "Accept"=>"*/*"], 1);
$content1 = curl('GET', $weblink, '', ["User-Agent"=>"qkqpttgf/OneManager 3.0.0", "Accept"=>"*/*"]);
$item['content'] = $content1;
} else $content1 = $res;
}// else $content1 = $res;
//error_log1($item['name'] . '~' . json_encode($content1, JSON_PRETTY_PRINT) . PHP_EOL);
$parent_folder['files'][$i] = $item;
savecache('path_' . $path, $parent_folder, $this->disktag);
Expand Down Expand Up @@ -175,7 +175,7 @@ protected function list_path($path = '/')
}
protected function fileList($parent_file_id = '')
{
$url = $this->api_url . 'files';
$url = $this->api_url . '/files';

$url .= '?fields=files(id,name,mimeType,size,modifiedTime,parents,webContentLink,thumbnailLink,shared,permissions,permissionIds),nextPageToken';
//$url .= '?fields=files(*),nextPageToken';
Expand All @@ -199,7 +199,7 @@ protected function fileList($parent_file_id = '')
}
protected function permission($op, $fileId)
{
$url = $this->api_url . 'files/' . $fileId . '/permissions';
$url = $this->api_url . '/files/' . $fileId . '/permissions';
if ($op=='create') {
$method = 'POST';
$header['Content-Type'] = 'application/json';
Expand All @@ -220,6 +220,189 @@ protected function permission($op, $fileId)
//error_log1('Set Share' . json_encode($res, JSON_PRETTY_PRINT));
return $res;
}

public function Rename($file, $newname) {
$url = $this->api_url . '/files/' . $file['id'] . '?supportsAllDrives=true';
$tmp['name'] = $newname;
$result = $this->GDAPI('PATCH', $url, json_encode($tmp));
return output(json_encode($this->files_format(json_decode($result['body'], true))), $result['stat']);
}
public function Delete($file) {
$url = $this->api_url . '/files/' . $file['id'] . '?supportsAllDrives=true';
$result = $this->GDAPI('DELETE', $url);
return output(json_encode($this->files_format(json_decode($result['body'], true))), $result['stat']);
}
public function Encrypt($folder, $passfilename, $pass) {
$existfile = $this->list_path($folder['path'] . '/' . $passfilename);
if (isset($existfile['name'])) {
if ($pass==='') {
// 如果为空,删除
$this->Delete(['id'=>$existfile['id']]);
return output('Success', 200);
} else {
$result = $this->editFile($existfile['id'], $pass);
}
} else {
if (!$folder['id']) {
$res = $this->list_path($folder['path']);
//error_log1('res:' . json_encode($res));
$folder['id'] = $res['id'];
}
if (!$folder['id']) {
if ($this->default_drive_id!='') $folder['id'] = $this->default_drive_id;
else $folder['id'] = 'root';
}
$result = $this->createFile_c($folder['id'], $passfilename, $pass);
}

//error_log1('2,url:' . $url .' res:' . json_encode($result));
return output(json_encode($this->files_format(json_decode($result['body'], true))), $result['stat']);
}
public function Move($file, $folder) {
$nowParentId = $this->list_path($file['path'])['id'];
if (!$nowParentId) {
if ($this->default_drive_id!='') $nowParentId = $this->default_drive_id;
else $nowParentId = 'root';
}
if (!$folder['id']) {
$res = $this->list_path($folder['path']);
//error_log1('res:' . json_encode($res));
$folder['id'] = $res['id'];
}
if (!$folder['id']) {
if ($this->default_drive_id!='') $folder['id'] = $this->default_drive_id;
else $folder['id'] = 'root';
}
$url = $this->api_url . '/files/' . $file['id'] . '?removeParents=' . $nowParentId . '&addParents=' . $folder['id'] . '&supportsAllDrives=true';
//$tmp['name'] = $newname;
$result = $this->GDAPI('PATCH', $url);
return output(json_encode($this->files_format(json_decode($result['body'], true))), $result['stat']);
}
public function Copy($file) {
$url = $this->api_url . '/files/' . $file['id'] . '/copy?supportsAllDrives=true';
$namearr = splitlast($file['name'], '.');
date_default_timezone_set('UTC');
if ($namearr[0]!='') {
$newname = $namearr[0] . '_' . date("Ymd\_His");
if ($namearr[1]!='') $newname .= '.' . $namearr[1];
} else {
$newname = '.' . $namearr[1] . '_' . date("Ymd\_His");
}
$tmp['name'] = $newname;
$result = $this->GDAPI('POST', $url, json_encode($tmp));
return output(json_encode($this->files_format(json_decode($result['body'], true))), $result['stat']);
}
public function Edit($file, $content) {
$tmp = splitlast($file['path'], '/');
$folderpath = $tmp[0];
$filename = $tmp[1];
$existfile = $this->list_path($file['path']);
if (isset($existfile['name'])) {
$result = $this->editFile($existfile['id'], $content);
} else {
$result = $this->createFile_c($this->list_path($folderpath)['id'], $filename, $content);
}
//error_log1('edit: ' . json_encode($result, JSON_PRETTY_PRINT));
if ($result['stat']==200) return output('success', 0);
return output(json_encode($this->files_format(json_decode($result['body'], true))), $result['stat']);
}
public function Create($parent, $type, $name, $content = '') {
$filename = spurlencode($name);
$filename = path_format($parent['path'] . '/' . $filename);
$res = $this->list_path($filename);
//error_log1($filename . '查重:' . json_encode($res, JSON_PRETTY_PRINT) . PHP_EOL);
if (isset($res['name'])) {
$tmp['error']['code'] = 'File exist';
$tmp['error']['message'] = $res;
$tmp['error']['stat'] = 403;
return output(json_encode($this->files_format($tmp), JSON_PRETTY_PRINT), 403);
}
if (!$parent['id']) {
$res = $this->list_path($parent['path']);
//error_log1('找ID:' . json_encode($res));
$parent['id'] = $res['id'];
}
if (!$parent['id']) {
if ($this->default_drive_id!='') $parent['id'] = $this->default_drive_id;
else $parent['id'] = 'root';
}

if ($type=='file') {
$result = $this->createFile_c($parent['id'], $name, $content);
}
if ($type=='folder') {
$result = $this->createFolder($parent['id'], $name);
}
//error_log1('data:' . $data . ' res:' . json_encode($result, JSON_PRETTY_PRINT));
//savecache('path_' . $path1, json_decode('{}',true), $_SERVER['disktag'], 1);
return output(json_encode($this->files_format(json_decode($result['body'], true))), $result['stat']);
}
public function get_thumbnails_url($path = '/') {
$res = $this->list_path($path);
$thumb_url = $res['thumbnailLink'];
return $thumb_url;
}
public function bigfileupload($path) {
return output('To Do', 500);
}

protected function editFile($id, $content) {
$url = 'https://www.googleapis.com/upload/drive/v3/files/' . $id . '?uploadType=multipart&supportsAllDrives=true';

$boundary = md5($id . date());
$header['Content-Type'] = 'multipart/related; boundary=' . $boundary;
$header['Authorization'] = 'Bearer ' . $this->access_token;
$data = '--' . $boundary . "\r\n";
$data .= 'Content-Type: application/json; charset=UTF-8' . "\r\n";
$data .= "\r\n";
$data .= "\r\n";
$data .= "\r\n";
$data .= '--' . $boundary . "\r\n";
$data .= 'Content-Type: text/plain' . "\r\n";
$data .= "\r\n";
$data .= $content . "\r\n";
$data .= '--' . $boundary . "--";

$result = curl('PATCH', $url, $data, $header);
//error_log1('url: ' . $url . ' data: ' . $data . ' result: ' . json_encode($result, JSON_PRETTY_PRINT));
return $result;
}
protected function createFile_c($parentId, $name, $content) {
$url = 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&supportsAllDrives=true';
$tmp['name'] = $name;
$tmp['parents'][0] = $parentId;

$boundary = md5($name . date());
$header['Content-Type'] = 'multipart/related; boundary=' . $boundary;
$header['Authorization'] = 'Bearer ' . $this->access_token;
$data = '--' . $boundary . "\r\n";
$data .= 'Content-Type: application/json; charset=UTF-8' . "\r\n";
$data .= "\r\n";
$data .= json_encode($tmp, JSON_PRETTY_PRINT) . "\r\n";
$data .= "\r\n";
$data .= '--' . $boundary . "\r\n";
$data .= 'Content-Type: text/plain' . "\r\n";
$data .= "\r\n";
$data .= $content . "\r\n";
$data .= '--' . $boundary . "--";

$result = curl('POST', $url, $data, $header);
return $result;
}
protected function createFolder($parentId, $name) {
$url = $this->api_url . '/files?&supportsAllDrives=true';

$tmp['name'] = $name;
$tmp['parents'][0] = $parentId;
$tmp['mimeType'] = 'application/vnd.google-apps.folder';
$data = json_encode($tmp);

$header['Content-Type'] = 'application/json';
$header['Authorization'] = 'Bearer ' . $this->access_token;

$result = curl('POST', $url, $data, $header);
return $result;
}

public function AddDisk() {
global $constStr;
Expand Down Expand Up @@ -272,7 +455,7 @@ public function AddDisk() {
if (!$response) return message($this->error['body'], 'Error', $this->error['stat']);
}

$api = $this->api_url . 'drives';
$api = $this->api_url . '/drives';
$arr = curl('GET', $api, '', [ 'Authorization' => 'Bearer ' . $this->access_token ]);
//if (!($arr['stat']==200||$arr['stat']==403||$arr['stat']==400||$arr['stat']==404))
if ($arr['stat']!=200) return message($arr['stat'] . json_encode(json_decode($arr['body']), JSON_PRETTY_PRINT), 'Get followedSites', $arr['stat']);
Expand Down Expand Up @@ -516,16 +699,12 @@ protected function get_access_token($refresh_token) {
return true;
}

protected function GDAPI($method, $path, $data = '')
protected function GDAPI($method, $url, $data = '')
{
if (substr($path,0,7) == 'https://' or substr($path,0,8) == 'https://') {
$url=$path;
$lenth=strlen($data);
$headers['Content-Length'] = $lenth;
$lenth--;
$headers['Content-Range'] = 'bytes 0-' . $lenth . '/' . $headers['Content-Length'];
/*if (substr($path,0,7) == 'https://' or substr($path,0,8) == 'https://') {
$url = $path;
} else {
$url = $this->api_url . $this->ext_api_url;
$url = $this->api_url;
if ($path=='' or $path=='/') {
$url .= '/';
} else {
Expand Down Expand Up @@ -554,9 +733,10 @@ protected function GDAPI($method, $path, $data = '')
$method='POST';
$headers['Content-Type'] = 'application/json';
}
}
}*/
$headers['Authorization'] = 'Bearer ' . $this->access_token;
if (!isset($headers['Accept'])) $headers['Accept'] = '*/*';
$headers['Content-Type'] = 'application/json';
//if (!isset($headers['Referer'])) $headers['Referer'] = $url;*
$sendHeaders = array();
foreach ($headers as $headerName => $headerVal) {
Expand All @@ -575,13 +755,13 @@ protected function GDAPI($method, $path, $data = '')
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $sendHeaders);
$response['body'] = curl_exec($ch);
$response['stat'] = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$response['stat'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//$response['Location'] = curl_getinfo($ch);
curl_close($ch);
error_log1($response['stat'].'
'.$response['body'].'
'.$url.'
');
/*error_log1($response['stat'].'
' . $response['body'].'
' . $url.'
' . $data);*/
return $response;
}

Expand Down

0 comments on commit 1af93af

Please sign in to comment.