Skip to content

Commit

Permalink
Merge pull request #5 from sd-milani/master
Browse files Browse the repository at this point in the history
Support shared drives and orderBy, fix deprecation warning
  • Loading branch information
vatri authored May 22, 2020
2 parents fe3718c + a8a116a commit 4e84427
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/Resources/config/vatri_google_drive.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ services:

Vatri\GoogleDriveBundle\Controller\VatriGoogleDriveAuthController:
public: true
autowire: true
autoconfigure: true
arguments:
$parameterBag: '@parameter_bag'
$session: '@session'
Expand Down
25 changes: 16 additions & 9 deletions src/Service/DriveApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ public function createFolder($path, $parentId = null): DriveServiceResponse
$fileMetadata = new \Google_Service_Drive_DriveFile($fileParams);

$res = $drive->files->create($fileMetadata, [
'fields' => 'id'
'fields' => 'id',
'supportsAllDrives' => true
]);

$response->setResourceId($res->id);
Expand All @@ -184,7 +185,8 @@ public function folderExists(?string $folderId, bool $inTrash = true): bool
$res = null;
try {
$res = $drive->files->get($folderId, [
'fields' => 'id,trashed'
'fields' => 'id,trashed',
'supportsAllDrives' => true
]);

if ($res->trashed) {
Expand Down Expand Up @@ -212,7 +214,7 @@ public function deleteFile(string $fileId): bool
$drive = $this->buildDrive();

try {
$res = $drive->files->delete($fileId);
$res = $drive->files->delete($fileId, ['supportsAllDrives' => true]);
} catch (\Exception $e) {
return false;
}
Expand All @@ -237,7 +239,7 @@ public function renameResource(string $fileId, string $newName)

try{

$drive->files->update($fileId, $fileMetadata);
$drive->files->update($fileId, $fileMetadata, ['supportsAllDrives' => true]);
return true;

} catch (\Exception $e){
Expand All @@ -252,7 +254,7 @@ public function renameResource(string $fileId, string $newName)
*
* @return \Google_Service_Drive_FileList|null
*/
public function listFiles(?string $parentId = '', ?bool $includeTrashed = true, ?bool $onlyStarred = false): ?\Google_Service_Drive_FileList
public function listFiles(?string $parentId = '', ?bool $includeTrashed = true, ?bool $onlyStarred = false, $orderBy = 'folder,name'): ?\Google_Service_Drive_FileList
{
$drive = $this->getDrive();

Expand All @@ -272,6 +274,9 @@ public function listFiles(?string $parentId = '', ?bool $includeTrashed = true,
$res = $drive->files->listFiles([
'q' => $q,
'fields' => "files/*",
'supportsAllDrives' => true,
'includeItemsFromAllDrives' => true,
'orderBy' => $orderBy
]);

return $res;
Expand All @@ -296,7 +301,7 @@ public function copyFile(string $fileId, ?string $parentId = null): DriveService
}

try {
$res = $drive->files->copy($fileId, $driveFile);
$res = $drive->files->copy($fileId, $driveFile, ['supportsAllDrives' => true]);
$response->setResourceId($res->getId());
} catch (\Exception $e) {
$response->setError($e->getMessage());
Expand All @@ -316,7 +321,8 @@ public function find(string $name, string $parentId = ''): ?Google_Service_Drive
$q .= " and parents in '$parentId' ";
}
$res = $this->generateDrive()->files->listFiles([
'q' => $q
'q' => $q,
'supportsAllDrives' => true
]);

return $res;
Expand Down Expand Up @@ -345,7 +351,8 @@ public function uploadFile(UploadedFile $file, $parentId = null): DriveServiceRe
'data' => file_get_contents($file->getPathname()),
'mimeType' => $file->getClientMimeType(),
'uploadType' => 'multipart',
// 'parents' => $parentId == null ? [] : [$parentId]
// 'parents' => $parentId == null ? [] : [$parentId],
'supportsAllDrives' => true
)
);

Expand All @@ -370,7 +377,7 @@ public function setStarred(string $fileId, bool $starred): DriveServiceResponse
$file = new Google_Service_Drive_DriveFile();
$file->setStarred($starred);
try {
$res = $drive = $this->getDrive()->files->update($fileId, $file);
$res = $drive = $this->getDrive()->files->update($fileId, $file, ['supportsAllDrives' => true]);
$response->setResourceId($res->getId());
} catch (\Exception $e) {
$response->setError($e->getMessage());
Expand Down

0 comments on commit 4e84427

Please sign in to comment.