Skip to content

Commit

Permalink
1.2.0 url access (#40)
Browse files Browse the repository at this point in the history
* Add description

* Add label
  • Loading branch information
Pierre-Yves authored Jan 23, 2024
1 parent 16747a8 commit 4966a85
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 149 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target/
/.idea/
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# zebee-cherry-filestorage
[![](https://img.shields.io/badge/Community%20Extension-An%20open%20source%20community%20maintained%20project-FF4700)](https://github.com/camunda-community-hub/community)
![Compatible with: Camunda 8](https://img.shields.io/badge/Compatible%20with-Camunda%208-0072Ce)

Library to manipulate File process variable, and store the content in different storage (Folder, CMIS...)

This library is used by connectors. There is three main function
This library is used by connectors.


# Store file to the storage
Expand Down Expand Up @@ -51,7 +53,7 @@ The fileVariableReference is only a reference, and can be saved in the process v

# Read file from the storage

To read a file from the storage, the first operation consist to access the FileVariableReference
To read a file from the storage, the first operation consist of accessing the FileVariableReference

````
String processVariableSt = <GetFromProcessVariable>
Expand Down Expand Up @@ -137,3 +139,15 @@ import StorageCMIS;
StorageCMIS.getStorageDefinitionString(String url, String repositoryName, String userName, String password, String storageDefinitionFolder)
````

## URL
The file may be access via an URL. This storage is use to READ only.

For example, this soure file read a document from an URL.
`````json
{
"storageDefinition": "URL",
"content": "https://github.com/camunda-community-hub/camunda-8-connector-officetopdf/raw/main/src/test/resources/OfficeToPdfExample.docx"
}

`````
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.camunda.filestorage</groupId>
<artifactId>filestorage</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>

<properties>
<maven.compiler.release>17</maven.compiler.release>
Expand Down Expand Up @@ -36,7 +36,7 @@
<parent>
<groupId>org.camunda.community</groupId>
<artifactId>community-hub-release-parent</artifactId>
<version>1.3.1</version>
<version>1.4.2</version>
</parent>

<dependencies>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/camunda/filestorage/FileRepoFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ private Storage getStorage(StorageDefinition storageDefinition) throws Exception
case FOLDER -> new StorageFolder(storageDefinition, this);
case CMIS -> new StorageCMIS(storageDefinition, this);
case TEMPFOLDER -> new StorageTempFolder(storageDefinition, this);
case URL -> new StorageURL(storageDefinition, this);
};
}
}
151 changes: 84 additions & 67 deletions src/main/java/io/camunda/filestorage/FileVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,87 +7,104 @@
package io.camunda.filestorage;

import java.io.File;
import java.net.URLConnection;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/* Definition of a FileVariable */
public class FileVariable {
private String name;
private String mimeType;
private byte[] value;

/**
* Keep the information from where this fileVariable come from.
* So, if the worker wants to save it at the same place, it has the information.
* This is only an information from the FileVariable, it may be null
*/
private StorageDefinition storageDefinition;

/**
* The default connectors exist to let the Json deserializer create it
*/
public FileVariable() {
private String name;
private String mimeType;
private byte[] value;

}
/**
* To load / create a file Variable, go to the FileVariableFactory
*/
protected FileVariable(StorageDefinition storageDefinition) {
this.storageDefinition = storageDefinition;
}
/**
* Keep the information from where this fileVariable come from.
* So, if the worker wants to save it at the same place, it has the information.
* This is only an information from the FileVariable, it may be null
*/
private StorageDefinition storageDefinition;

public String getName() {
return name;
}
/**
* The default connectors exist to let the Json deserializer create it
*/
public FileVariable() {

public String getMimeType() {
return mimeType;
}
}

public byte[] getValue() {
return value;
}
/**
* To load / create a file Variable, go to the FileVariableFactory
*/
protected FileVariable(StorageDefinition storageDefinition) {
this.storageDefinition = storageDefinition;
}

public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}

public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
public String getMimeType() {
return mimeType;
}

public void setValue(byte[] value) {
this.value = value;
}
public byte[] getValue() {
return value;
}

public void setStorageDefinition(StorageDefinition storageDefinition) {
this.storageDefinition = storageDefinition;
}
public StorageDefinition getStorageDefinition() {
return storageDefinition;
}
public void setName(String name) {
this.name = name;
}

public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}

public void setValue(byte[] value) {
this.value = value;
}

/**
* Return the suffix of the file, based on the name or on the mimeType
*
* @return the suffix
*/
public static String getSuffix(String fileName) {
if (fileName != null) {
int lastDot = fileName.lastIndexOf(".");
if (lastDot != -1)
return fileName.substring(lastDot + 1);
}
return "";
public void setStorageDefinition(StorageDefinition storageDefinition) {
this.storageDefinition = storageDefinition;
}

public StorageDefinition getStorageDefinition() {
return storageDefinition;
}

/**
* Return the suffix of the file, based on the name or on the mimeType
*
* @return the suffix
*/
public static String getSuffix(String fileName) {
if (fileName != null) {
int lastDot = fileName.lastIndexOf(".");
if (lastDot != -1)
return fileName.substring(lastDot + 1);
}
return "";
}

/**
* return the Mimetype from the name.
*
* @return the mime type
*/
public static String getMimeTypeFromName(String fileName) {
File file = new File(fileName);
return URLConnection.guessContentTypeFromName(file.getName());
/**
* return the Mimetype from the fileName.
*
* @param fileName file name. Must exist/accessible to be transformed to a File
* @return the mime type
*/
public static String getMimeTypeFromName(String fileName) {
return getMimeTypeFromPath(new File(fileName).toPath());
}

/**
* Return the MimeType from a Path
* @param path path to the information
* @return the mime type
*/
public static String getMimeTypeFromPath(Path path) {
try {
return Files.probeContentType(path);
} catch (IOException var3) {
return null;
}
}

}
45 changes: 32 additions & 13 deletions src/main/java/io/camunda/filestorage/FileVariableReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,17 @@ public class FileVariableReference {
public static FileVariableReference fromJson(String fileReferenceJson) throws Exception {
try {
return new ObjectMapper().readValue(fileReferenceJson, FileVariableReference.class);
} catch (JsonProcessingException e) {
logger.error("FileStorage.FileVariableReference.fromJson: exception " + e + " During un serialize fileVariable");
} catch (Exception e) {
}
// do a second tentative before logging
String fileReferenceJsonWithoutBackslash = fileReferenceJson.replace("\\\"", "\"");
try {
// if the value is given explicitly, the modeler impose to \ each ", so we have to replace all \" by "
return new ObjectMapper().readValue(fileReferenceJsonWithoutBackslash, FileVariableReference.class);
} catch (Exception e) {
// then now we have to log the error
logger.error("FileStorage.FileVariableReference.fromJson:" + e + " During UnSerialize[" + fileReferenceJson
+ "], secondTentative[" + fileReferenceJsonWithoutBackslash + "]");
throw e;
}
}
Expand Down Expand Up @@ -70,17 +79,27 @@ public Object getContent() {

/**
* Must be static to not make any trouble in the serialization/deserialization
* @param fieldReference field reference to get the identification
* @return an indentification, to log it for example
*
* @return information, to log it for example
*/
public static String getIdentification(FileVariableReference fieldReference) {
StringBuilder result = new StringBuilder();
result.append(fieldReference.storageDefinition);
result.append( ": ");
if (fieldReference.content==null)
result.append("null");
else
result.append((fieldReference.content.toString() + " ").substring(0, 50));
return result.toString();
public static String getInformation(FileVariableReference fileVariableReference) {
StringBuilder result = new StringBuilder();
try {
StorageDefinition storageDefinition = StorageDefinition.getFromString(fileVariableReference.storageDefinition);
result.append(storageDefinition.getInformation());
} catch (Exception e) {
result.append("Can't get storageDefinition from [");
result.append(fileVariableReference.storageDefinition);
result.append("] : ");
result.append(e.getMessage());
}
result.append(": ");
if (fileVariableReference.content == null)
result.append("null");
else if (fileVariableReference.content.toString().length() < 100)
result.append(fileVariableReference.content.toString());
else
result.append(fileVariableReference.content.toString().substring(0, 100));
return result.toString();
}
}
Loading

0 comments on commit 4966a85

Please sign in to comment.