Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable creating CouchDB attachments for images #1716

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.streampipes.model.file;

public class GenericStorageAttachment {
String contentType;
byte[] attachment;

public GenericStorageAttachment(String contentType, byte[] attachment) {
this.contentType = contentType;
this.attachment = attachment;
}

public String getContentType() {
return contentType;
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

public byte[] getAttachment() {
return attachment;
}

public void setAttachment(byte[] attachment) {
this.attachment = attachment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,6 +104,25 @@ public void delete(String id, String rev) throws IOException {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ public static Request putRequest(String route,
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();
}
Expand Down