Skip to content
Snippets Groups Projects
Commit 0e6cb407 authored by Jan Sladek's avatar Jan Sladek Committed by Björn
Browse files

Worked on #15326. Implemented controller, configurable properties...

Worked on #15326. Implemented controller, configurable properties imgupload.thumbnail.{width, heigth}, stared on working on communication between client and server.
parent 84a2d1f9
Branches
Tags
No related merge requests found
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -33,6 +33,9 @@ import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import de.thm.arsnova.entities.Answer;
/**
* Util class for image operations.
......@@ -51,7 +54,21 @@ public class ImageUtils {
*/
public static final Pattern BASE64_IMAGE_PREFIX_PATTERN = Pattern.compile("data:image/(.*);base64,(.*)");
/* default value is 200 pixel in width, set the value in the configuration file */
private static int THUMB_WIDTH = 200;
/* default value is 200 pixel in height, set the value in the configuration file */
private static int THUMB_HEIGHT = 200;
@Value(value = "{imageupload.thumbnail.width}")
public void setThumbWidth(int thumbWidth) {
ImageUtils.THUMB_WIDTH = thumbWidth;
}
@Value(value = "{imageupload.thumbnail.height}")
public void setThumbHeight(int thumbHeight) {
ImageUtils.THUMB_HEIGHT = thumbHeight;
}
private ImageUtils() {
}
......@@ -113,7 +130,7 @@ public class ImageUtils {
* the new height
* @return The rescaled Image as Base64-encoded {@link String}
*/
public static String rescaleImage(String originalImageString, final int width, final int height) {
public static String createCover(String originalImageString, final int width, final int height) {
if (!isBase64EncodedImage(originalImageString)) return null;
else {
Matcher imageMatcher = BASE64_IMAGE_PREFIX_PATTERN.matcher(originalImageString);
......@@ -165,6 +182,13 @@ public class ImageUtils {
}
}
}
public static void generateThumbnailImage(Answer answer) {
if (!isBase64EncodedImage(answer.getAnswerThumbnailImage())) {
final String thumbImage = createCover(answer.getAnswerImage(), THUMB_WIDTH, THUMB_HEIGHT);
answer.setAnswerThumbnailImage(thumbImage);
}
}
/**
* Gets the bytestream of an image url.
......
......@@ -37,6 +37,8 @@ import org.springframework.web.bind.annotation.RestController;
import de.thm.arsnova.entities.Answer;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.transport.ThumbnailRequest;
import de.thm.arsnova.entities.transport.ThumbnailResponse;
import de.thm.arsnova.exceptions.BadRequestException;
import de.thm.arsnova.exceptions.ForbiddenException;
import de.thm.arsnova.exceptions.NoContentException;
......@@ -338,6 +340,24 @@ public class LecturerQuestionController extends AbstractController {
) {
return questionService.updateAnswer(answer);
}
@RequestMapping(value = "/{questionId}/thumbs", method = RequestMethod.POST)
public ThumbnailResponse getThumbnails(
@PathVariable final String questionId,
@RequestBody final ThumbnailRequest thumbRequest
) {
return questionService.getThumbnails(questionId, thumbRequest);
}
@RequestMapping(value = "/{questionId}/answer/{answerId}/image", method = RequestMethod.GET)
public String getImage(
@PathVariable final String questionId,
@PathVariable final String answerId
) {
return questionService.getImage(questionId, answerId);
}
@RequestMapping(value = "/{questionId}/answer/{answerId}", method = RequestMethod.DELETE)
public void deleteAnswer(
......
......@@ -36,7 +36,9 @@ public class Answer {
private int answerCount = 1;
private boolean abstention;
private int abstentionCount;
@JsonIgnore
private String answerImage;
@JsonIgnore
private String answerThumbnailImage;
public Answer() {
......@@ -115,6 +117,7 @@ public class Answer {
return user;
}
@JsonIgnore
public String getAnswerImage() {
return answerImage;
}
......@@ -123,6 +126,7 @@ public class Answer {
this.answerImage = answerImage;
}
@JsonIgnore
public String getAnswerThumbnailImage() {
return answerThumbnailImage;
}
......
package de.thm.arsnova.entities.transport;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class ThumbnailRequest {
private String questionId;
private List<String> answerIds;
public String getQuestionId() {
return questionId;
}
public List<String> getAnswerIds() {
return answerIds;
}
public void setQuestionId(String questionId) {
this.questionId = questionId;
}
public void setAnswerIds(List<String> answerIds) {
this.answerIds = answerIds;
}
}
package de.thm.arsnova.entities.transport;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class ThumbnailResponse {
private String questionId;
private List<ThumbnailResponseEntry> entries;
public String getQuestionId() {
return questionId;
}
public void setQuestionId(String questionId) {
this.questionId = questionId;
}
public List<ThumbnailResponseEntry> getEntries() {
return entries;
}
@JsonIgnore
public void addThumbnailEntry(String answerId, String answerThumbnailImage) {
if (entries == null) {
entries = new ArrayList<>();
}
entries.add(new ThumbnailResponseEntry(answerId, answerThumbnailImage));
}
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
static class ThumbnailResponseEntry {
private String answerId;
private String answerThumbnailImage;
public ThumbnailResponseEntry(String answerId, String answerThumbnailImage) {
this.answerId = answerId;
this.answerThumbnailImage = answerThumbnailImage;
}
public String getAnswerId() {
return answerId;
}
public String getAnswerThumbnailImage() {
return answerThumbnailImage;
}
public void setAnswerId(String answerId) {
this.answerId = answerId;
}
public void setAnswerThumbnailImage(String answerThumbnailImage) {
this.answerThumbnailImage = answerThumbnailImage;
}
}
}
......@@ -25,6 +25,8 @@ import de.thm.arsnova.entities.InterposedQuestion;
import de.thm.arsnova.entities.InterposedReadingCount;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.entities.transport.ThumbnailRequest;
import de.thm.arsnova.entities.transport.ThumbnailResponse;
public interface IQuestionService {
Question saveQuestion(Question question);
......@@ -136,4 +138,8 @@ public interface IQuestionService {
void deleteAllLectureAnswers(String sessionkey);
int getAbstentionAnswerCount(String questionId);
ThumbnailResponse getThumbnails(String questionId, ThumbnailRequest thumbRequest);
String getImage(String questionId, String answerId);
}
......@@ -25,6 +25,7 @@ import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
......@@ -45,6 +46,8 @@ import de.thm.arsnova.entities.InterposedReadingCount;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.entities.transport.ThumbnailRequest;
import de.thm.arsnova.entities.transport.ThumbnailResponse;
import de.thm.arsnova.events.DeleteAllLectureAnswersEvent;
import de.thm.arsnova.events.DeleteAllPreparationAnswersEvent;
import de.thm.arsnova.events.DeleteAllQuestionsAnswersEvent;
......@@ -782,4 +785,42 @@ public class QuestionService implements IQuestionService, ApplicationEventPublis
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
@Override
public ThumbnailResponse getThumbnails(String questionId, ThumbnailRequest thumbRequest) {
final ThumbnailResponse response = new ThumbnailResponse();
response.setQuestionId(thumbRequest.getQuestionId());
final List<Answer> answers = getAnswers(questionId);
outer: for (String answerId : thumbRequest.getAnswerIds()) {
for (Answer answer : answers) {
if (answerId != null && answerId.equals(answer.get_id())) {
response.addThumbnailEntry(answerId, answer.getAnswerThumbnailImage());
break outer;
}
}
}
return response;
}
@Override
public String getImage(String questionId, String answerId) {
final List<Answer> answers = getAnswers(questionId);
Answer answer = null;
for (Answer a : answers) {
if (answerId.equals(a.get_id())) {
answer = a;
break;
}
}
if (answer == null) {
throw new NotFoundException();
}
return answer.getAnswerImage();
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment