Integrare le Google Drive API con PHP - Step 3

- Pubblicato in coding di - Permalink

Google Workspace

Creare una cartella in Google Drive

    public function createFolderInDrive(array $params = [])
    {
        if (empty($params["folderParent"]) && empty($params["folderName"])) {
            return false;
        }

        try {
            $client = $this->manageDriveClient();
            $service = new Google\Service\Drive($client);

            $wparent = "";
            if (!empty($params["folderParent"])) {
                $wparent = $params["folderParent"];
            } else {
                $leg = $this->loadRootDrive();
                if ($leg !== false && is_array($leg) && !empty($leg) && !empty($leg["id"])) {
                    $wparent = $leg["id"];
                }
            }

            $dataFolder = [
                "name"      =>  $params["folderName"],
                "mimeType"  =>  "application/vnd.google-apps.folder",
                "parents"   =>  [$wparent]
            ];

            $postBody = new Google\Service\Drive\DriveFile($dataFolder);

            $result = $service->files->create($postBody);

            if (!empty($result->id)) {
                return $result->id;
            } else {
                throw new Exception("Errore durante la creazione della cartella.");
            }
        } catch(Exception $e) {
            throw new Exception("Errore durante la creazione della cartella su Google Drive: " . $e->getMessage());
        }

        return false;
    }

Codice Identificativo della Cartella principale su Google Drive

    public function loadRootDrive()
    {
        try {
            $client = $this->manageDriveClient();
            $service = new Google\Service\Drive($client);

            $nfo = $service->files->get('root');
            $res = [
                "kind"      =>  $nfo["kind"],
                "id"        =>  $nfo["id"],
                "name"      =>  $nfo["name"],
                "mimeType"  =>  $nfo["mimeType"]
            ];

            return $res;
        } catch(Exception $e) {
            throw new Exception("Errore durante la ricezione delle informazioni da Google Drive: " . $e->getMessage());
        }

        return false;
    }

Muovere/spostare una cartella o un file

    public function moveFileFromTo(array $params = [])
    {
        if (empty($params["entCode"]) && empty($params["entDest"])) {
            return false;
        }

        try {
            $client = $this->manageDriveClient();
            $service = new Google\Service\Drive($client);

            $file = $service->files->get($params["entCode"], [
                'fields' => 'parents'
            ]);

            $previousParents = implode(',', $file->getParents());

            $emptyFile = new Google\Service\Drive\DriveFile();

            $service->files->update($params["entCode"], $emptyFile, [
                'addParents'    => $params["entDest"],
                'removeParents' => $previousParents,
                'fields'        => 'id, parents'
            ]);

            return true; 
        } catch (Exception $e) {
            return false;
        }
    }

Rinominare un file o una cartella

    public function renameFolderFile(array $params = [])
    {
        if (empty($params["entCode"]) OR empty($params["entName"])) {
            return false;
        }

        $client = $this->manageDriveClient();
        $service = new Google\Service\Drive($client);

        try {
            $file = new Google\Service\Drive\DriveFile();
            $file->setName(trim($params["entName"]));

            $service->files->update($params["entCode"], $file, [
                'fields'    =>  'id, name'
            ]);

            return true;
        } catch(Exception $e) {
             throw new Exception("Errore durante l'operazione di rinomina su Google Drive: " . $e->getMessage());
        }

        return false;
    }

Condividere un file o una cartella

    public function shareFileFolder(array $params = [])
    {
        if (empty($params["fileFolder"]) OR empty($params["mailDest"]) OR empty($params["userRole"])) {
            return false;
        }

        try {
            $client = $this->manageDriveClient();
            $service = new Google\Service\Drive($client);

            $resource = new Google\Service\Drive\Permission([
                'type'          =>  'user',
                'role'          =>  $params["userRole"],
                'emailAddress'  =>  $params["mailDest"]
            ]);

            $optParams = [
                'sendNotificationEmail' => false
            ];

            try {
                $result = $service->permissions->create($params["fileFolder"], $resource, $optParams);
                return true;
            } catch(Exception $e) {
                throw new Exception("Errore durante la condivisione di un elemento su Google Drive: " . $e->getMessage());
            }
        } catch(\Exception $e) {
            throw new Exception("Errore durante la condivisione di un elemento su Google Drive: " . $e->getMessage());
        }

        return false;
    }
→