here is my code in controller
constructor code in
private $drive;
public function __construct(\Google_Client $client)
{
$this->middleware(function ($request, $next) use ($client) {
$accessToken = [
'access_token' => auth()->user()->token,
'created' => auth()->user()->created_at->timestamp,
'expires_in' => auth()->user()->expires_in,
'refresh_token' => auth()->user()->refresh_token
];
$client->setAccessToken($accessToken);
if ($client->isAccessTokenExpired()) {
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
}
Auth::user()->update([
'token' => $client->getAccessToken()['access_token'],
'expires_in' => $client->getAccessToken()['expires_in'],
'created_at' => $client->getAccessToken()['created'],
]);
}
$client->refreshToken(auth()->user()->refresh_token);
$this->drive = new \Google_Service_Drive($client);
return $next($request);
});
}
the method that uploading files to drive I think the problem is this method
function createFile($file, $parent_id = null){
$fileName = FileStorage::find($file)->first();
$name = pathinfo(asset('uploadedfiles/' . $fileName->filenames));
$meta = new \Google_Service_Drive_DriveFile([
'name' => $name['basename'],
'parent' => '1GJ3KC-vsBrLAtlwUYgOvm7AjrtIXb4t-',// Parent Folder ID
]);
$content = File::get('uploadedfiles/' . $fileName->UniqueFileName);
$mime = File::mimeType('uploadedfiles/' . $fileName->UniqueFileName);
$file = $this->drive->files->create($meta, [
'data' => $content,
'mimeType' => $mime,
'uploadType' => 'multipart',
'fields' => 'id',
]);
}
where is the problem in my code? Any help would be appreciated.
CodePudding user response:
The reason it is going to root is that you are not setting a parent folder.
You are using the parameter parent
when in fact the parameter you should be using is parents
and the value should be an array. parent is just getting ignore since its not a valid parameter
change
$meta = new \Google_Service_Drive_DriveFile([
'name' => $name['basename'],
'parent' => '1GJ3KC-vsBrLAtlwUYgOvm7AjrtIXb4t-',// Parent Folder ID
]);
to
$meta = new Drive\DriveFile(array(
'name' => $name['basename'],
'parents' => array('1GJ3KC-vsBrLAtlwUYgOvm7AjrtIXb4t-')
));