Example #1
1
 public function upload($file, $remote_path, $chunk_size)
 {
     if (!file_exists($file) || !is_readable($file) || !($fp = fopen($file, 'rb'))) {
         throw new \Exception(sprintf(__('The file %s does not seem to exist or is not readable.', 'my-wp-backup'), $file));
     }
     $upload_id = null;
     while (!feof($fp)) {
         try {
             $res = $this->client->put('chunked_upload', null, array('upload_id' => $upload_id, 'offset' => ftell($fp)))->setBody(wpb_get_file_chunk($fp, $chunk_size))->send()->json();
             if (null === $upload_id) {
                 $upload_id = $res['upload_id'];
             }
         } catch (BadResponseException $e) {
             $body = $e->getResponse()->getBody(true);
             error_log($e->getRequest()->getRawHeaders());
             error_log($body);
             throw new \Exception($body);
         }
     }
     $req = $this->client->post('commit_chunked_upload/auto/' . ltrim($remote_path, '/'), null, array('upload_id' => $upload_id, 'overwrite' => 'true'));
     try {
         return $req->send()->json();
     } catch (BadResponseException $e) {
         $body = $e->getResponse()->getBody(true);
         error_log($e->getRequest()->getRawHeaders());
         error_log($body);
         throw new \Exception($body);
     }
 }
Example #2
0
 public function upload_googledrive($options, $settings)
 {
     $this->log(__('Uploading backup via google drive', 'my-wp-backup'));
     $client = Admin\Job::get_drive_client();
     $client->setAccessToken(html_entity_decode($options['token_json']));
     $service = new \Google_Service_Drive($client);
     $data =& $this->destinations['googledrive'];
     $root = '';
     $files = $service->files->listFiles(array('q' => 'mimeType="application/vnd.google-apps.folder" AND trashed=false AND "root" IN parents'));
     /** @var \Google_Service_Drive_DriveFile $driveFolder */
     foreach ($files->getItems() as $driveFolder) {
         if ($driveFolder->getTitle() === self::UPLOAD_ROOT_FOLDER) {
             $root = $driveFolder->getId();
             break;
         }
     }
     $create_folder = function ($title, $parent = null) use($service) {
         $newFolder = new \Google_Service_Drive_DriveFile();
         $newFolder->setTitle($title);
         $newFolder->setMimeType('application/vnd.google-apps.folder');
         if (!is_null($parent)) {
             $parentFolder = new \Google_Service_Drive_ParentReference();
             $parentFolder->setId($parent);
             $newFolder->setParents(array($parentFolder));
         }
         $insert = $service->files->insert($newFolder, array('mimeType' => 'application/vnd.google-apps.folder'));
         return $insert->getId();
     };
     if (empty($root)) {
         $root = $create_folder(self::UPLOAD_ROOT_FOLDER);
     }
     $basedir = new \Google_Service_Drive_ParentReference();
     $basedir->setId($create_folder($this->uniqid, $root));
     $data['parent'] = $basedir->getId();
     $client->setDefer(true);
     foreach ($this->archive->get_archives() as $path) {
         $filename = basename($path);
         $fp = fopen($path, 'rb');
         $status = false;
         $size = filesize($path);
         $chunkSizeBytes = $settings['upload_part'];
         $this->log(sprintf(__('Uploading %s -> %s...', 'my-wp-backup'), $path, $filename), 'debug');
         $file = new \Google_Service_Drive_DriveFile();
         $file->setTitle($filename);
         $file->setParents(array($basedir));
         /** @var \Google_Http_Request $request */
         $request = $service->files->insert($file);
         $media = new \Google_Http_MediaFileUpload($client, $request, '', null, true, $chunkSizeBytes);
         $media->setFileSize($size);
         while (!$status && !feof($fp)) {
             // read until you get $chunkSizeBytes from TESTFILE
             // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
             // An example of a read buffered file is when reading from a URL
             $chunk = wpb_get_file_chunk($fp, $chunkSizeBytes);
             $status = $media->nextChunk($chunk);
         }
         if (false !== $status) {
             $data['files'][$filename] = array('filename' => $filename, 'id' => $status['id']);
         }
         if (is_resource($fp)) {
             fclose($fp);
         }
         $this->log(__('Ok.', 'my-wp-backup'), 'debug');
     }
     $this->log(__('Done google drive upload.', 'my-wp-backup'));
 }