Search in sources :

Example 1 with ParentReference

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

the class GoogleDriveAPI method createSpreadsheetFromFile.

/**
	 * Method to create a google spreadsheet from a file
	 * 
	 * @param idParentFolder
	 *            String to identify the parent folder
	 * @param emailUserOwner
	 *            String to identify email user owner
	 * @param extensionFile
	 *            String with extension file
	 * @param nameFile
	 *            String with name google spreadsheet
	 * @param fileOrigin
	 *            File with content file origin
	 * @param mimeType
	 *            String with mime type file origin
	 * 
	 * @return True if create is ok, False otherwise
	 */
public boolean createSpreadsheetFromFile(String idParentFolder, String emailUserOwner, String extensionFile, String nameFile, java.io.File fileOrigin, String mimeType) {
    boolean result = true;
    Permission newPermission = createPermission();
    File body = new File();
    body.setTitle(nameFile);
    body.setMimeType("application/vnd.google-apps.spreadsheet");
    body.setEditable(true);
    body.setShared(true);
    body.setPermissions(Arrays.asList(newPermission));
    body.setUserPermission(newPermission);
    body.setWritersCanShare(true);
    body.setFileExtension(extensionFile);
    User user = new User();
    user.setEmailAddress(emailUserOwner);
    user.setIsAuthenticatedUser(true);
    List<User> list = new ArrayList<User>();
    list.add(user);
    body.setOwners(list);
    body.setParents(Arrays.asList(new ParentReference().setId(idParentFolder)));
    FileContent mediaContent = new FileContent(mimeType, fileOrigin);
    String fileId = "";
    try {
        File file = drive.files().insert(body, mediaContent).execute();
        fileId = file.getId();
        insertPermission(newPermission, fileId);
    } catch (Exception e) {
        log.error("Error create spreadsheet from file ", e);
        result = false;
    }
    log.info("create Spreadsheet in google Drive from " + nameFile);
    return result;
}
Also used : FileContent(com.google.api.client.http.FileContent) ParentReference(com.google.api.services.drive.model.ParentReference) User(com.google.api.services.drive.model.User) Permission(com.google.api.services.drive.model.Permission) ArrayList(java.util.ArrayList) File(com.google.api.services.drive.model.File) GeneralSecurityException(java.security.GeneralSecurityException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 2 with ParentReference

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

the class GoogleDriveAPI method createSpreadsheetFromFile.

public boolean createSpreadsheetFromFile(String idParentFolder, String emailUserOwner, String extensionFile, String nameFile, java.io.File fileOrigin, String mimeType) {
    boolean result = true;
    Permission newPermission = createPermission();
    File body = new File();
    body.setTitle(nameFile);
    body.setMimeType("application/vnd.google-apps.spreadsheet");
    body.setEditable(true);
    body.setShared(true);
    body.setPermissions(Arrays.asList(newPermission));
    body.setUserPermission(newPermission);
    body.setWritersCanShare(true);
    body.setFileExtension(extensionFile);
    User user = new User();
    user.setEmailAddress(emailUserOwner);
    user.setIsAuthenticatedUser(true);
    List<User> list = new ArrayList<User>();
    list.add(user);
    body.setOwners(list);
    body.setParents(Arrays.asList(new ParentReference().setId(idParentFolder)));
    FileContent mediaContent = new FileContent(mimeType, fileOrigin);
    String fileId = "";
    try {
        File file = service.files().insert(body, mediaContent).execute();
        fileId = file.getId();
        insertPermission(newPermission, fileId);
    } catch (IOException e) {
        log.error("Error create spreadsheet from file ", e);
        result = false;
    }
    log.info("create Spreadsheet in google Drive from " + nameFile);
    return result;
}
Also used : FileContent(com.google.api.client.http.FileContent) ParentReference(com.google.api.services.drive.model.ParentReference) User(com.google.api.services.drive.model.User) Permission(com.google.api.services.drive.model.Permission) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Example 3 with ParentReference

use of com.google.api.services.drive.model.ParentReference in project keepass2android by PhilippC.

the class GoogleDriveFileStorage method createFolder.

@Override
public String createFolder(String parentPath, String newDirName) throws Exception {
    File body = new File();
    body.setTitle(newDirName);
    body.setMimeType(FOLDER_MIME_TYPE);
    GDrivePath parentGdrivePath = new GDrivePath(parentPath);
    body.setParents(Arrays.asList(new ParentReference().setId(parentGdrivePath.getGDriveId())));
    try {
        File file = getDriveService(parentGdrivePath.getAccount()).files().insert(body).execute();
        logDebug("created folder " + newDirName + " in " + parentPath + ". id: " + file.getId());
        return new GDrivePath(parentPath, file).getFullPath();
    } catch (Exception e) {
        throw convertException(e);
    }
}
Also used : ParentReference(com.google.api.services.drive.model.ParentReference) File(com.google.api.services.drive.model.File) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) UserRecoverableAuthIOException(com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with ParentReference

use of com.google.api.services.drive.model.ParentReference in project keepass2android by PhilippC.

the class GoogleDriveFileStorage method createFilePath.

@Override
public String createFilePath(String parentPath, String newFileName) throws Exception {
    File body = new File();
    body.setTitle(newFileName);
    GDrivePath parentGdrivePath = new GDrivePath(parentPath);
    body.setParents(Arrays.asList(new ParentReference().setId(parentGdrivePath.getGDriveId())));
    try {
        File file = getDriveService(parentGdrivePath.getAccount()).files().insert(body).execute();
        return new GDrivePath(parentPath, file).getFullPath();
    } catch (Exception e) {
        throw convertException(e);
    }
}
Also used : ParentReference(com.google.api.services.drive.model.ParentReference) File(com.google.api.services.drive.model.File) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) UserRecoverableAuthIOException(com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with ParentReference

use of com.google.api.services.drive.model.ParentReference in project Aegis by Decad3nce.

the class BackupGoogleAccountsActivity method saveFileToDrive.

private void saveFileToDrive() {
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                if (!isAegisFolderAvailable()) {
                    createAegisFolder();
                }
                FileContent mediaContent = null;
                File body = new File();
                File file = null;
                if (callLogs) {
                    Log.i(TAG, "Generating new file to upload: " + callLogFileUri);
                    java.io.File callFileContent = new java.io.File(callLogFileUri.getPath());
                    mediaContent = new FileContent("text/plain", callFileContent);
                    body.setTitle(callFileContent.getName());
                    body.setParents(Arrays.asList(new ParentReference().setId(getAegisFolder())));
                    body.setMimeType("text/plain");
                    file = service.files().insert(body, mediaContent).execute();
                    if (file != null) {
                        Log.i(TAG, "File uploaded successfully: " + file.getTitle());
                        Utils.sendSMS(context, address, context.getResources().getString(R.string.util_sendsms_data_recovery_pass) + " " + file.getTitle() + " to Google Drive");
                        deleteFile(file.getTitle());
                        finish();
                    }
                }
                if (smsLogs) {
                    Log.i(TAG, "Generating new file to upload: " + smsLogFileUri);
                    java.io.File smsFileContent = new java.io.File(smsLogFileUri.getPath());
                    mediaContent = new FileContent("text/plain", smsFileContent);
                    body.setTitle(smsFileContent.getName());
                    body.setParents(Arrays.asList(new ParentReference().setId(getAegisFolder())));
                    body.setMimeType("text/plain");
                    file = service.files().insert(body, mediaContent).execute();
                    if (file != null) {
                        Log.i(TAG, "File uploaded successfully: " + file.getTitle());
                        Utils.sendSMS(context, address, context.getResources().getString(R.string.util_sendsms_data_recovery_pass) + " " + file.getTitle() + " to Google Drive");
                        deleteFile(file.getTitle());
                        finish();
                    }
                }
            } catch (UserRecoverableAuthIOException e) {
                Log.i(TAG, "Exception: " + e.toString());
                startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
            } catch (IOException e) {
                Log.i(TAG, "Exception: " + e.toString());
                e.printStackTrace();
                Utils.sendSMS(context, address, context.getResources().getString(R.string.util_sendsms_data_recovery_fail));
            }
        }
    });
    t.start();
}
Also used : FileContent(com.google.api.client.http.FileContent) ParentReference(com.google.api.services.drive.model.ParentReference) UserRecoverableAuthIOException(com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException) UserRecoverableAuthIOException(com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Aggregations

File (com.google.api.services.drive.model.File)5 ParentReference (com.google.api.services.drive.model.ParentReference)5 IOException (java.io.IOException)5 UserRecoverableAuthIOException (com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException)3 FileContent (com.google.api.client.http.FileContent)3 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)2 Permission (com.google.api.services.drive.model.Permission)2 User (com.google.api.services.drive.model.User)2 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 GeneralSecurityException (java.security.GeneralSecurityException)1 ParseException (java.text.ParseException)1