Laravel Google 驱动器集成“masbug/flysystem-google-drive-ext”:“^2.3.0”包不适用于 laravel 11
<?php
namespace App\Admin\Controllers;
use App\Admin\AdminController;
use Illuminate\Support\Facades\Storage;
use Masbug\Flysystem\GoogleDrive\GoogleDriveAdapter;
use Masbug\Flysystem\GoogleDrive\GoogleDriveServiceProvider;
class GoogleDriveController extends AdminController
{
protected $client;
protected $folder_id;
protected $service;
protected $adapter;
protected $serviceProvider;
protected $ClientId = '###';
protected $ClientSecret = '###';
protected $refreshToken = '###';
public function __construct()
{
$this->client = new \Google_Client();
$this->client->setClientId($this->ClientId);
$this->client->setClientSecret($this->ClientSecret);
$this->client->refreshToken($this->refreshToken);
$this->service = new \Google_Service_Drive($this->client);
$this->folder_id = '1zQA5vLXEOnDVQ9ePPKfyiXUPbPxfJhT_';
$this->adapter = new GoogleDriveAdapter([
'clientId' => $this->client->setClientId($this->ClientId),
'clientSecret' => $this->client->setClientSecret($this->ClientSecret),
'refreshToken' => $this->client->refreshToken($this->refreshToken),
$this->service = new \Google_Service_Drive($this->client),
]);
$this->serviceProvider = new GoogleDriveServiceProvider($this->adapter);
// Set the folder ID
$this->folder_id = '1zQA5vLXEOnDVQ9ePPKfyiXUPbPxfJhT_';
}
public function create_folder_in_parent($folderName, $parentFolderID)
{
$fileMetadata = [
'name' => $folderName,
'parents' => [$parentFolderID],
'mimeType' => 'application/vnd.google-apps.folder',
];
$folder = $this->serviceProvider->createDirectory($folderName, $parentFolderID);
return $folder['id'];
}
public function check_if_folder_exists($folderName, $folderID)
{
$folders = $this->serviceProvider->listContents('/', true);
foreach ($folders as $folder) {
if ($folder['type'] == 'dir' && $folder['basename'] == $folderName) {
return true;
}
}
return false;
}
public function upload_image($todayFolderID, $imageToBeUploaded, $imageName)
{
$result = $this->serviceProvider->write($todayFolderID.'/'.$imageName, $imageToBeUploaded);
return $result;
}
public function getShareableLink($fileID)
{
$file = $this->serviceProvider->getMetadata($fileID);
return $file['url'];
}
protected function remove_duplicated($fileName)
{
$files = $this->serviceProvider->listContents('/', true);
foreach ($files as $file) {
if ($file['type'] == 'file' && $file['basename'] == $fileName) {
$this->serviceProvider->delete($file['path']);
}
}
}
public function upload_files()
{
$files = Storage::files();
foreach ($files as $file) {
$this->remove_duplicated($file);
$read = Storage::get($file);
$this->upload_image($this->folder_id, $read, basename($file));
Storage::delete($file);
}
}
public function files_count()
{
$files = $this->serviceProvider->listContents($this->folder_id, true);
return count($files);
}
}
我将 Laravel 6 升级到 Laravel 10
在 Laravel 10 中它不起作用。
请为我提供更好的解决方案。
尝试安装
作曲家需要 masbug/flysystem-google-drive-ext