.files() .export(file.getId(), newMimeType) .executeMedia() .getContent(); } else {
logger.info("Download file as sheet... '" + gFile.getId()); return drive.files().export(file.getId(), GFile.MIME_TYPE.MS_EXCEL.getValue()).executeMediaAsInputStream(); GFile.MIME_TYPE.MS_WORD.getValue()).executeMediaAsInputStream();
public GoogleDriveGetResult getResource(GoogleDriveGetParameters parameters) throws IOException { String fileId = parameters.getResourceId(); File file = getMetadata(fileId, "id,mimeType,fileExtension"); String fileMimeType = file.getMimeType(); String outputFileExt = "." + file.getFileExtension(); LOG.debug("[getResource] Found fileName `{}` [id: {}, mime: {}, ext: {}]", parameters.getResourceId(), fileId, fileMimeType, file.getFileExtension()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); /* Google Apps types */ if (GoogleDriveMimeTypes.GOOGLE_DRIVE_APPS.contains(fileMimeType)) { String exportFormat = parameters.getMimeType().get(fileMimeType).getMimeType(); outputFileExt = parameters.getMimeType().get(fileMimeType).getExtension(); drive.files().export(fileId, exportFormat).executeMediaAndDownloadTo(outputStream); } else { /* Standard fileName */ drive.files().get(fileId).executeMediaAndDownloadTo(outputStream); } byte[] content = outputStream.toByteArray(); if (parameters.isStoreToLocal()) { String localFile = parameters.getOutputFileName(); if (parameters.isAddExt()) { localFile = localFile + ((localFile.endsWith(outputFileExt)) ? "" : outputFileExt); } LOG.info(messages.getMessage("message.writing.resource", parameters.getResourceId(), localFile)); try (FileOutputStream fout = new FileOutputStream(localFile)) { fout.write(content); fout.close(); } } return new GoogleDriveGetResult(fileId, content); }
public InputStream downloadFile(SyncItem syncItem) { Drive drive = driveFactory.getDrive(this.credential); try { File remoteFile = syncItem.getRemoteFile().get(); GenericUrl genericUrl = null; if(isGoogleAppsDocumentforExport(remoteFile)){ Optional<String> exportMimeType = supportedGooglMimeType.get(remoteFile.getMimeType()); Export export = drive.files().export(remoteFile.getId(), exportMimeType.get()); genericUrl = export.buildHttpRequestUrl(); }else{ genericUrl = drive.files().get(remoteFile.getId()).set("alt", "media").buildHttpRequestUrl(); } if (genericUrl != null) { HttpRequest httpRequest = drive.getRequestFactory().buildGetRequest(genericUrl); LOGGER.log(Level.FINE, "Downloading file " + remoteFile.getId() + "."); if (!options.isDryRun()) { HttpResponse httpResponse = executeWithRetry(options, () -> httpRequest.execute()); return httpResponse.getContent(); } } else { LOGGER.log(Level.SEVERE, "No download URL for file " + remoteFile); } } catch (Exception e) { throw new JDriveSyncException(JDriveSyncException.Reason.IOException, "Failed to download file: " + e.getMessage(), e); } return new ByteArrayInputStream(new byte[0]); }
public GoogleDriveGetResult getResource(GoogleDriveGetParameters parameters) throws IOException { String fileId = parameters.getResourceId(); File file = getMetadata(fileId, "id,mimeType,fileExtension"); String fileMimeType = file.getMimeType(); String outputFileExt = "." + file.getFileExtension(); LOG.debug("[getResource] Found fileName `{}` [id: {}, mime: {}, ext: {}]", parameters.getResourceId(), fileId, fileMimeType, file.getFileExtension()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); /* Google Apps types */ if (GoogleDriveMimeTypes.GOOGLE_DRIVE_APPS.contains(fileMimeType)) { String exportFormat = parameters.getMimeType().get(fileMimeType).getMimeType(); outputFileExt = parameters.getMimeType().get(fileMimeType).getExtension(); drive.files().export(fileId, exportFormat).executeMediaAndDownloadTo(outputStream); } else { /* Standard fileName */ drive.files().get(fileId).executeMediaAndDownloadTo(outputStream); } byte[] content = outputStream.toByteArray(); if (parameters.isStoreToLocal()) { String localFile = parameters.getOutputFileName(); if (parameters.isAddExt()) { localFile = localFile + ((localFile.endsWith(outputFileExt)) ? "" : outputFileExt); } LOG.info(messages.getMessage("message.writing.resource", parameters.getResourceId(), localFile)); try (FileOutputStream fout = new FileOutputStream(localFile)) { fout.write(content); fout.close(); } } return new GoogleDriveGetResult(fileId, content); }
/** * Exports a Google Doc to the requested MIME type and returns the exported content. Please note * that the exported content is limited to 10MB. * * Create a request for the method "files.export". * * This request holds the parameters needed by the the drive server. After setting any optional * parameters, call the {@link Export#execute()} method to invoke the remote operation. <p> {@link * Export#initialize(com.google.api.client.googleapis.services.AbstractGoogleClientRequest)} must * be called to initialize this instance immediately after invoking the constructor. </p> * * @param fileId The ID of the file. * @param mimeType The MIME type of the format requested for this export. * @since 1.13 */ protected Export(java.lang.String fileId, java.lang.String mimeType) { super(Drive.this, "GET", REST_PATH, null, Void.class); this.fileId = com.google.api.client.util.Preconditions.checkNotNull(fileId, "Required parameter fileId must be specified."); this.mimeType = com.google.api.client.util.Preconditions.checkNotNull(mimeType, "Required parameter mimeType must be specified."); initializeMediaDownload(); }
/** * Exports a Google Doc to the requested MIME type and returns the exported content. Please note * that the exported content is limited to 10MB. * * Create a request for the method "files.export". * * This request holds the parameters needed by the drive server. After setting any optional * parameters, call the {@link Export#execute()} method to invoke the remote operation. * * @param fileId The ID of the file. * @param mimeType The MIME type of the format requested for this export. * @return the request */ public Export export(java.lang.String fileId, java.lang.String mimeType) throws java.io.IOException { Export result = new Export(fileId, mimeType); initialize(result); return result; }