Skip to content

Commit

Permalink
feat(drive): upload and download file
Browse files Browse the repository at this point in the history
  • Loading branch information
DenovVasil committed Jan 23, 2025
1 parent fcf9944 commit 4ccbc16
Show file tree
Hide file tree
Showing 13 changed files with 421 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@
"name" : "Create file from template",
"value" : "file"
}, {
"name" : "upload file",
"name" : "Upload file",
"value" : "upload"
}, {
"name" : "download file",
"name" : "Download file",
"value" : "download"
} ]
}, {
Expand All @@ -183,6 +183,11 @@
"name" : "resource.name",
"type" : "zeebe:input"
},
"condition" : {
"property" : "resource.type",
"oneOf" : [ "folder", "file" ],
"type" : "simple"
},
"type" : "String"
}, {
"id" : "resource.parent",
Expand All @@ -195,6 +200,11 @@
"name" : "resource.parent",
"type" : "zeebe:input"
},
"condition" : {
"property" : "resource.type",
"oneOf" : [ "folder", "file", "upload" ],
"type" : "simple"
},
"type" : "String"
}, {
"id" : "resource.additionalGoogleDriveProperties",
Expand Down Expand Up @@ -254,7 +264,7 @@
"constraints" : {
"notEmpty" : true
},
"feel" : "required",
"feel" : "optional",
"group" : "operationDetails",
"binding" : {
"name" : "resource.downloadData.id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@
"name" : "Create file from template",
"value" : "file"
}, {
"name" : "upload file",
"name" : "Upload file",
"value" : "upload"
}, {
"name" : "download file",
"name" : "Download file",
"value" : "download"
} ]
}, {
Expand All @@ -188,6 +188,11 @@
"name" : "resource.name",
"type" : "zeebe:input"
},
"condition" : {
"property" : "resource.type",
"oneOf" : [ "folder", "file" ],
"type" : "simple"
},
"type" : "String"
}, {
"id" : "resource.parent",
Expand All @@ -200,6 +205,11 @@
"name" : "resource.parent",
"type" : "zeebe:input"
},
"condition" : {
"property" : "resource.type",
"oneOf" : [ "folder", "file", "upload" ],
"type" : "simple"
},
"type" : "String"
}, {
"id" : "resource.additionalGoogleDriveProperties",
Expand Down Expand Up @@ -259,7 +269,7 @@
"constraints" : {
"notEmpty" : true
},
"feel" : "required",
"feel" : "optional",
"group" : "operationDetails",
"binding" : {
"name" : "resource.downloadData.id",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. Licensed under a proprietary license.
* See the License.txt file for more information. You may not use this file
* except in compliance with the proprietary license.
*/
package io.camunda.connector.gdrive;

import com.google.api.client.googleapis.media.MediaHttpUploader;
import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CustomProgressListener implements MediaHttpUploaderProgressListener {
private static final Logger LOGGER = LoggerFactory.getLogger(CustomProgressListener.class);

@Override
public void progressChanged(MediaHttpUploader uploader) throws IOException {
switch (uploader.getUploadState()) {
case NOT_STARTED:
LOGGER.debug("Upload not started");
break;
case INITIATION_STARTED:
LOGGER.debug("Initiation has started!");
break;
case INITIATION_COMPLETE:
LOGGER.debug("Initiation is complete!");
break;
case MEDIA_IN_PROGRESS:
LOGGER.debug("Upload progress: {}", uploader.getProgress());
break;
case MEDIA_COMPLETE:
LOGGER.debug("Upload is complete!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
*/
package io.camunda.connector.gdrive;

import com.google.api.services.drive.model.File;
import io.camunda.connector.api.annotation.OutboundConnector;
import io.camunda.connector.api.outbound.OutboundConnectorContext;
import io.camunda.connector.api.outbound.OutboundConnectorFunction;
import io.camunda.connector.gdrive.model.GoogleDriveResult;
import io.camunda.connector.gdrive.model.request.GoogleDriveRequest;
import io.camunda.connector.gdrive.supliers.GoogleDocsServiceSupplier;
import io.camunda.connector.generator.java.annotation.ElementTemplate;
import io.camunda.document.Document;
import io.camunda.document.store.DocumentCreationRequest;
import io.camunda.google.supplier.GoogleDriveServiceSupplier;
import java.io.ByteArrayOutputStream;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -54,15 +58,35 @@ public GoogleDriveFunction(final GoogleDriveService service) {
@Override
public Object execute(final OutboundConnectorContext context) {
var request = context.bindVariables(GoogleDriveRequest.class);
return executeConnector(request);

var result = executeConnector(request);

if (result instanceof Pair) {
return prepareDocument(context, (Pair<File, ByteArrayOutputStream>) result);
}

return result;
}

private GoogleDriveResult executeConnector(final GoogleDriveRequest request) {
private Object executeConnector(final GoogleDriveRequest request) {
LOGGER.debug("Executing my connector with request {}", request);

GoogleDriveClient drive =
new GoogleDriveClient(
GoogleDriveServiceSupplier.createDriveClientInstance(request.getAuthentication()),
GoogleDocsServiceSupplier.createDocsClientInstance(request.getAuthentication()));

return service.execute(drive, request.getResource());
}

private Document prepareDocument(
final OutboundConnectorContext context, final Pair<File, ByteArrayOutputStream> pair) {
byte[] bytes = pair.getRight().toByteArray();
var metaData = pair.getLeft();
return context.createDocument(
DocumentCreationRequest.from(bytes)
.contentType(metaData.getMimeType())
.fileName(metaData.getName())
.build());
}
}
Loading

0 comments on commit 4ccbc16

Please sign in to comment.