diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..9119de6 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,15 @@ +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.github export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.editorconfig export-ignore +/.php_cs.dist.php export-ignore +/.scrutinizer.yml export-ignore +/.styleci.yml export-ignore +/.travis.yml export-ignore +/phpunit.xml.dist export-ignore +/docs export-ignore +/tests export-ignore diff --git a/README.md b/README.md index 7e1fc95..3101d51 100644 --- a/README.md +++ b/README.md @@ -134,20 +134,16 @@ try { ##### How to get TeamDrive list and IDs ```php -$client = new \Google\Client(); -$client->setClientId([client_id]); -$client->setClientSecret([client_secret]); -$client->refreshToken([refresh_token]); -$client->setApplicationName('My Google Drive App'); - -$service = new \Google\Service\Drive($client); - -$drives = $service->teamdrives->listTeamdrives()->getTeamDrives(); +$drives = $fs->getAdapter()->getService()->teamdrives->listTeamdrives()->getTeamDrives(); foreach ($drives as $drive) { echo 'TeamDrive: ' . $drive->name . PHP_EOL; - echo 'ID: ' . $drive->id . PHP_EOL; + echo 'ID: ' . $drive->id . PHP_EOL. PHP_EOL; } +``` +##### How permanently deletes all of the user's trashed files +```php +$fs->getAdapter()->emptyTrash([]); ``` ## Using with Laravel Framework @@ -205,7 +201,10 @@ Example: ```php namespace App\Providers; -class AppServiceProvider extends \Illuminate\Support\ServiceProvider{ // can be a custom ServiceProvider +use Illuminate\Support\Facades\Storage; +use Illuminate\Support\ServiceProvider; + +class AppServiceProvider extends ServiceProvider { // can be a custom ServiceProvider // ... public function boot(){ // ... @@ -225,6 +224,7 @@ class AppServiceProvider extends \Illuminate\Support\ServiceProvider{ // can be $service = new \Google\Service\Drive($client); $adapter = new \Masbug\Flysystem\GoogleDriveAdapter($service, $config['folder'] ?? '/', $options); $driver = new \League\Flysystem\Filesystem($adapter); + return new \Illuminate\Filesystem\FilesystemAdapter($driver, $adapter); }); } catch(\Exception $e) { diff --git a/composer.json b/composer.json index b675ead..897a342 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,8 @@ "ext-mbstring": "*", "guzzlehttp/guzzle": "^6.3 | ^7.0", "league/flysystem": "^2.1.1", - "google/apiclient": "^2.2" + "google/apiclient": "^2.2", + "guzzlehttp/psr7": "^1.7|^2.0" }, "require-dev": { "phpunit/phpunit": "^8.0 | ^9.3", diff --git a/src/GoogleDriveAdapter.php b/src/GoogleDriveAdapter.php index 4a4e51c..a79e758 100644 --- a/src/GoogleDriveAdapter.php +++ b/src/GoogleDriveAdapter.php @@ -9,6 +9,7 @@ use Google\Service\Drive\DriveFile; use Google\Service\Drive\FileList; use Google\Service\Drive\Permission; +use GuzzleHttp\Psr7\Utils; use League\Flysystem\Config; use League\Flysystem\DirectoryAttributes; use League\Flysystem\FileAttributes; @@ -70,7 +71,7 @@ class GoogleDriveAdapter implements FilesystemAdapter const DIRMIME = 'application/vnd.google-apps.folder'; /** - * Google\Service\Drive instance + * \Google\Service\Drive instance * * @var Drive */ @@ -282,7 +283,7 @@ public function __construct($service, $root = null, $options = []) /** * Gets the service * - * @return Google\Service\Drive + * @return \Google\Service\Drive */ public function getService() { @@ -1020,7 +1021,7 @@ protected function publish($path) $file->setPermissions([$permission]); return true; } - } catch (Exception $e) { + } catch (\Exception $e) { return false; } } @@ -1047,7 +1048,7 @@ protected function unPublish($path) } $file->setPermissions([]); return true; - } catch (Exception $e) { + } catch (\Exception $e) { return false; } } @@ -1122,7 +1123,7 @@ protected function normaliseObject(DriveFile $object, $dirname) break; } } - } catch (Exception $e) { + } catch (\Exception $e) { // Unnecesary } if ($this->useDisplayPaths) { @@ -1206,7 +1207,7 @@ protected function getItems($dirname, $recursive = false, $maxResults = 0, $quer } else { $pageToken = null; } - } catch (Exception $e) { + } catch (\Exception $e) { $pageToken = null; } } while ($pageToken && $maxResults === 0); @@ -1369,11 +1370,7 @@ protected function upload($path, $contents, Config $config, $updating = null) $file->setMimeType($mime); /** @var StreamInterface $stream */ - if (function_exists('\GuzzleHttp\Psr7\stream_for')) { - $stream = /** @scrutinizer ignore-call */ \GuzzleHttp\Psr7\stream_for($contents); - } else { - $stream = \GuzzleHttp\Psr7\Utils::streamFor($contents); - } + $stream = Utils::streamFor($contents); $size = $stream->getSize(); if ($size <= self::MAX_CHUNK_SIZE) { diff --git a/src/StreamableUpload.php b/src/StreamableUpload.php index 5ed6c2c..a4d0482 100644 --- a/src/StreamableUpload.php +++ b/src/StreamableUpload.php @@ -27,6 +27,7 @@ use GuzzleHttp\Psr7\LimitStream; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Uri; +use GuzzleHttp\Psr7\Utils; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; @@ -98,15 +99,7 @@ public function __construct( $this->client = $client; $this->request = $request; $this->mimeType = $mimeType; - if ($data !== null) { - if (function_exists('\GuzzleHttp\Psr7\stream_for')) { - $this->data = /** @scrutinizer ignore-call */ \GuzzleHttp\Psr7\stream_for($data); - } else { - $this->data = \GuzzleHttp\Psr7\Utils::streamFor($data); - } - } else { - $this->data = null; - } + $this->data = $data !== null ? Utils::streamFor($data) : null; $this->resumable = $resumable; $this->chunkSize = is_bool($chunkSize) ? 0 : $chunkSize; $this->progress = 0; @@ -165,11 +158,7 @@ public function nextChunk($chunk = false) } $chunk = new LimitStream($this->data, $this->chunkSize, $this->data->tell()); } else { - if (function_exists('\GuzzleHttp\Psr7\stream_for')) { - $chunk = /** @scrutinizer ignore-call */ \GuzzleHttp\Psr7\stream_for($chunk); - } else { - $chunk = \GuzzleHttp\Psr7\Utils::streamFor($chunk); - } + $chunk = Utils::streamFor($chunk); } $size = $chunk->getSize(); @@ -311,13 +300,8 @@ private function process() } } } - if (function_exists('\GuzzleHttp\Psr7\stream_for')) { - $stream = /** @scrutinizer ignore-call */ \GuzzleHttp\Psr7\stream_for($postBody); - } else { - $stream = \GuzzleHttp\Psr7\Utils::streamFor($postBody); - } - $request = $request->withBody($stream); + $request = $request->withBody(Utils::streamFor($postBody)); if (isset($contentType) && $contentType) { $request = $request->withHeader('content-type', $contentType);