예제 #1
0
/**
 * Insert new file.
 *
 * @param Google_Service_Drive $service Drive API service instance.
 * @param string $title Title of the file to insert, including the extension.
 * @param string $description Description of the file to insert.
 * @param string $parentId Parent folder's ID.
 * @param string $mimeType MIME type of the file to insert.
 * @param string $filename Filename of the file to insert.
 * @return Google_Service_Drive_DriveFile The file that was inserted. NULL is
 *     returned if an API error occurred.
 */
function insertFile($service, $title, $description, $parentId, $mimeType, $filename, $properties = [])
{
    $file = new Google_Service_Drive_DriveFile();

    $file->setTitle($title);
    $file->setDescription($description);
    $file->setMimeType($mimeType);
    $file->setProperties($properties);

    // Set the parent folder.
    if ($parentId != null) {
        $parent = new Google_Service_Drive_ParentReference();
        $parent->setId($parentId);
        $file->setParents(array($parent));
    }

    try {
        $data = file_get_contents($filename);

        $createdFile = $service->files->insert($file, array(
            'data' => $data,
            'mimeType' => $mimeType,
            'uploadType' => 'media',
            'convert' => true,
        ));

        // Uncomment the following line to print the File ID
        // print 'File ID: %s' % $createdFile->getId();

        return $createdFile;
    } catch (Exception $e) {
        catchGoogleExceptions($e);
    }
}
 private function _upload_chunk($files, &$results, $force_rewrite, $timeout_time, $allow_refresh_token)
 {
     list($result, $listed_files) = $this->list_files_chunk($files, $allow_refresh_token, $timeout_time);
     if ($result != 'success') {
         return $result;
     }
     // remove dups
     $files_by_title = array();
     for ($n = 0; $n < count($listed_files); $n++) {
         $title_to_search = $listed_files[$n]->title;
         $files_by_title[$title_to_search] = $listed_files[$n];
         for ($m = $n + 1; $m < count($listed_files); $m++) {
             if ($listed_files[$m]->title == $title_to_search) {
                 try {
                     $this->_service->files->delete($listed_files[$m]->id);
                 } catch (\Google_Service_Exception $e) {
                     $errors = $e->getErrors();
                     $details = '';
                     if (count($errors) >= 1) {
                         if ($errors[0]['reason'] == 'notFound') {
                             continue;
                         } else {
                             $details = $errors[0]['reason'];
                         }
                     }
                     $results[] = $this->_get_result('', '', W3TC_CDN_RESULT_ERROR, 'Failed to delete dup file ' . $title_to_search . ' ' . $details);
                     $result = 'with_errors';
                 }
             }
         }
     }
     // check update date and upload
     foreach ($files as $file_descriptor) {
         if (!is_null($timeout_time) && time() > $timeout_time) {
             return 'timeout';
         }
         list($parent_id, $title) = $this->remote_path_to_title($file_descriptor['remote_path']);
         $properties = array();
         if (isset($file_descriptor['content'])) {
             // when content specified - just upload
             $content = $file_descriptor['content'];
         } else {
             $local_path = $file_descriptor['local_path'];
             if (!file_exists($local_path)) {
                 $results[] = $this->_get_result($local_path, $file_descriptor['remote_path'], W3TC_CDN_RESULT_ERROR, 'Source file not found.', $file_descriptor);
                 continue;
             }
             $mtime = @filemtime($local_path);
             $p = new \Google_Service_Drive_Property();
             $p->key = 'mtime';
             $p->value = $mtime;
             $properties[] = $p;
             if (!$force_rewrite && isset($files_by_title[$title])) {
                 $existing_file = $files_by_title[$title];
                 $existing_size = $existing_file->fileSize;
                 $existing_mtime = 0;
                 if (is_array($existing_file->properties)) {
                     foreach ($existing_file->properties as $p) {
                         if ($p->key == 'mtime') {
                             $existing_mtime = $p->value;
                         }
                     }
                 }
                 $size = @filesize($local_path);
                 if ($mtime == $existing_mtime && $size == $existing_size) {
                     $results[] = $this->_get_result($file_descriptor['local_path'], $file_descriptor['remote_path'], W3TC_CDN_RESULT_OK, 'File up-to-date.', $file_descriptor);
                     continue;
                 }
             }
             $content = file_get_contents($local_path);
         }
         $file = new \Google_Service_Drive_DriveFile();
         $file->setTitle($title);
         $file->setProperties($properties);
         $parent = new \Google_Service_Drive_ParentReference();
         $parent->setId($parent_id);
         $file->setParents(array($parent));
         try {
             try {
                 // update file if there's one already or insert
                 if (isset($files_by_title[$title])) {
                     $existing_file = $files_by_title[$title];
                     $created_file = $this->_service->files->update($existing_file->id, $file, array('data' => $content, 'uploadType' => 'media'));
                 } else {
                     $created_file = $this->_service->files->insert($file, array('data' => $content, 'uploadType' => 'media'));
                     $permission = new \Google_Service_Drive_Permission();
                     $permission->setValue('');
                     $permission->setType('anyone');
                     $permission->setRole('reader');
                     $this->_service->permissions->insert($created_file->id, $permission);
                 }
             } catch (\Google_Auth_Exception $e) {
                 if ($allow_refresh_token) {
                     return 'refresh_required';
                 }
                 throw $e;
             }
             $results[] = $this->_get_result($file_descriptor['local_path'], $file_descriptor['remote_path'], W3TC_CDN_RESULT_OK, 'OK', $file_descriptor);
         } catch (\Google_Service_Exception $e) {
             $errors = $e->getErrors();
             $details = '';
             if (count($errors) >= 1) {
                 $details = $errors[0]['reason'];
             }
             delete_transient('w3tc_cdn_google_drive_folder_ids');
             $results[] = $this->_get_result($file_descriptor['local_path'], $file_descriptor['remote_path'], W3TC_CDN_RESULT_ERROR, 'Failed to upload file ' . $file_descriptor['remote_path'] . ' ' . $details, $file_descriptor);
             $result = 'with_errors';
             continue;
         } catch (\Exception $e) {
             delete_transient('w3tc_cdn_google_drive_folder_ids');
             $results[] = $this->_get_result($file_descriptor['local_path'], $file_descriptor['remote_path'], W3TC_CDN_RESULT_ERROR, 'Failed to upload file ' . $file_descriptor['remote_path'], $file_descriptor);
             $result = 'with_errors';
             continue;
         }
     }
     return $result;
 }