Skip to content
Snippets Groups Projects
Unverified Commit 5a65ba2b authored by Tobias Reiter's avatar Tobias Reiter Committed by GitHub
Browse files

feat: changed file controller #41 (#42)

parent 2a9f30c1
No related branches found
No related tags found
No related merge requests found
package com.quiz.converter.controller; package com.quiz.converter.controller;
import com.quiz.converter.models.FileDto;
import com.quiz.converter.models.QuizDetails;
import com.quiz.converter.services.ConverterService; import com.quiz.converter.services.ConverterService;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.math3.util.Pair;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity; import org.springframework.http.*;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -17,6 +16,7 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -17,6 +16,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
@Controller @Controller
@RequestMapping(value = "/file/convert") @RequestMapping(value = "/file/convert")
...@@ -25,39 +25,27 @@ public class FileController { ...@@ -25,39 +25,27 @@ public class FileController {
@Autowired @Autowired
ConverterService service; ConverterService service;
@PostMapping(value = "/moodleXML") @PostMapping("/moodleXML")
public ResponseEntity<MultiValueMap<String, HttpEntity<?>>> convertDocFileToMoodleXML(@RequestBody MultipartFile file) throws IOException, ParserConfigurationException, TransformerException { public ResponseEntity<FileDto> convertDocFileToMoodleXML(@RequestBody MultipartFile file) throws IOException, ParserConfigurationException, TransformerException {
var convertedFileName = file.getOriginalFilename().replace(".docx", "") + "_moodle.xml"; var conversionResult = service.convertDocToMoodle(file);
var attachments = service.convertDocToMoodle(file); var fileContent = conversionResult.getFirst();
var details = conversionResult.getSecond();
var builder = new MultipartBodyBuilder(); var base64EncodedFile = Base64.encodeBase64String(fileContent);
builder.part("details", attachments.getSecond())
.contentType(MediaType.APPLICATION_JSON); FileDto response = new FileDto(Objects.requireNonNull(file.getOriginalFilename()).replace(".docx", "") + "_moodle.xml", base64EncodedFile, details);
builder.part("file", attachments.getFirst())
.contentType(MediaType.APPLICATION_XML) return ResponseEntity.ok(response);
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + convertedFileName);
var body = builder.build();
return ResponseEntity.ok()
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(body);
} }
@PostMapping(value = "/courseraDocx") @PostMapping("/courseraDocx")
public ResponseEntity<MultiValueMap<String, HttpEntity<?>>> convertDocFileToCourseraDocx(@RequestBody MultipartFile file) throws IOException { public ResponseEntity<FileDto> convertDocFileToCourseraDocx(@RequestBody MultipartFile file) throws IOException {
var convertedFileName = file.getOriginalFilename().replace(".docx", "") + "_coursera.docx"; var conversionResult = service.convertDocToCoursera(file);
var attachments = service.convertDocToCoursera(file); var fileContent = conversionResult.getFirst();
var details = conversionResult.getSecond();
var builder = new MultipartBodyBuilder(); var base64EncodedFile = Base64.encodeBase64String(fileContent);
builder.part("details", attachments.getSecond())
.contentType(MediaType.APPLICATION_JSON); FileDto response = new FileDto(Objects.requireNonNull(file.getOriginalFilename()).replace(".docx", "") + "_coursera.docx", base64EncodedFile, details);
builder.part("file", attachments.getFirst())
.contentType(MediaType.valueOf("application/vnd.ms-word")) return ResponseEntity.ok(response);
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + convertedFileName);
var body = builder.build();
return ResponseEntity.ok()
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(body);
} }
} }
\ No newline at end of file
package com.quiz.converter.models;
public record FileDto(String fileName, String file, QuizDetails details) {
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment