private void importSingleFile( UUID jobId, Drive driveInterface, DigitalDocumentWrapper file, String parentId) throws IOException { InputStreamContent content = new InputStreamContent( null, jobStore.getStream(jobId, file.getCachedContentId())); DtpDigitalDocument dtpDigitalDocument = file.getDtpDigitalDocument(); File driveFile = new File().setName(dtpDigitalDocument.getName()); if (!Strings.isNullOrEmpty(parentId)) { driveFile.setParents(ImmutableList.of(parentId)); } if (!Strings.isNullOrEmpty(dtpDigitalDocument.getDateModified())) { driveFile.setModifiedTime(DateTime.parseRfc3339(dtpDigitalDocument.getDateModified())); } if (!Strings.isNullOrEmpty(file.getOriginalEncodingFormat()) && file.getOriginalEncodingFormat().startsWith("application/vnd.google-apps.")) { driveFile.setMimeType(file.getOriginalEncodingFormat()); } driveInterface.files().create( driveFile, content ).execute(); }
private String importSingleFolder( UUID jobId, Drive driveInterface, String folderName, String folderId, String parentId) throws IOException { File newFolder = new File() .setName(folderName) .setMimeType(DriveExporter.FOLDER_MIME_TYPE); if (!Strings.isNullOrEmpty(parentId)) { newFolder.setParents(ImmutableList.of(parentId)); } File resultFolder = driveInterface.files().create(newFolder).execute(); DriveFolderMapping mapping = new DriveFolderMapping(folderId, resultFolder.getId()); jobStore.update(jobId, folderId, mapping); return resultFolder.getId(); }
protected void doCreateFolder() throws Exception { if ( !getName().getBaseName().isEmpty() ) { File folder = new File(); folder.setName( getName().getBaseName() ); folder.setMimeType( MIME_TYPES.FOLDER.mimeType ); folder = driveService.files().create( folder ).execute(); if ( folder != null ) { id = folder.getId(); mimeType = MIME_TYPES.get( folder.getMimeType() ); } } }
file.setName("My Image File"); FileContent mediaContent = new FileContent(imageMimeType, new java.io.File(imageFilePath)); File uploadedFile = driveService.files().create(file, mediaContent).execute(); String fileId = uploadedFile.getId();
@Override public Path mkdir(final Path folder, final String region, final TransferStatus status) throws BackgroundException { try { if(DriveHomeFinderService.TEAM_DRIVES_NAME.equals(folder.getParent())) { final TeamDrive execute = session.getClient().teamdrives().create( new UUIDRandomStringService().random(), new TeamDrive().setName(folder.getName()) ).execute(); return new Path(folder.getParent(), folder.getName(), folder.getType(), new PathAttributes(folder.attributes()).withVersionId(execute.getId())); } else { // Identified by the special folder MIME type application/vnd.google-apps.folder final Drive.Files.Create insert = session.getClient().files().create(new File() .setName(folder.getName()) .setMimeType("application/vnd.google-apps.folder") .setParents(Collections.singletonList(fileid.getFileid(folder.getParent(), new DisabledListProgressListener())))); final File execute = insert .setSupportsTeamDrives(PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")).execute(); return new Path(folder.getParent(), folder.getName(), folder.getType(), new DriveAttributesFinderFeature(session, fileid).toAttributes(execute)); } } catch(IOException e) { throw new DriveExceptionMappingService().map("Cannot create folder {0}", e, folder); } }
private File mkdir_impl(GFile gFile, int retry) { try { // New file logger.info("Creating new directory..."); File file = new File(); file.setMimeType("application/vnd.google-apps.folder"); file.setName(gFile.getName()); file.setModifiedTime(new DateTime(System.currentTimeMillis())); file.setParents(new ArrayList<>(gFile.getParents())); file = drive.files().create(file).setFields(REQUEST_FILE_FIELDS).execute(); logger.info("Directory created successfully: " + file.getId()); return file; } catch (IOException e) { if (retry > 0) { try { Thread.sleep(1000); } catch (InterruptedException e1) { throw new RuntimeException(e1); } logger.warn("Uploading file failed. Retrying... '" + gFile.getId()); return mkdir_impl(gFile, --retry); } throw new RuntimeException("Exception uploading file " + gFile.getId(), e); } }
public void store(SyncDirectory syncDirectory) { Drive drive = driveFactory.getDrive(this.credential); try { java.io.File localFile = syncDirectory.getLocalFile().get(); File remoteFile = new File(); remoteFile.setName(localFile.getName()); remoteFile.setMimeType(MIME_TYPE_FOLDER); remoteFile.setParents(createParentReferenceList(syncDirectory)); BasicFileAttributes attr = Files.readAttributes(localFile.toPath(), BasicFileAttributes.class); remoteFile.setModifiedTime(new DateTime(attr.lastModifiedTime().toMillis())); LOGGER.log(Level.FINE, "Inserting new directory '" + syncDirectory.getPath() + "'."); if (!options.isDryRun()) { File insertedFile = executeWithRetry(options, () -> drive.files().create(remoteFile).execute()); syncDirectory.setRemoteFile(Optional.of(insertedFile)); } } catch (IOException e) { throw new JDriveSyncException(JDriveSyncException.Reason.IOException, "Failed to update file: " + e.getMessage(), e); } }
public File createDirectory(File parentDirectory, String title) { File returnValue = null; Drive drive = driveFactory.getDrive(this.credential); try { File remoteFile = new File(); remoteFile.setName(title); remoteFile.setMimeType(MIME_TYPE_FOLDER); remoteFile.setParents(Arrays.asList(parentDirectory.getId())); LOGGER.log(Level.FINE, "Creating new directory '" + title + "'."); if (!options.isDryRun()) { returnValue = executeWithRetry(options, () -> drive.files().create(remoteFile).execute()); } } catch (IOException e) { throw new JDriveSyncException(JDriveSyncException.Reason.IOException, "Failed to create directory: " + e.getMessage(), e); } return returnValue; }
private File createSpreadsheet(final String name, final java.io.File export, final String mime) throws IOException { final FileContent fc = new FileContent(mime, export); final File parent = getOrCreateRoboZonkyFolder(); // retrieve Google folder in which to place the spreadsheet // convert the spreadsheet to Google Spreadsheet final File f = new File(); f.setName(name); f.setParents(Collections.singletonList(parent.getId())); f.setMimeType(MIME_TYPE_GOOGLE_SPREADSHEET); LOGGER.debug("Creating a new Google spreadsheet: {}.", f); final File result = driveService.files().create(f, fc) .setFields(getFields()) .execute(); // and mark the time when the file was last updated LOGGER.debug("New Google spreadsheet created: {}.", result.getId()); return result; }
private File createSpreadsheet(final String name, final java.io.File export, final String mime) throws IOException { final FileContent fc = new FileContent(mime, export); final File parent = getOrCreateRoboZonkyFolder(); // retrieve Google folder in which to place the spreadsheet // convert the spreadsheet to Google Spreadsheet final File f = new File(); f.setName(name); f.setParents(Collections.singletonList(parent.getId())); f.setMimeType(MIME_TYPE_GOOGLE_SPREADSHEET); LOGGER.debug("Creating a new Google spreadsheet: {}.", f); final File result = driveService.files().create(f, fc) .setFields(getFields()) .execute(); // and mark the time when the file was last updated LOGGER.debug("New Google spreadsheet created: {}.", result.getId()); return result; }
/** * Create a folder in the specified parent folder * * @param parentFolderId folder ID where to create folderName * @param folderName new folder's name * @return folder ID value * @throws IOException when operation fails */ public String createFolder(String parentFolderId, String folderName) throws IOException { File createdFolder = new File(); createdFolder.setName(folderName); createdFolder.setMimeType(MIME_TYPE_FOLDER); createdFolder.setParents(Collections.singletonList(parentFolderId)); return drive.files().create(createdFolder).setFields("id").execute().getId(); }
/** * Create a folder in the specified parent folder * * @param parentFolderId folder ID where to create folderName * @param folderName new folder's name * @return folder ID value * @throws IOException when operation fails */ public String createFolder(String parentFolderId, String folderName) throws IOException { File createdFolder = new File(); createdFolder.setName(folderName); createdFolder.setMimeType(MIME_TYPE_FOLDER); createdFolder.setParents(Collections.singletonList(parentFolderId)); return drive.files().create(createdFolder).setFields("id").execute().getId(); }
@Override public Path touch(final Path file, final TransferStatus status) throws BackgroundException { try { final Drive.Files.Create insert = session.getClient().files().create(new File() .setName(file.getName()) .setMimeType(status.getMime()) .setParents(Collections.singletonList(fileid.getFileid(file.getParent(), new DisabledListProgressListener())))); final File execute = insert.setSupportsTeamDrives(PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")).execute(); return new Path(file.getParent(), file.getName(), file.getType(), new DriveAttributesFinderFeature(session, fileid).toAttributes(execute)); } catch(IOException e) { throw new DriveExceptionMappingService().map("Cannot create file {0}", e, file); } }
private File createRoboZonkyFolder(final Drive driveService) throws IOException { final File fileMetadata = new File(); fileMetadata.setName(getFolderName(sessionInfo)); fileMetadata.setDescription("RoboZonky aktualizuje obsah tohoto adresáře jednou denně brzy ráno."); fileMetadata.setMimeType(MIME_TYPE_FOLDER); final File result = driveService.files().create(fileMetadata) .setFields(getFields()) .execute(); LOGGER.debug("Created a new Google folder '{}'.", result.getId()); return result; }
private File createRoboZonkyFolder(final Drive driveService) throws IOException { final File fileMetadata = new File(); fileMetadata.setName(getFolderName(sessionInfo)); fileMetadata.setDescription("RoboZonky aktualizuje obsah tohoto adresáře jednou denně brzy ráno."); fileMetadata.setMimeType(MIME_TYPE_FOLDER); final File result = driveService.files().create(fileMetadata) .setFields(getFields()) .execute(); LOGGER.debug("Created a new Google folder '{}'.", result.getId()); return result; }
public void close() throws IOException { File file = new File(); file.setName( getName().getBaseName() ); if ( parent != null ) { file.setParents( Collections.singletonList( parent.getId() ) ); } ByteArrayContent fileContent = new ByteArrayContent( "application/octet-stream", toByteArray() ); if ( count > 0 ) { driveService.files().create( file, fileContent ).execute(); ( (GoogleDriveFileSystem) getFileSystem() ).clearFileFromCache( getName() ); } } };