Search in sources :

Example 1 with GoogleDriveGetResult

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);
}
Also used : GoogleDriveGetResult(org.talend.components.google.drive.runtime.utils.GoogleDriveGetResult) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(com.google.api.services.drive.model.File)

Example 2 with GoogleDriveGetResult

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;
}
Also used : GoogleDriveGetResult(org.talend.components.google.drive.runtime.utils.GoogleDriveGetResult) GoogleDriveGetParameters(org.talend.components.google.drive.runtime.utils.GoogleDriveGetParameters) Record(org.apache.avro.generic.GenericData.Record) MimeType(org.talend.components.google.drive.GoogleDriveMimeTypes.MimeType)

Example 3 with GoogleDriveGetResult

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);
    }
}
Also used : GoogleDriveGetResult(org.talend.components.google.drive.runtime.utils.GoogleDriveGetResult) GoogleDriveGetParameters(org.talend.components.google.drive.runtime.utils.GoogleDriveGetParameters) GeneralSecurityException(java.security.GeneralSecurityException) ComponentException(org.talend.components.api.exception.ComponentException) IOException(java.io.IOException) MimeType(org.talend.components.google.drive.GoogleDriveMimeTypes.MimeType)

Aggregations

GoogleDriveGetResult (org.talend.components.google.drive.runtime.utils.GoogleDriveGetResult)3 MimeType (org.talend.components.google.drive.GoogleDriveMimeTypes.MimeType)2 GoogleDriveGetParameters (org.talend.components.google.drive.runtime.utils.GoogleDriveGetParameters)2 File (com.google.api.services.drive.model.File)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Record (org.apache.avro.generic.GenericData.Record)1 ComponentException (org.talend.components.api.exception.ComponentException)1