private File patchFile(String fileId, File patch, int retry) { try { Files.Update patchRequest = drive.files().update(fileId, patch) .setFields(REQUEST_FILE_FIELDS); // control we are not exceeding number of requests/second bandwidthController.newRequest(); return patchRequest.execute(); } catch (Exception e) { if (retry > 0) { try { Thread.sleep(1000); } catch (InterruptedException e1) { throw new RuntimeException(e1); } logger.warn("Error updating file. Retrying..."); patchFile(fileId, patch, --retry); } throw new RuntimeException("Error updating file " + fileId, e); } }
throw new IllegalArgumentException("Error. file parents can't be null nor empty"); uploadRequest = drive.files().update(gFile.getId(), file, mediaContent); StringBuilder parents = new StringBuilder(); gFile.getParents().forEach((p) -> parents.append(p).append(","));
public void updateFile(SyncItem syncItem) { Drive drive = driveFactory.getDrive(this.credential); try { java.io.File localFile = syncItem.getLocalFile().get(); File remoteFile = syncItem.getRemoteFile().get(); BasicFileAttributes attr = Files.readAttributes(localFile.toPath(), BasicFileAttributes.class); remoteFile.setModifiedTime(new DateTime(attr.lastModifiedTime().toMillis())); if (isGoogleAppsDocument(remoteFile)) { return; } LOGGER.log(Level.INFO, "Updating file " + remoteFile.getId() + " (" + syncItem.getPath() + ")."); if (!options.isDryRun()) { Drive.Files.Update updateRequest = drive.files().update(remoteFile.getId(), remoteFile, new FileContent(determineMimeType(localFile), localFile)); //updateRequest.setModifiedDate(true); File updatedFile = executeWithRetry(options, () -> updateRequest.execute()); syncItem.setRemoteFile(Optional.of(updatedFile)); } } catch (IOException e) { throw new JDriveSyncException(JDriveSyncException.Reason.IOException, "Failed to update file: " + e.getMessage(), e); } }
properties.setName(renamed.getName()); properties.setMimeType(status.getMime()); session.getClient().files().update(id, properties). setSupportsTeamDrives(PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")).execute(); session.getClient().files().update(id, null) .setAddParents(fileid.getFileid(renamed.getParent(), new DisabledListProgressListener())) .setRemoveParents(previousParents.toString())
GApiGateway.drive.files().update(remoteFile.getId(), null, mediaContent).execute();
final File properties = new File(); properties.setTrashed(true); session.getClient().files().update(fileid.getFileid(file, new DisabledListProgressListener()), properties) .setSupportsTeamDrives(PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")) .queue(batch, new DeleteBatchCallback<File>(file, failures, callback));
private void delete(File file) { Drive drive = driveFactory.getDrive(this.credential); try { String id = file.getId(); if (isGoogleAppsDocument(file)) { LOGGER.log(Level.FINE, String.format("Not deleting file '%s' because it is a Google Apps document.", id)); return; } if (options.isNoDelete()) { LOGGER.log(Level.FINE, String.format("Not deleting file '%s' because option --no-delete is set.", id)); return; } if (options.isDeleteFiles()) { LOGGER.log(Level.FINE, "Deleting file " + id + " (" + file.getName() +")."); if (!options.isDryRun()) { executeWithRetry(options, () -> drive.files().delete(id).execute()); } } else { LOGGER.log(Level.FINE, "Trashing file " + id + " (" + file.getName() +")."); if (!options.isDryRun()) { executeWithRetry(options, () -> drive.files().update("{'trashed':true}", file).execute()); //trash(id).execute()); } } } catch (IOException e) { throw new JDriveSyncException(JDriveSyncException.Reason.IOException, "Failed to delete file: " + e.getMessage(), e); } }
@Override public void delete(final List<Path> files, final PasswordCallback prompt, final Callback callback) throws BackgroundException { for(Path file : files) { if(file.getType().contains(Path.Type.placeholder)) { continue; } callback.delete(file); try { if(DriveHomeFinderService.TEAM_DRIVES_NAME.equals(file.getParent())) { session.getClient().teamdrives().delete(fileid.getFileid(file, new DisabledListProgressListener())).execute(); } else { if(PreferencesFactory.get().getBoolean("googledrive.delete.trash")) { final File properties = new File(); properties.setTrashed(true); session.getClient().files().update(fileid.getFileid(file, new DisabledListProgressListener()), properties) .setSupportsTeamDrives(PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")).execute(); } else { session.getClient().files().delete(fileid.getFileid(file, new DisabledListProgressListener())) .setSupportsTeamDrives(PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")).execute(); } } } catch(IOException e) { throw new DriveExceptionMappingService().map("Cannot delete {0}", e, file); } } }
public void updateMetadata(SyncItem syncItem) { Drive drive = driveFactory.getDrive(this.credential); try { java.io.File localFile = syncItem.getLocalFile().get(); File remoteFile = syncItem.getRemoteFile().get(); BasicFileAttributes attr = Files.readAttributes(localFile.toPath(), BasicFileAttributes.class); if (isGoogleAppsDocument(remoteFile)) { return; } LOGGER.log(Level.FINE, "Updating metadata of remote file " + remoteFile.getId() + " (" + syncItem.getPath() + ")."); if (!options.isDryRun()) { File newRemoteFile = new File(); newRemoteFile.setModifiedTime(new DateTime(attr.lastModifiedTime().toMillis())); Drive.Files.Update updateRequest = drive.files().update(remoteFile.getId(), newRemoteFile).setFields("modifiedTime"); File updatedFile = executeWithRetry(options, () -> updateRequest.execute()); syncItem.setRemoteFile(Optional.of(updatedFile)); } } catch (IOException e) { throw new JDriveSyncException(JDriveSyncException.Reason.IOException, "Failed to update file: " + e.getMessage(), e); } }
@Override public void setMetadata(final Path file, final Map<String, String> metadata) throws BackgroundException { try { final String fileid = this.fileid.getFileid(file, new DisabledListProgressListener()); final File body = new File(); body.setProperties(metadata); session.getClient().files().update(fileid, body).setFields("properties"). setSupportsTeamDrives(PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")).execute(); } catch(IOException e) { throw new DriveExceptionMappingService().map("Failure to write attributes of {0}", e, file); } } }
private String removeResource(String resourceId, boolean useTrash) throws IOException { if (useTrash) { drive.files().update(resourceId, new File().setTrashed(true)).execute(); } else { drive.files().delete(resourceId).execute(); } return resourceId; }
private File actuallyModifySpreadsheet(final File original, final java.io.File export) throws IOException { final FileContent fc = new FileContent(MIME_TYPE_XLS_SPREADSHEET, export); return driveService.files().update(original.getId(), null, fc) .setFields(getFields()) .execute(); }
private File actuallyModifySpreadsheet(final File original, final java.io.File export) throws IOException { final FileContent fc = new FileContent(MIME_TYPE_XLS_SPREADSHEET, export); return driveService.files().update(original.getId(), null, fc) .setFields(getFields()) .execute(); }
private String removeResource(String resourceId, boolean useTrash) throws IOException { if (useTrash) { drive.files().update(resourceId, new File().setTrashed(true)).execute(); } else { drive.files().delete(resourceId).execute(); } return resourceId; }
@Override public void setTimestamp(final Path file, final Long modified) throws BackgroundException { try { final String fileid = this.fileid.getFileid(file, new DisabledListProgressListener()); final File properties = new File(); properties.setModifiedTime(new DateTime(modified)); session.getClient().files().update(fileid, properties).setFields("modifiedTime"). setSupportsTeamDrives(PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")).execute(); } catch(IOException e) { throw new DriveExceptionMappingService().map("Failure to write attributes of {0}", e, file); } } }