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); } }
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); } }
StringBuilder parents = new StringBuilder(); gFile.getParents().forEach((p) -> parents.append(p).append(",")); ((Files.Update) uploadRequest).setAddParents(parents.toString());
GApiGateway.drive.files().update(remoteFile.getId(), null, mediaContent).execute();
properties.setMimeType(status.getMime()); session.getClient().files().update(id, properties). setSupportsTeamDrives(PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")).execute(); .setAddParents(fileid.getFileid(renamed.getParent(), new DisabledListProgressListener())) .setRemoveParents(previousParents.toString()) .setFields("id, parents") .setSupportsTeamDrives(PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")) .execute(); return new Path(renamed.getParent(), renamed.getName(), renamed.getType(), new DriveAttributesFinderFeature(session, fileid).find(renamed));
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));
@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); } } }
/** * Updates a file's metadata and/or content with patch semantics. * * Create a request for the method "files.update". * * This request holds the parameters needed by the the drive server. After setting any optional * parameters, call the {@link Update#execute()} method to invoke the remote operation. * * <p> * This method should be used for uploading media content. * </p> * * @param fileId The ID of the file. * @param content the {@link com.google.api.services.drive.model.File} media metadata or {@code null} if none * @param mediaContent The media HTTP content or {@code null} if none. * @return the request * @throws java.io.IOException if the initialization of the request fails */ public Update update(java.lang.String fileId, com.google.api.services.drive.model.File content, com.google.api.client.http.AbstractInputStreamContent mediaContent) throws java.io.IOException { Update result = new Update(fileId, content, mediaContent); initialize(result); return result; }
/** * Updates a file's metadata and/or content with patch semantics. * * Create a request for the method "files.update". * * This request holds the parameters needed by the the drive server. After setting any optional * parameters, call the {@link Update#execute()} method to invoke the remote operation. <p> {@link * Update#initialize(com.google.api.client.googleapis.services.AbstractGoogleClientRequest)} must * be called to initialize this instance immediately after invoking the constructor. </p> * * <p> * This constructor should be used for uploading media content. * </p> * * @param fileId The ID of the file. * @param content the {@link com.google.api.services.drive.model.File} media metadata or {@code null} if none * @param mediaContent The media HTTP content or {@code null} if none. * @since 1.13 */ protected Update(java.lang.String fileId, com.google.api.services.drive.model.File content, com.google.api.client.http.AbstractInputStreamContent mediaContent) { super(Drive.this, "PATCH", "/upload/" + getServicePath() + REST_PATH, content, com.google.api.services.drive.model.File.class); this.fileId = com.google.api.client.util.Preconditions.checkNotNull(fileId, "Required parameter fileId must be specified."); initializeMediaUpload(mediaContent); }
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); } }
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); } }
/** * Updates a file's metadata and/or content with patch semantics. * * Create a request for the method "files.update". * * This request holds the parameters needed by the drive server. After setting any optional * parameters, call the {@link Update#execute()} method to invoke the remote operation. * * @param fileId The ID of the file. * @param content the {@link com.google.api.services.drive.model.File} * @return the request */ public Update update(java.lang.String fileId, com.google.api.services.drive.model.File content) throws java.io.IOException { Update result = new Update(fileId, content); initialize(result); return result; }
@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 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); } } }