src/GoogleDriveAdapter.php
protected function ensureDriveFileExists($contents, $parentId, $fileName, $srcDriveFile, $mime)
    {
        if (! $mime) {
            $mime = Util::guessMimeType($fileName, $contents);
        }

        $driveFile = new Google_Service_Drive_DriveFile();

        $mode = 'update';
        if (! $srcDriveFile) {
            $mode = 'insert';
            $driveFile->setName($fileName);
            $driveFile->setParents([$parentId]);
        }

        $driveFile->setMimeType($mime);

        $params = ['fields' => $this->fetchfieldsGet];
        if ($contents) {
            $params['data'] = $contents;
            $params['uploadType'] = 'media';
        }
        if ($mode === 'insert') {
            $retrievedDriveFile = $this->service->files->create($driveFile, $this->applyDefaultParams($params, 'files.create'));
        } else {
            $retrievedDriveFile = $this->service->files->update(
                $srcDriveFile->getId(),
                $driveFile,
                $this->applyDefaultParams($params, 'files.update')
            );
        }

        return $retrievedDriveFile;
    }
protected function ensureDriveFileExists($contents, $parentId, $fileName, $srcDriveFile, $mime)
    {
        if (! $mime) {
            $mime = Util::guessMimeType($fileName, $contents);
        }

        $driveFile = new Google_Service_Drive_DriveFile();

        $mode = 'update';
        if (! $srcDriveFile) {
            $mode = 'insert';
            $driveFile->setName($fileName);
            $driveFile->setParents([$parentId]);
        }

        $driveFile->setMimeType($mime);

        $params = ['fields' => $this->fetchfieldsGet];
        if ($contents) {
            $params['data'] = $contents;
            $params['uploadType'] = 'media';
        }
        if ($mode === 'insert') {
            $retrievedDriveFile = $this->service->files->create($driveFile, $this->applyDefaultParams($params, 'files.create'));
        } else {
            $retrievedDriveFile = $this->service->files->update(
                $srcDriveFile->getId(),
                $driveFile,
                $this->applyDefaultParams($params, 'files.update')
            );
        }

        return $retrievedDriveFile;
    }
protected function ensureDriveFileExists ( $ contents , $ parentId , $ fileName , $ srcDriveFile , $ mime ) { if ( ! $ mime ) { $ mime = Util :: guessMimeType ( $ fileName , $ contents ) ; } $ driveFile = new Google_Service_Drive_DriveFile ( ) ; $ mode = 'update' ; if ( ! $ srcDriveFile ) { $ mode = 'insert' ; $ driveFile -> setName ( $ fileName ) ; $ driveFile -> setParents ( [ $ parentId ] ) ; } $ driveFile -> setMimeType ( $ mime ) ; $ params = [ 'fields' => $ this -> fetchfieldsGet ] ; if ( $ contents ) { $ params [ 'data' ] = $ contents ; $ params [ 'uploadType' ] = 'media' ; } if ( $ mode === 'insert' ) { $ retrievedDriveFile = $ this -> service -> files -> create ( $ driveFile , $ this -> applyDefaultParams ( $ params , 'files.create' ) ) ; } else { $ retrievedDriveFile = $ this -> service -> files -> update ( $ srcDriveFile -> getId ( ) , $ driveFile , $ this -> applyDefaultParams ( $ params , 'files.update' ) ) ; } return $ retrievedDriveFile ; }
Ensure that a file exists on Google Drive by creating it if it doesn't exist or updating it if it does

@param string $contents
@param string $parentId
@param string $fileName
@param string $mime
@return Google_Service_Drive_DriveFile
Ensure that a file exists on Google Drive by creating it if it doesn t exist or updating it if it does