-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from CaritasDeutschland/feat-mime-time-detecti…
…on-of-uploaded-files feat: detect mime type of uploaded file
- Loading branch information
Showing
12 changed files
with
367 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/de/caritas/cob/uploadservice/api/service/FileService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package de.caritas.cob.uploadservice.api.service; | ||
|
||
import static java.util.Objects.isNull; | ||
|
||
import de.caritas.cob.uploadservice.api.exception.InvalidFileTypeException; | ||
import de.caritas.cob.uploadservice.api.exception.httpresponses.InternalServerErrorException; | ||
import de.caritas.cob.uploadservice.media.MimeTypeDetector; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class FileService { | ||
|
||
private final MimeTypeDetector mimeTypeDetector; | ||
@Value("#{${mime-type-whitelist}}") | ||
private final Set<String> mimeTypeWhitelist; | ||
|
||
public void verifyMimeType(MultipartFile multipartFile) { | ||
if (isNull(multipartFile)) { | ||
return; | ||
} | ||
|
||
Optional<String> mimeType; | ||
try { | ||
mimeType = mimeTypeDetector.detect(multipartFile.getInputStream()); | ||
} catch (Exception e) { | ||
throw new InternalServerErrorException( | ||
"failed to detect mime type of file " + multipartFile.getOriginalFilename(), e, | ||
LogService::logInternalServerError); | ||
} | ||
if (mimeType.isEmpty() || !mimeTypeWhitelist.contains(mimeType.get())) { | ||
throw new InvalidFileTypeException( | ||
"invalid mime type " + mimeType + " of file " + multipartFile.getOriginalFilename()); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/de/caritas/cob/uploadservice/media/MimeTypeDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package de.caritas.cob.uploadservice.media; | ||
|
||
import java.io.InputStream; | ||
import java.util.Optional; | ||
|
||
public interface MimeTypeDetector { | ||
|
||
Optional<String> detect(InputStream input); | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/de/caritas/cob/uploadservice/media/TikaMimeTypeDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package de.caritas.cob.uploadservice.media; | ||
|
||
import static java.util.Objects.isNull; | ||
|
||
import java.io.InputStream; | ||
import java.util.Optional; | ||
import org.apache.tika.Tika; | ||
|
||
public class TikaMimeTypeDetector implements MimeTypeDetector { | ||
|
||
private final Tika tika; | ||
|
||
public TikaMimeTypeDetector(Tika tika) { | ||
this.tika = tika; | ||
} | ||
|
||
@Override | ||
public Optional<String> detect(InputStream input) { | ||
if (isNull(input)) { | ||
return Optional.empty(); | ||
} | ||
try { | ||
return Optional.ofNullable(tika.detect(input)); | ||
} catch (Exception e) { | ||
throw new RuntimeException("failed to detect media type from input stream", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.