This is an automated email from the ASF dual-hosted git repository.

oehler pushed a commit to branch storage-attachment-for-images
in repository https://gitbox.apache.org/repos/asf/streampipes.git

commit 9a5c847c98384323bc36bea00e04c72154bed85a
Author: Sven Oehler <[email protected]>
AuthorDate: Tue Jun 20 17:32:05 2023 +0200

    Add creation of attachments for CouchDB
---
 .../model/file/GenericStorageAttachment.java       | 34 +++++++++++++---------
 .../streampipes/storage/api/IGenericStorage.java   |  7 +++++
 .../storage/couchdb/impl/GenericStorageImpl.java   | 20 +++++++++++++
 .../streampipes/storage/couchdb/utils/Utils.java   |  6 ++++
 4 files changed, 53 insertions(+), 14 deletions(-)

diff --git 
a/streampipes-storage-api/src/main/java/org/apache/streampipes/storage/api/IGenericStorage.java
 
b/streampipes-model/src/main/java/org/apache/streampipes/model/file/GenericStorageAttachment.java
similarity index 58%
copy from 
streampipes-storage-api/src/main/java/org/apache/streampipes/storage/api/IGenericStorage.java
copy to 
streampipes-model/src/main/java/org/apache/streampipes/model/file/GenericStorageAttachment.java
index b74bbc5b3..54c7f44fa 100644
--- 
a/streampipes-storage-api/src/main/java/org/apache/streampipes/storage/api/IGenericStorage.java
+++ 
b/streampipes-model/src/main/java/org/apache/streampipes/model/file/GenericStorageAttachment.java
@@ -15,24 +15,30 @@
  * limitations under the License.
  *
  */
+package org.apache.streampipes.model.file;
 
-package org.apache.streampipes.storage.api;
+public class GenericStorageAttachment {
+  String contentType;
+  byte[] attachment;
 
-import java.io.IOException;
-import java.util.List;
-import java.util.Map;
+  public GenericStorageAttachment(String contentType, byte[] attachment) {
+    this.contentType = contentType;
+    this.attachment = attachment;
+  }
 
-public interface IGenericStorage {
+  public String getContentType() {
+    return contentType;
+  }
 
-  List<Map<String, Object>> findAll(String type) throws IOException;
+  public void setContentType(String contentType) {
+    this.contentType = contentType;
+  }
 
-  Map<String, Object> findOne(String id) throws IOException;
+  public byte[] getAttachment() {
+    return attachment;
+  }
 
-  Map<String, Object> create(String payload) throws IOException;
-
-  <T> T create(T payload, Class<T> targetClass) throws IOException;
-
-  Map<String, Object> update(String id, String payload) throws IOException;
-
-  void delete(String id, String rev) throws IOException;
+  public void setAttachment(byte[] attachment) {
+    this.attachment = attachment;
+  }
 }
diff --git 
a/streampipes-storage-api/src/main/java/org/apache/streampipes/storage/api/IGenericStorage.java
 
b/streampipes-storage-api/src/main/java/org/apache/streampipes/storage/api/IGenericStorage.java
index b74bbc5b3..528e8c93b 100644
--- 
a/streampipes-storage-api/src/main/java/org/apache/streampipes/storage/api/IGenericStorage.java
+++ 
b/streampipes-storage-api/src/main/java/org/apache/streampipes/storage/api/IGenericStorage.java
@@ -18,6 +18,8 @@
 
 package org.apache.streampipes.storage.api;
 
+import org.apache.streampipes.model.file.GenericStorageAttachment;
+
 import java.io.IOException;
 import java.util.List;
 import java.util.Map;
@@ -35,4 +37,9 @@ public interface IGenericStorage {
   Map<String, Object> update(String id, String payload) throws IOException;
 
   void delete(String id, String rev) throws IOException;
+
+  void createAttachment(String docId, String attachmentName, String 
contentType, byte[] payload, String rev)
+      throws IOException;
+
+  GenericStorageAttachment findAttachment(String docId, String attachmentName) 
throws IOException;
 }
diff --git 
a/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/impl/GenericStorageImpl.java
 
b/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/impl/GenericStorageImpl.java
index 0817e5229..bf2936d0b 100644
--- 
a/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/impl/GenericStorageImpl.java
+++ 
b/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/impl/GenericStorageImpl.java
@@ -18,6 +18,7 @@
 
 package org.apache.streampipes.storage.couchdb.impl;
 
+import org.apache.streampipes.model.file.GenericStorageAttachment;
 import org.apache.streampipes.storage.api.IGenericStorage;
 import 
org.apache.streampipes.storage.couchdb.constants.GenericCouchDbConstants;
 import org.apache.streampipes.storage.couchdb.utils.Utils;
@@ -103,6 +104,25 @@ public class GenericStorageImpl implements IGenericStorage 
{
     Content content = executeAndReturnContent(req);
   }
 
+  @Override
+  public void createAttachment(String docId,
+                               String attachmentName,
+                               String contentType,
+                               byte[] payload,
+                               String rev) throws IOException {
+    Request req = Utils.putRequest(
+            getDatabaseRoute() + SLASH + docId + SLASH + attachmentName + 
"?rev=" + rev, payload, contentType
+    );
+    executeAndReturnContent(req);
+  }
+
+  @Override
+  public GenericStorageAttachment findAttachment(String docId, String 
attachmentName) throws IOException {
+    Request req = Utils.getRequest(getDatabaseRoute() + SLASH + docId + SLASH 
+ attachmentName);
+    Content content =  executeAndReturnContent(req);
+    return new GenericStorageAttachment(content.getType().getMimeType(), 
content.asBytes());
+  }
+
   private Map<String, Object> queryDocuments(String route) throws IOException {
     Request req = Utils.getRequest(route);
     Content content = executeAndReturnContent(req);
diff --git 
a/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/utils/Utils.java
 
b/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/utils/Utils.java
index ba4356f0f..36dbe2029 100644
--- 
a/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/utils/Utils.java
+++ 
b/streampipes-storage-couchdb/src/main/java/org/apache/streampipes/storage/couchdb/utils/Utils.java
@@ -216,6 +216,12 @@ public class Utils {
     return append(Request.Put(route).bodyString(payload, 
ContentType.APPLICATION_JSON));
   }
 
+  public static Request putRequest(String route,
+                                   byte[] payload,
+                                   String contentType) {
+    return append(Request.Put(route).bodyByteArray(payload, 
ContentType.getByMimeType(contentType)));
+  }
+
   private static Environment getEnvironment() {
     return Environments.getEnvironment();
   }

Reply via email to