use of org.talend.components.google.drive.runtime.utils.GoogleDriveGetResult in project components by Talend.
the class GoogleDriveUtils method getResource.
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);
}
use of org.talend.components.google.drive.runtime.utils.GoogleDriveGetResult in project components by Talend.
the class GoogleDriveGetReader method start.
@Override
public boolean start() throws IOException {
super.start();
//
String resourceId = properties.fileAccessMethod.getValue().equals(AccessMethod.Id) ? properties.file.getValue() : utils.getFileId(properties.file.getValue());
Map<String, MimeType> mimes = GoogleDriveMimeTypes.newDefaultMimeTypesSupported();
mimes.put(MIME_TYPE_GOOGLE_DOCUMENT, properties.exportDocument.getValue());
mimes.put(MIME_TYPE_GOOGLE_DRAWING, properties.exportDrawing.getValue());
mimes.put(MIME_TYPE_GOOGLE_PRESENTATION, properties.exportPresentation.getValue());
mimes.put(MIME_TYPE_GOOGLE_SPREADSHEET, properties.exportSpreadsheet.getValue());
GoogleDriveGetParameters p = new GoogleDriveGetParameters(resourceId, mimes, properties.storeToLocal.getValue(), properties.outputFileName.getValue(), properties.setOutputExt.getValue());
//
GoogleDriveGetResult r = utils.getResource(p);
fileId = r.getId();
byte[] content = r.getContent();
record = new Record(properties.schemaMain.schema.getValue());
record.put(0, content);
result.totalCount++;
result.successCount++;
return true;
}
use of org.talend.components.google.drive.runtime.utils.GoogleDriveGetResult in project components by Talend.
the class GoogleDriveGetRuntime method getFile.
public void getFile(RuntimeContainer container) {
try {
String resourceId = properties.fileAccessMethod.getValue().equals(AccessMethod.Id) ? properties.file.getValue() : getDriveUtils().getFileId(properties.file.getValue());
Map<String, MimeType> mimes = GoogleDriveMimeTypes.newDefaultMimeTypesSupported();
mimes.put(MIME_TYPE_GOOGLE_DOCUMENT, properties.exportDocument.getValue());
mimes.put(MIME_TYPE_GOOGLE_DRAWING, properties.exportDrawing.getValue());
mimes.put(MIME_TYPE_GOOGLE_PRESENTATION, properties.exportPresentation.getValue());
mimes.put(MIME_TYPE_GOOGLE_SPREADSHEET, properties.exportSpreadsheet.getValue());
GoogleDriveGetParameters p = new GoogleDriveGetParameters(resourceId, mimes, properties.storeToLocal.getValue(), properties.outputFileName.getValue(), properties.setOutputExt.getValue());
//
GoogleDriveGetResult r = getDriveUtils().getResource(p);
fileId = r.getId();
} catch (IOException | GeneralSecurityException e) {
LOG.error(e.getMessage());
throw new ComponentException(e);
}
}
Aggregations