Search in sources :

Example 56 with File

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

the class GoogleDriveAPI method downloadChildFile.

/**
	 * Method to download file from folder
	 * @param path String with destination file
	 * @param child ChildReference to download
	 * @throws IOException
	 */
private void downloadChildFile(String path, ChildReference child) throws IOException {
    File file = drive.files().get(child.getId()).execute();
    download(path, file);
}
Also used : File(com.google.api.services.drive.model.File)

Example 57 with File

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

the class GoogleDriveAPI method updateFile.

/**
	 * Method by update file
	 * 
	 * @param name
	 *            String name file
	 * @param fileContent
	 *            content file
	 * @param newMimeType
	 *            String with mime type
	 * @return boolean true if updated is success
	 */
public boolean updateFile(String name, java.io.File fileContent, String newMimeType) {
    File file = searchFile(name);
    FileContent mediaContent = new FileContent(newMimeType, fileContent);
    File updatedFile = null;
    try {
        updatedFile = drive.files().update(file.getId(), file, mediaContent).execute();
    } catch (Exception e) {
        log.error("Error actualizando fichero en google drive", e);
    }
    return (updatedFile != null);
}
Also used : FileContent(com.google.api.client.http.FileContent) File(com.google.api.services.drive.model.File) GeneralSecurityException(java.security.GeneralSecurityException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 58 with File

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

the class GoogleDriveAPI method downloadFilesAfterDate.

/**
	 * Download files after a date
	 * 
	 * @param path
	 *            Path where download files
	 * @param stringDateLastChange
	 *            String with a date
	 */
public void downloadFilesAfterDate(String path, String stringDateLastChange) {
    List<File> files = listOwnerFilesAfterDate(stringDateLastChange);
    for (File file : files) {
        try {
            String downloadUrl = file.getExportLinks().get("text/csv");
            HttpResponse resp = drive.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() + ".csv");
            FileUtils.copyInputStreamToFile(input, f);
        } catch (Exception e) {
            log.error("Error decargando ficheros despues de una fecha en google drive", e);
        }
    }
}
Also used : InputStream(java.io.InputStream) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) File(com.google.api.services.drive.model.File) GeneralSecurityException(java.security.GeneralSecurityException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 59 with File

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

the class GoogleDriveAPI method searchFile.

/**
	 * Method to list all files and folders of id account
	 */
public File searchFile(String name) {
    log.debug("init listOwnerFiles()");
    //		FileList result;
    List<ChildReference> list = listFolderFiles(Prop.idParentFolder);
    File file = null;
    for (ChildReference child : list) {
        try {
            file = drive.files().get(child.getId()).execute();
            if (file != null) {
                if (file.getShared() == true && file.getTitle().contains(name)) {
                    break;
                }
            }
        } catch (IOException e) {
            log.error("Error read file " + name + " in google Drive API", e);
        }
    }
    //		com.google.api.services.drive.Drive.Files.List request = null;
    //		try {
    //			request = drive.files().list().setMaxResults(1000);
    //			result = request.execute();
    //			files.addAll(result.getItems());
    //
    //		} catch (Exception e) {
    //			log.error("Error list files", e);
    //		}
    //		if (files == null || files.size() == 0) {
    //			log.error("No files found");
    //		} else {
    //			log.debug("Files:\n");
    //			for (File fileAux : files) {
    //				if (fileAux.getShared() == true && fileAux.getTitle().contains(name)) {
    //					file = fileAux;
    //					break;
    //				}
    //			}
    //		}
    log.debug("end listOwnerFiles()");
    return file;
}
Also used : ChildReference(com.google.api.services.drive.model.ChildReference) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Example 60 with File

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

the class GoogleDriveAPI method download.

private void download(String path, File file) throws IOException {
    if (file.getShared()) {
        String downloadUrl = file.getExportLinks().get("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        HttpResponse resp = drive.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() + "." + Prop.formatConfig);
        FileUtils.copyInputStreamToFile(input, f);
    }
}
Also used : InputStream(java.io.InputStream) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) File(com.google.api.services.drive.model.File)

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