Search in sources :

Example 81 with File

use of com.google.api.services.drive.model.File in project components by Talend.

the class GoogleDriveUtils method createFolder.

/**
 * Create a folder in the specified parent folder
 *
 * @param parentFolderId folder ID where to create folderName
 * @param folderName new folder's name
 * @return folder ID value
 * @throws IOException when operation fails
 */
public String createFolder(String parentFolderId, String folderName) throws IOException {
    File createdFolder = new File();
    createdFolder.setName(folderName);
    createdFolder.setMimeType(MIME_TYPE_FOLDER);
    createdFolder.setParents(Collections.singletonList(parentFolderId));
    return drive.files().create(createdFolder).setFields("id").execute().getId();
}
Also used : File(com.google.api.services.drive.model.File)

Example 82 with File

use of com.google.api.services.drive.model.File in project components by Talend.

the class GoogleDriveUtils method copyFile.

/**
 * @param fileId ID of the fileName to copy
 * @param destinationFolderId folder ID where to copy the fileId
 * @param newName if not empty rename copy to this name
 * @param deleteOriginal remove original fileName (aka mv)
 * @return copied fileName ID
 * @throws IOException when copy fails
 */
public String copyFile(String fileId, String destinationFolderId, String newName, boolean deleteOriginal) throws IOException {
    LOG.debug("[copyFile] fileId: {}; destinationFolderId: {}, newName: {}; deleteOriginal: {}.", fileId, destinationFolderId, newName, deleteOriginal);
    File copy = new File();
    copy.setParents(Collections.singletonList(destinationFolderId));
    if (!newName.isEmpty()) {
        copy.setName(newName);
    }
    File resultFile = drive.files().copy(fileId, copy).setFields("id, parents").execute();
    String copiedResourceId = resultFile.getId();
    if (deleteOriginal) {
        drive.files().delete(fileId).execute();
    }
    return copiedResourceId;
}
Also used : File(com.google.api.services.drive.model.File)

Example 83 with File

use of com.google.api.services.drive.model.File in project components by Talend.

the class GoogleDriveUtils method copyFolder.

/**
 * @param sourceFolderId source folder ID
 * @param destinationFolderId folder ID where to copy the sourceFolderId's content
 * @param newName folder name to assign
 * @return created folder ID
 * @throws IOException when operation fails
 */
public String copyFolder(String sourceFolderId, String destinationFolderId, String newName) throws IOException {
    LOG.debug("[copyFolder] sourceFolderId: {}; destinationFolderId: {}; newName: {}", sourceFolderId, destinationFolderId, newName);
    // create a new folder
    String newFolderId = createFolder(destinationFolderId, newName);
    // Make a recursive copy of all files/folders inside the source folder
    String query = format(Q_IN_PARENTS, sourceFolderId) + Q_AND + Q_NOT_TRASHED;
    FileList originals = drive.files().list().setQ(query).execute();
    LOG.debug("[copyFolder] Searching for copy {}", query);
    for (File file : originals.getFiles()) {
        if (file.getMimeType().equals(MIME_TYPE_FOLDER)) {
            copyFolder(file.getId(), newFolderId, file.getName());
        } else {
            copyFile(file.getId(), newFolderId, file.getName(), false);
        }
    }
    return newFolderId;
}
Also used : FileList(com.google.api.services.drive.model.FileList) File(com.google.api.services.drive.model.File)

Example 84 with File

use of com.google.api.services.drive.model.File in project components by Talend.

the class GoogleDriveAbstractListReader method processFolder.

private boolean processFolder() throws IOException {
    if (folderId == null && !subFolders.isEmpty()) {
        folderId = subFolders.get(0);
        subFolders.remove(0);
        request.setQ(format(query, folderId));
        LOG.debug("query = {} {}.", query, folderId);
    }
    searchResults.clear();
    FileList files = request.execute();
    for (File file : files.getFiles()) {
        if (canAddSubFolder(file.getMimeType())) {
            subFolders.add(file.getId());
        }
        if (canAddFile(file.getMimeType())) {
            searchResults.add(file);
            result.totalCount++;
        }
    }
    request.setPageToken(files.getNextPageToken());
    searchCount = searchResults.size();
    // finished for folderId
    if (StringUtils.isEmpty(request.getPageToken()) || searchCount == 0) {
        folderId = null;
    }
    return searchCount > 0;
}
Also used : FileList(com.google.api.services.drive.model.FileList) File(com.google.api.services.drive.model.File)

Example 85 with File

use of com.google.api.services.drive.model.File in project components by Talend.

the class GoogleDriveCreateRuntimeTest method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    properties = new GoogleDriveCreateProperties("test");
    properties.setupProperties();
    properties = (GoogleDriveCreateProperties) setupConnectionWithInstalledApplicationWithIdAndSecret(properties);
    // 
    properties.parentFolder.setValue(FOLDER_ROOT);
    properties.newFolder.setValue(FOLDER_CREATE);
    testRuntime = spy(GoogleDriveCreateRuntime.class);
    doReturn(drive).when(testRuntime).getDriveService();
    File fc = new File();
    fc.setId(FOLDER_CREATE_ID);
    when(drive.files().create(any(File.class)).setFields(eq("id")).execute()).thenReturn(fc);
    // 
    FileList flA = new FileList();
    List<File> fs = new ArrayList<>();
    File fA = new File();
    fA.setId("A");
    fs.add(fA);
    flA.setFiles(fs);
    when(drive.files().list().setQ(eq(qA)).execute()).thenReturn(flA);
    FileList flB = new FileList();
    List<File> fsA = new ArrayList<>();
    File fB = new File();
    fB.setId("B");
    fsA.add(fB);
    flB.setFiles(fsA);
    when(drive.files().list().setQ(eq(qB)).execute()).thenReturn(flB);
    FileList flC = new FileList();
    List<File> fsC = new ArrayList<>();
    File fC = new File();
    fC.setId("C");
    fsC.add(fC);
    flC.setFiles(fsC);
    when(drive.files().list().setQ(eq(qC)).execute()).thenReturn(flC);
}
Also used : GoogleDriveCreateProperties(org.talend.components.google.drive.create.GoogleDriveCreateProperties) FileList(com.google.api.services.drive.model.FileList) ArrayList(java.util.ArrayList) File(com.google.api.services.drive.model.File) Before(org.junit.Before)

Aggregations

File (com.google.api.services.drive.model.File)94 FileList (com.google.api.services.drive.model.FileList)35 Test (org.junit.Test)33 IOException (java.io.IOException)27 ArrayList (java.util.ArrayList)24 Before (org.junit.Before)12 FileContent (com.google.api.client.http.FileContent)10 HashMap (java.util.HashMap)10 UserRecoverableAuthIOException (com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException)9 FileNotFoundException (java.io.FileNotFoundException)7 ParseException (java.text.ParseException)7 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)6 DateTime (com.google.api.client.util.DateTime)6 Drive (com.google.api.services.drive.Drive)6 InputStream (java.io.InputStream)6 GeneralSecurityException (java.security.GeneralSecurityException)6 Matchers.anyString (org.mockito.Matchers.anyString)6 ParentReference (com.google.api.services.drive.model.ParentReference)5 Permission (com.google.api.services.drive.model.Permission)5 User (com.google.api.services.drive.model.User)5