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() ); } } }
if (FOLDER_MIME_TYPE.equals(file.getMimeType())) { folders.add(new BlobbyStorageContainerResource(file.getName(), file.getId(), null, null)); } else if (FUSION_TABLE_MIME_TYPE.equals(file.getMimeType())) { monitor.info(() -> "Exporting of fusion tables is not yet supported: " + file); } else if (MAP_MIME_TYPE.equals(file.getMimeType())) { monitor.info(() -> "Exporting of maps is not yet supported: " + file); } else { try { InputStream inputStream; String newMimeType = file.getMimeType(); if (EXPORT_FORMATS.containsKey(file.getMimeType())) { newMimeType = EXPORT_FORMATS.get(file.getMimeType()); inputStream = driveInterface .files() .export(file.getId(), newMimeType) .executeMedia() .getContent(); driveInterface .files() .get(file.getId()) .setAlt("media") .executeMedia() .getContent(); jobStore.create(jobId, file.getId(), inputStream); files.add( new DigitalDocumentWrapper(
private FileStatus toFileStatus(File metadata) { return new FileStatus(metadata.getSize() == null ? 0L : metadata.getSize(), FOLDER_MIME_TYPE.equals(metadata.getMimeType()), -1, -1, metadata.getModifiedTime().getValue(), new Path(metadata.getId())); }
public static DriveOverview create(final SessionInfo sessionInfo, final Drive driveService, final Sheets sheetService) throws IOException { final String folderName = getFolderName(sessionInfo); LOGGER.debug("Listing all files in the root of Google Drive, looking up folder: {}.", folderName); final List<File> files = getFilesInFolder(driveService, "root").collect(Collectors.toList()); final Optional<File> result = files.stream() .peek(f -> LOGGER.debug("Found '{}' ({}) as {}.", f.getName(), f.getMimeType(), f.getId())) .filter(f -> Objects.equals(f.getMimeType(), MIME_TYPE_FOLDER)) .filter(f -> Objects.equals(f.getName(), folderName)) .findFirst(); if (result.isPresent()) { return create(sessionInfo, driveService, sheetService, result.get()); } else { return new DriveOverview(sessionInfo, driveService, sheetService); } }
private IndexedRecord convertSearchResultToIndexedRecord(File file) { // Main record IndexedRecord main = new GenericData.Record(schema); main.put(0, file.getId()); main.put(1, file.getName()); main.put(2, file.getMimeType()); main.put(3, file.getModifiedTime().getValue()); main.put(4, file.getSize()); main.put(5, file.getKind()); main.put(6, file.getTrashed()); main.put(7, file.getParents().toString()); // TODO This should be a List<String> main.put(8, file.getWebViewLink()); return main; }
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 FileList createFileList(java.util.List<String> fileIds, String folderId) { FileList fileList = new FileList(); java.util.List<File> list = Lists.newArrayList(); for (String fileId : fileIds) { File f = new File(); f.setId(fileId); f.setModifiedTime(new DateTime(System.currentTimeMillis())); list.add(f); } if (folderId != null) { File f = new File(); f.setMimeType(FOLDER_MIME_TYPE); f.setId(folderId); f.setModifiedTime(new DateTime(System.currentTimeMillis())); list.add(f); } fileList.setFiles(list); return fileList; } }
private GFile create(File file) { GFile newFile = new GFile(file.getName() != null ? file.getName() : file.getOriginalFilename()); newFile.setId(file.getId()); newFile.setLastModified(file.getModifiedTime() != null ? file.getModifiedTime().getValue() : 0); newFile.setDirectory(GFile.MIME_TYPE.GOOGLE_FOLDER.getValue().equals(file.getMimeType())); newFile.setSize(file.getSize() != null ? file.getSize() : 0); // null for directories newFile.setMimeType(file.getMimeType()); newFile.setMd5Checksum(file.getMd5Checksum()); if (file.getParents() != null) { Set<String> newParents = new HashSet<>(); for (String newParent : file.getParents()) { newParents.add(newParent.equals(ROOT_FOLDER_ID) ? "root" : newParent); } newFile.setParents(newParents); } else { // does this happen? newFile.setParents(Collections.singleton("root")); } newFile.setTrashed(file.getTrashed() != null && file.getTrashed()); return newFile; }
private void resolveFileMetadata() throws Exception { String parentId = null; if ( getName().getParent() != null ) { File parent = searchFile( getName().getParent().getBaseName(), null ); if ( parent != null ) { FileType mime = MIME_TYPES.get( parent.getMimeType() ); if ( mime.equals( FileType.FOLDER ) ) { parentId = parent.getId(); } } } String fileName = getName().getBaseName(); File file = searchFile( fileName, parentId ); if ( file != null ) { mimeType = MIME_TYPES.get( file.getMimeType() ); id = file.getId(); } else { if ( getName().getURI().equals( GoogleDriveFileProvider.SCHEME + ":///" ) ) { mimeType = FileType.FOLDER; } } }
private void removeDuplicates(List<File> resultList) { Map<String, File> fileNameMap = new HashMap<>(); Iterator<File> iterator = resultList.iterator(); while (iterator.hasNext()) { File file = iterator.next(); String title = file.getName(); File titleFound = fileNameMap.get(title); if (titleFound == null) { fileNameMap.put(title, file); } else { LOGGER.log(Level.WARNING, "Ignoring remote file '" + title + "' (id: '" + file.getId() + "') because its title/name appears more than once in this folder."); iterator.remove(); } } }
protected PathAttributes toAttributes(final File f) { final PathAttributes attributes = new PathAttributes(); if(null != f.getExplicitlyTrashed()) { if(f.getExplicitlyTrashed()) { if(null != f.getSize()) { if(!DRIVE_FOLDER.equals(f.getMimeType()) && !StringUtils.startsWith(f.getMimeType(), GOOGLE_APPS_PREFIX)) { attributes.setSize(f.getSize()); attributes.setVersionId(f.getId()); if(f.getModifiedTime() != null) { attributes.setModificationDate(f.getModifiedTime().getValue()); if(f.getCreatedTime() != null) { attributes.setCreationDate(f.getCreatedTime().getValue()); attributes.setChecksum(Checksum.parse(f.getMd5Checksum())); if(StringUtils.isNotBlank(f.getWebViewLink())) { attributes.setLink(new DescriptiveUrl(URI.create(f.getWebViewLink()), DescriptiveUrl.Type.http, MessageFormat.format(LocaleFactory.localizedString("{0} URL"), "HTTP"))); if(!DRIVE_FOLDER.equals(f.getMimeType()) && StringUtils.startsWith(f.getMimeType(), GOOGLE_APPS_PREFIX)) { attributes.setSize(UrlFileWriterFactory.get().write(new DescriptiveUrl(URI.create(f.getWebViewLink()))) .getBytes(Charset.defaultCharset()).length);
final File s = o.latestStonky(); LOGGER.debug("Making a backup of the existing spreadsheet."); final Instant lastModified = Instant.EPOCH.plus(Duration.ofMillis(s.getModifiedTime().getValue())); final LocalDate d = lastModified.atZone(Defaults.ZONE_ID).toLocalDate(); if (d.isBefore(DateUtil.localNow().toLocalDate())) { Util.copyFile(driveService, s, o.getFolder(), d + " " + s.getName()); final Spreadsheet result = sheetsService.spreadsheets().get(s.getId()).execute(); return new Summary(o, result); })); final File f = s.getOverview().latestWelcome(welcome); final Spreadsheet result = copySheet(sheetsService, s.getStonky(), f, InternalSheet.WELCOME); driveService.files().delete(f.getId()).execute(); return result; }));
private File modifySpreadsheet(final File original, final java.io.File export) throws IOException { final String id = original.getId(); LOGGER.debug("Updating an existing Google spreadsheet: {}.", id); final File result = actuallyModifySpreadsheet(original, export); LOGGER.debug("Google spreadsheet updated."); return result; }
public String copyPresentation(String presentationId, String copyTitle) throws IOException { Drive driveService = this.driveService; // [START slides_copy_presentation] File copyMetadata = new File().setName(copyTitle); File presentationCopyFile = driveService.files().copy(presentationId, copyMetadata).execute(); String presentationCopyId = presentationCopyFile.getId(); // [END slides_copy_presentation] return presentationCopyId; }
if(!StringUtils.equals(file.getName(), renamed.getName())) { final File properties = new File(); properties.setName(renamed.getName()); properties.setMimeType(status.getMime()); session.getClient().files().update(id, properties). setSupportsTeamDrives(PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")).execute(); .setSupportsTeamDrives(PreferencesFactory.get().getBoolean("googledrive.teamdrive.enable")) .execute(); for(String parent : reference.getParents()) { previousParents.append(parent); previousParents.append(',');
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); } }
/** * Touches the file, changing the name or date modified * * @param fileId the file id to patch * @param newName the new file name * @param newLastModified the new last modified date * @return the patched file */ public GFile patchFile(String fileId, String newName, long newLastModified) { File patch = new File(); if (newName != null) { patch.setName(newName); } if (newLastModified > 0) { patch.setModifiedTime(new DateTime(newLastModified)); } return create(patchFile(fileId, patch, 3)); }
public File trashFile(String fileId) { File patch = new File(); patch.setTrashed(true); return patchFile(fileId, patch, 3); }
final com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File(); body.setTitle("My Test File"); body.setDescription("A Test File");