Search in sources :

Example 51 with File

use of com.google.api.services.drive.model.File in project camel by apache.

the class FilesConsumerIntegrationTest method testListConsumer.

@Test
public void testListConsumer() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    File testFile = uploadTestFile();
    String fileId = testFile.getId();
    assertMockEndpointsSatisfied();
    FileList fileList = mock.getReceivedExchanges().get(0).getIn().getBody(com.google.api.services.drive.model.FileList.class);
    assertTrue(fileInList(fileId, fileList));
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FileList(com.google.api.services.drive.model.FileList) File(com.google.api.services.drive.model.File) Test(org.junit.Test)

Example 52 with File

use of com.google.api.services.drive.model.File in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method downloadAllFiles.

public void downloadAllFiles(String path) {
    List<File> files = listOwnerFiles();
    for (File file : files) {
        try {
            String downloadUrl = file.getExportLinks().get("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl)).execute();
            InputStream input = resp.getContent();
            java.io.File f = new java.io.File(path + java.io.File.separator + file.getTitle() + ".xlsx");
            FileUtils.copyInputStreamToFile(input, f);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : InputStream(java.io.InputStream) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Example 53 with File

use of com.google.api.services.drive.model.File in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method listOwnerFiles.

public List<File> listOwnerFiles() {
    FileList result;
    List<File> files = new ArrayList<>();
    com.google.api.services.drive.Drive.Files.List request = null;
    try {
        do {
            request = service.files().list().setMaxResults(500);
            result = request.execute();
            files.addAll(result.getItems());
            request.setPageToken(result.getNextPageToken());
        } while (request.getPageToken() != null && request.getPageToken().length() > 0);
    } catch (IOException e) {
        log.error("Error list files", e);
    }
    return files;
}
Also used : FileList(com.google.api.services.drive.model.FileList) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Example 54 with File

use of com.google.api.services.drive.model.File in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method downloadFilesAfterDate.

public void downloadFilesAfterDate(String path, String stringDateLastChange) {
    List<File> files = listOwnerFilesAfterDate(stringDateLastChange);
    for (File file : files) {
        try {
            String downloadUrl = file.getExportLinks().get("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl)).execute();
            InputStream input = resp.getContent();
            java.io.File f = new java.io.File(path + java.io.File.separator + file.getTitle() + ".xlsx");
            FileUtils.copyInputStreamToFile(input, f);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : InputStream(java.io.InputStream) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Example 55 with File

use of com.google.api.services.drive.model.File in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method listOwnerFilesAfterDate.

public List<File> listOwnerFilesAfterDate(String stringDateLastChange) {
    SimpleDateFormat formatFullDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date dateLastChange = null;
    try {
        dateLastChange = formatFullDate.parse(stringDateLastChange);
    } catch (ParseException e1) {
        log.error("Error parse date in list", e1);
    }
    FileList result;
    List<File> files = null;
    try {
        result = service.files().list().setMaxResults(500).execute();
        files = result.getItems();
    } catch (IOException e) {
        log.error("Error list files", e);
    }
    if (files == null || files.size() == 0) {
        log.error("No files found");
    } else {
        log.info("Files:\n");
        for (File file : files) {
            DateTime dateTime = file.getModifiedDate();
            Date dateModifyFile = new Date(dateTime.getValue());
            if (dateModifyFile.after(dateLastChange))
                log.info("Title " + file.getTitle() + " id " + file.getId() + " DateTime " + formatFullDate.format(dateModifyFile));
        }
    }
    return files;
}
Also used : FileList(com.google.api.services.drive.model.FileList) ParseException(java.text.ParseException) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) File(com.google.api.services.drive.model.File) Date(java.util.Date) DateTime(com.google.api.client.util.DateTime)

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