Skip to content
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
Expand Up @@ -20,6 +20,7 @@ public class ArtifactJson extends HashMap<String, Object> {
public static final String EXTENSION = "extension";
public static final String LOCATION = "location";
public static final String LOCATIONTYPE = "locationType";
public static final String CHECKSUM = "checksum";

public ArtifactJson() {
super();
Expand Down Expand Up @@ -77,6 +78,18 @@ public ArtifactJson setLocationType(String locationType) {
return this;
}

@JsonProperty(CHECKSUM)
@Schema(accessMode = Schema.AccessMode.READ_ONLY)
public String getChecksum() {
return (String) this.get(CHECKSUM);
}

@JsonProperty(CHECKSUM)
public ArtifactJson setChecksum(String checksum) {
this.put(CHECKSUM, checksum);
return this;
}

public static List<ArtifactJson> getArtifacts(ElementJson elementJson){

List<Object> rawArtifacts = (List)elementJson.get(ARTIFACTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class ArtifactResponse {
private String mimeType;
private String extension;
private String checksum;
private byte[] data;

public String getMimeType() {
Expand All @@ -21,6 +22,14 @@ public void setExtension(String extension) {
this.extension = extension;
}

public String getChecksum() {
return checksum;
}

public void setChecksum(String checksum) {
this.checksum = checksum;
}

public byte[] getData() {
return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
import org.openmbee.mms.json.ElementJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class DefaultArtifactService implements ArtifactService {
Expand Down Expand Up @@ -54,6 +58,7 @@ public ArtifactResponse get(String projectId, String refId, String id, Map<Strin
response.setData(data);
response.setExtension(artifact.getExtension());
response.setMimeType(artifact.getMimeType());
response.setChecksum(artifact.getChecksum());
return response;
}

Expand All @@ -79,9 +84,10 @@ public ElementsResponse createOrUpdate(String projectId, String refId, String id

String mimeType = getMimeTypeOfFile(file);
String fileExtension = getFileExtension(file);
String checksum = getChecksumOfFile(file);
String artifactLocation = artifactStorage.store(fileContents, elementJson, mimeType);

elementJson = attachOrUpdateArtifact(elementJson, artifactLocation, fileExtension, mimeType, "internal");
elementJson = attachOrUpdateArtifact(elementJson, artifactLocation, fileExtension, mimeType, "internal", checksum);
ElementsRequest elementsRequest = new ElementsRequest();
elementsRequest.setElements(Arrays.asList(elementJson));
return nodeService.createOrUpdate(projectId, refId, elementsRequest, params, user);
Expand Down Expand Up @@ -113,7 +119,7 @@ private ElementJson getElement(NodeService nodeService, String projectId, String
}
}

private ElementJson attachOrUpdateArtifact(ElementJson elementJson, String artifactLocation, String fileExtension, String mimeType, String type) {
private ElementJson attachOrUpdateArtifact(ElementJson elementJson, String artifactLocation, String fileExtension, String mimeType, String type, String checksum) {

List<ArtifactJson> artifacts = ArtifactJson.getArtifacts(elementJson);
ArtifactJson artifact;
Expand All @@ -128,6 +134,7 @@ private ElementJson attachOrUpdateArtifact(ElementJson elementJson, String artif
artifact.setExtension(fileExtension);
artifact.setMimeType(mimeType);
artifact.setLocationType(type);
artifact.setChecksum(checksum);

ArtifactJson.setArtifacts(elementJson, artifacts);
return elementJson;
Expand Down Expand Up @@ -166,6 +173,16 @@ private String getMimeTypeOfFile(MultipartFile file) {
return file.getContentType();
}

public static String getChecksumOfFile(MultipartFile file) {
String checksum = "";
try {
checksum = DigestUtils.md5DigestAsHex(file.getBytes());
} catch (IOException ioe) {
throw new BadRequestException(ioe);
}
return checksum;
}

private NodeService getNodeService(String projectId) {
return serviceFactory.getNodeService(getProjectType(projectId));
}
Expand Down
4 changes: 3 additions & 1 deletion example/artifacts.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
" pm.expect(jsonData.elements.length).to.eql(1);",
" pm.expect(jsonData.elements[0]['_artifacts'].length).to.eq(1);",
" pm.expect(jsonData.elements[0]['_artifacts'][0].location).to.include('arta/x/jpg/')",
" pm.expect(jsonData.elements[0]['_artifacts'][0].checksum).to.include('c946d2fc350ad561fdb3c23c86a81343')",
" pm.environment.set(\"commit-1-loc\", pm.response.json().elements[0]['_artifacts'][0].location);",
" pm.environment.set(\"x-commit-1\", jsonData.elements[0][\"_commitId\"]);",
"});",
Expand Down Expand Up @@ -310,7 +311,8 @@
"pm.test(\"response element has 1 artifact\", function () {",
" var jsonData = pm.response.json();",
" pm.expect(jsonData.elements[0]['_artifacts'].length).to.eq(1);",
"});"
" pm.expect(jsonData.elements[0]['_artifacts'][0].checksum).to.include('5407655262fcca873c2f407f2dead2cf')",
"});"
],
"type": "text/javascript"
}
Expand Down