Completely Working Version, still possible improvements

This commit is contained in:
RobinFleige
2018-07-30 11:43:40 +02:00
parent cb6d587861
commit ddd8fcbbbc
5 changed files with 34 additions and 22 deletions
@@ -281,8 +281,6 @@ public abstract class ImageDialog extends DataEntryDialog<ImageDTO> {
for(int i= 0; i < doResize.size(); i++) {
if(tags.get(i).equals(MediaTagConstants.LOGO) || tags.get(i).equals(MediaTagConstants.TEASER) || tags.get(i).equals(MediaTagConstants.STAGE) || tags.get(i).equals(MediaTagConstants.GALLERY)) {
map.put(tags.get(i), doResize.get(i).getValue());
}else {
map.put(tags.get(i),null);
}
}
ImageDTO result = new ToResizeImageDTO(imageURLAndUploadComposite.getURL(), creationDate, map);
@@ -293,7 +291,7 @@ public abstract class ImageDialog extends DataEntryDialog<ImageDTO> {
result.setSizeInPx(widthInPxBox.getValue(), heightInPxBox.getValue());
}
result.setTags(tags);
return result;
return (ImageDTO)result;
}
@Override
@@ -350,9 +350,14 @@ public class ImagesListComposite extends Composite {
obj.put("Width", new JSONNumber(image.getWidthInPx()));
obj.put("Date", new JSONNumber(image.getCreatedAtDate().getTime()));
obj.put("Copyright", new JSONString(image.getCopyright()));
JSONObject tags = new JSONObject();
for(String tag : image.getTags()) {
tags.put(tag, JSONBoolean.getInstance(image.resizeForTag(tag)));
JSONObject doResizeMap = new JSONObject();
for(String doResize : image.getMap().keySet()) {
doResizeMap.put(doResize, JSONBoolean.getInstance(image.resizeForTag(doResize)));
}
obj.put("ResizeMap", doResizeMap);
JSONArray tags = new JSONArray();
for(int i = 0; i < image.getTags().size(); i++) {
tags.set(i, new JSONString(image.getTags().get(i)));
}
obj.put("Tags", tags);
return obj.toString();
@@ -373,9 +378,9 @@ public class ImagesListComposite extends Composite {
for(int i = 0; i < images.size(); i++) {
ImageDTO image = new ImageDTO(images.get(i).isObject().get("URI").isString().stringValue(),new Date(Long.valueOf(images.get(i).isObject().get("Date").isNumber().toString())));
List<String> tags = new ArrayList<>();
JSONObject jsonTags = images.get(i).isObject().get("Tags").isObject();
for(String key : jsonTags.keySet()) {
tags.add(key);
JSONArray jsonTags = images.get(i).isObject().get("Tags").isArray();
for(int j = 0; j < jsonTags.size(); j++) {
tags.add(jsonTags.get(j).isString().stringValue());
}
image.setTags(tags);
image.setTitle(images.get(i).isObject().get("Title").isString().stringValue());
@@ -38,17 +38,18 @@ public class ImageResizingServlet extends AbstractJsonHttpServlet {
JSONArray toReturnArray = new JSONArray();
if(obj != null) {
String fileType = ((String)obj.get("URI")).substring(((String)obj.get("URI")).lastIndexOf(".")+1);
JSONObject tags = (JSONObject) obj.get("Tags");
JSONArray tags = (JSONArray) obj.get("Tags");
JSONObject resizeMap = (JSONObject) obj.get("ResizeMap");
InputStream is = getInputStreamFromURIString(((String)obj.get("URI")));
BufferedImage img = ImageConverter.isToBi(is);
is.close();
for(Object tagKey : tags.keySet()) {
if((boolean) tags.get(tagKey)) {
for(Object tagKey : resizeMap.keySet()) {
if((boolean) resizeMap.get(tagKey)) {
resizeTags.add((String)tagKey);//size tags, that have the resize checkBox checked
}else if(tags.get(tagKey) != null){
}else{
notResizeSizeTags.add((String)tagKey);//size tags, that not have the resize checkBox checked
}//else all non-size tags
}
}
for(String resizeTag : resizeTags) {
@@ -58,10 +59,10 @@ public class ImageResizingServlet extends AbstractJsonHttpServlet {
for(String toDeleteTag : notResizeSizeTags) {//delete all size tags
tags.remove(toDeleteTag);
}
tags.put(resizeTag, "Done");//read the deleted specific size tag
tags.add(resizeTag);//read the deleted specific size tag
resizeAndAddToAr(img, toReturnArray, obj, resizeTag, fileType);
obj = getObjFromJSON(jsonString);//reset the object
tags = (JSONObject) obj.get("Tags");//and the tags
tags = (JSONArray) obj.get("Tags");//and the tags
}
if(notResizeSizeTags.isEmpty()) {//if there is no size tag that does not need a resize we can delete the original source
try {
@@ -78,10 +79,10 @@ public class ImageResizingServlet extends AbstractJsonHttpServlet {
for(String toDeleteTag : notResizeSizeTags) {//delete all size tags
tags.remove(toDeleteTag);
}
tags.put(resizeTag, "Done");//re-add the deleted specific size tag
tags.add(resizeTag);//re-add the deleted specific size tag
toReturnArray.add(obj);
obj = getObjFromJSON(jsonString);//reset the object for next size-tag-iteration
tags = (JSONObject) obj.get("Tags");//and the tags
tags = (JSONArray) obj.get("Tags");//and the tags
}
}
@@ -14,7 +14,6 @@ import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.CopyObjectRequest;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
@@ -56,7 +55,7 @@ public class AmazonS3FileStorageServiceImpl extends BaseFileStorageServiceImpl i
private AmazonS3Client createS3Client() throws InvalidPropertiesException {
AWSCredentials creds;
// first try to use properties
if (accessId.getValue() != null && accessKey.getValue() != null) {
creds = new BasicAWSCredentials(accessId.getValue(), accessKey.getValue());
@@ -111,7 +110,7 @@ public class AmazonS3FileStorageServiceImpl extends BaseFileStorageServiceImpl i
@Override
public void removeFile(URI uri) throws InvalidPropertiesException, OperationFailedException {
String key = uri.getPath().substring(2+bucketName.getValue().length()); // remove 2 slashs and bucketName
String key = uri.getPath().substring(uri.getPath().lastIndexOf("/")+1);
AmazonS3Client s3Client = createS3Client();
try {
s3Client.deleteObject(new DeleteObjectRequest(bucketName.getValue(), key));
@@ -125,8 +124,13 @@ public class AmazonS3FileStorageServiceImpl extends BaseFileStorageServiceImpl i
public void testProperties() throws InvalidPropertiesException {
AmazonS3Client s3 = createS3Client();
if(bucketName.getValue().equals("")) {
throw new InvalidPropertiesException("empty bucketname is not allowed");
}
// test if credentials are valid
// TODO seems to even work if credentials are not valid if bucket is publicly visible
// Fix is probably available with doesBucketExistsV2 in a later version of amazons3 library
try {
s3.doesBucketExist(bucketName.getValue());
} catch (Exception e) {
@@ -144,7 +148,7 @@ public class AmazonS3FileStorageServiceImpl extends BaseFileStorageServiceImpl i
@Override
public InputStream loadFile(URI uri) throws OperationFailedException, InvalidPropertiesException, IOException {
String key = uri.getPath().substring(2+bucketName.getValue().length());
String key = uri.getPath().substring(uri.getPath().lastIndexOf("/")+1);
AmazonS3Client s3Client = createS3Client();
return s3Client.getObject(bucketName.getValue(), key).getObjectContent();
}
@@ -18,4 +18,8 @@ public class ToResizeImageDTO extends ImageDTO {
public boolean resizeForTag(String tag) {
return toResizeMap.get(tag);
}
public Map<String, Boolean> getMap(){
return toResizeMap;
}
}