2017-08-07 194 views
0

我有我的車在這個層次的排斥:文件列表文件夾和所有孫子的子目錄

-- logo1.png 
| 
|-- logo2.png 
| 
-- myFolder 
|  | 
|  --logo3.png 
| 
| 
-- myPrivateFolder 
     | 
     --anotherPrivateFolder 
      | 
      -- logo4.png 

我使用這個簡單的腳本來列出我的文件:

$client = new Google_Client(); 
$client->setAuthConfig($oauth_credentials); 
$client->setRedirectUri($redirect_uri); 
$client->addScope("https://www.googleapis.com/auth/drive"); 
$service = new Google_Service_Drive($client); 


$result = array(); 

try { 
    $parameters = array(
     'q' => "mimeType contains 'image/'", 
); 
    $files = $service->files->listFiles($parameters); 

    $result = array_merge($result, $files->getFiles()); 
} catch (Exception $e) { 
    print "Une erreur s'est produite : " . $e->getMessage(); 
} 
return $result; 

作爲參數,我在提取排除後的文件夾ID後:

$parameters = array('q' => "'not 'myFolderID' in parents and not 'myprivateFolderID' in parents"); 

當運行我的腳本時,我只希望t o得到logo1.pnglogo2.png,因爲我排除了myFoldermyPrivateFolder,但我也得到logo4.png,因爲它有anotherPrivateFoldermyPrivateFolder是祖父母。我瞭解Google Drive API沒有樹結構的概念,因此我製作了一個遞歸函數來傳播排除文件夾的所有子目錄。在這種情況下,我的腳本將獲得myFoldermyPrivateFolder以下文件夾的所有ID。這是我的功能:

$excluded_folders_id = array(); 
function retrieveAllChildren($service, $parameters) { 
    global $excluded_folders_id; 
    $results = array(); 
    $pageToken = NULL; 
    do{ 
    try { 
     if ($pageToken) { 
     $parameters['pageToken'] = $pageToken; 
     } 
     $files = $service->files->listFiles($parameters); 
     $results = array_merge($results, $files->getFiles()); 
     $pageToken = $files->getNextPageToken(); 
    } catch (Exception $e) { 
     print "Une erreur s'est produite : " . $e->getMessage(); 
     $pageToken = NULL; 
    } 
    }while($pageToken); 
    foreach ($results as $result) { 
    echo "=> New children to exclude : ".$result->name."\n"; 
    array_push($excluded_folders_id, $result->id); 
    retrieveAllChildren($service, array('q' => "'".$result->id."' in parents and mimeType = 'application/vnd.google-apps.folder'")); 
    } 
} 
retrieveAllChildren($service, array('q' => "name = 'myPrivateFolder'")) 

因此,我有一個數組包含我排除的文件夾及其所有子目錄的ID。然後我建立查詢,爲每個排除的文件夾ID,我添加...和'newsubdirectoryID'不在父母身上。 在包含數百個子文件夾的驅動器上運行請求時,我有413錯誤,說我的客戶端請求太大。那麼我如何排除一個文件夾及其所有內容,沒有限制?

預先感謝您的任何幫助

編輯:

function isFileInExcluded($service, $entity_id){ 

    global $excluded_folders_id; 

    $entityProperties = $service->files->get($entity_id,array('fields' => 'parents, id, name')); 

    If there is no more parents (so if on top of tree - no excluded encountered) 
    if($entityProperties->parents == NULL){ 
    return true; 
    } 
    // If file has excluded folder on upper part of tree 
    if(in_array($entityProperties->parents[0], $excluded_folders_id) == true){ 
    return true; 
    }else{ 
    // Spread acrosse upper parents 
    $parentProperties = $service->files->get($entityProperties->parents[0],array('fields' => 'parents, id, name')); 
    isFileInExcluded($service, $parentProperties->id); 
    } 
} 

回答

1

「當運行包含數以百計的子文件夾的驅動器上的要求,我有一個413錯誤,說我的客戶端請求太大「

您將需要重新設計您的應用程序。

一些方法你可以採取: -

  1. 而是排除,你能不能找到一個包容性的參數是常見的要恢復的文件。
  2. 只需獲取所有圖像,然後在你的php中過濾掉所需的圖像。
  3. 給出想要排除共同父母的圖像。所以例如。你的logo4.png是另一個私人文件夾的孩子。創建一個名爲say「excludeAllChildren」的新文件夾,並使除另一個私人文件夾的子項外的logo4.png成爲excludeAllChildren的子項。
+0

您好,非常感謝您的支持,我選擇了第二種解決方案。我提取所有圖像,並檢查排除的文件夾ID,如果它匹配:) – saperlipopette

相關問題