Skip to content

Commit

Permalink
feat: add support for more doc types
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Nov 15, 2023
1 parent 587b685 commit 8026215
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
*/
package ch.xxx.aidoclibchat.domain.common;

public enum DocumentType { PDF, UNKNOWN }
public enum DocumentType { PDF, HTML, TEXT, XML, UNKNOWN }
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.IOException;
import java.util.Optional;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -31,9 +32,17 @@ public Document toEntity(MultipartFile multipartFile) {
try {
entity.setDocumentContent(multipartFile.getBytes());
entity.setDocumentName(multipartFile.getOriginalFilename());
entity.setDocumentType(Optional.ofNullable(multipartFile.getContentType()).stream()
.filter(myContentType -> myContentType.contains("pdf")).map(x -> DocumentType.PDF).findFirst()
.orElse(DocumentType.UNKNOWN));
entity.setDocumentType(Optional.ofNullable(multipartFile.getContentType()).stream().map(myContentType -> {
var result = switch (myContentType) {
case MediaType.APPLICATION_PDF_VALUE -> DocumentType.PDF;
case MediaType.TEXT_HTML_VALUE -> DocumentType.HTML;
case MediaType.TEXT_PLAIN_VALUE -> DocumentType.TEXT;
case MediaType.APPLICATION_XML_VALUE -> DocumentType.XML;
case MediaType.TEXT_XML_VALUE -> DocumentType.XML;
default -> DocumentType.UNKNOWN;
};
return result;
}).findFirst().orElse(DocumentType.UNKNOWN));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 8026215

Please sign in to comment.