fix: mimetype to extension convert
This commit is contained in:
parent
27cf18de97
commit
f604e64966
6
pom.xml
6
pom.xml
@ -89,9 +89,9 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.tika</groupId>
|
<groupId>org.jodd</groupId>
|
||||||
<artifactId>tika-core</artifactId>
|
<artifactId>jodd-util</artifactId>
|
||||||
<version>3.0.0-BETA</version>
|
<version>6.2.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@ -21,6 +21,7 @@ RUN chown 1001 /work \
|
|||||||
&& chown 1001:root /work
|
&& chown 1001:root /work
|
||||||
COPY --chown=1001:root ffsaf/target/*-runner /work/application
|
COPY --chown=1001:root ffsaf/target/*-runner /work/application
|
||||||
COPY --chown=1001:root ffsaf/src/main/resources/cacerts /work/cacerts
|
COPY --chown=1001:root ffsaf/src/main/resources/cacerts /work/cacerts
|
||||||
|
RUN mkdir /work/media && chown -R 1001:root /work/media
|
||||||
|
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
USER 1001
|
USER 1001
|
||||||
|
|||||||
@ -12,17 +12,13 @@ import jakarta.ws.rs.*;
|
|||||||
import jakarta.ws.rs.core.HttpHeaders;
|
import jakarta.ws.rs.core.HttpHeaders;
|
||||||
import jakarta.ws.rs.core.MediaType;
|
import jakarta.ws.rs.core.MediaType;
|
||||||
import jakarta.ws.rs.core.Response;
|
import jakarta.ws.rs.core.Response;
|
||||||
import org.apache.commons.io.FileUtils;
|
import jodd.net.MimeTypes;
|
||||||
import org.apache.tika.Tika;
|
|
||||||
import org.apache.tika.mime.MimeTypeException;
|
|
||||||
import org.apache.tika.mime.MimeTypes;
|
|
||||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.FilenameFilter;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.URLConnection;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
@ -60,12 +56,25 @@ public class CombEndpoints {
|
|||||||
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
||||||
public Uni<String> setAdminMembre(@PathParam("id") long id, FullMemberForm input) {
|
public Uni<String> setAdminMembre(@PathParam("id") long id, FullMemberForm input) {
|
||||||
Future<String> future = CompletableFuture.supplyAsync(() -> {
|
Future<String> future = CompletableFuture.supplyAsync(() -> {
|
||||||
try{
|
try (InputStream is = new BufferedInputStream(new ByteArrayInputStream(input.getPhoto_data()))) {
|
||||||
String mimeType = new Tika().detect(input.getPhoto_data());
|
String mimeType = URLConnection.guessContentTypeFromStream(is);
|
||||||
String extension = MimeTypes.getDefaultMimeTypes().forName(mimeType).getExtension();
|
String[] detectedExtensions = MimeTypes.findExtensionsByMimeTypes(mimeType, false);
|
||||||
FileUtils.writeByteArrayToFile(new File(media, "ppMembre/" + input.getId() + extension), input.getPhoto_data());
|
if (detectedExtensions.length == 0)
|
||||||
|
throw new IOException("Fail to detect file extension for MIME type " + mimeType);
|
||||||
|
|
||||||
|
FilenameFilter filter = (directory, filename) -> filename.startsWith(String.valueOf(id));
|
||||||
|
File[] files = new File(media, "ppMembre").listFiles(filter);
|
||||||
|
if (files != null) {
|
||||||
|
for (File file : files) {
|
||||||
|
//noinspection ResultOfMethodCallIgnored
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String extension = "." + detectedExtensions[0];
|
||||||
|
Files.write(new File(media, "ppMembre/" + input.getId() + extension).toPath(), input.getPhoto_data());
|
||||||
return "OK";
|
return "OK";
|
||||||
} catch (IOException | MimeTypeException e) {
|
} catch (IOException e) {
|
||||||
return e.getMessage();
|
return e.getMessage();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -112,7 +121,7 @@ public class CombEndpoints {
|
|||||||
if (filePair == null)
|
if (filePair == null)
|
||||||
return Response.temporaryRedirect(uri).build();
|
return Response.temporaryRedirect(uri).build();
|
||||||
|
|
||||||
String mimeType = new Tika().detect(filePair.getKey().getName());
|
String mimeType = URLConnection.guessContentTypeFromName(filePair.getKey().getName());
|
||||||
|
|
||||||
Response.ResponseBuilder resp = Response.ok(filePair.getValue());
|
Response.ResponseBuilder resp = Response.ok(filePair.getValue());
|
||||||
resp.type(MediaType.APPLICATION_OCTET_STREAM);
|
resp.type(MediaType.APPLICATION_OCTET_STREAM);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user