All checks were successful
Deploy Production Server / if_merged (pull_request) Successful in 4m48s
196 lines
8.1 KiB
Java
196 lines
8.1 KiB
Java
package fr.titionfire.ffsaf.utils;
|
|
|
|
import io.smallrye.mutiny.Uni;
|
|
import jakarta.ws.rs.core.HttpHeaders;
|
|
import jakarta.ws.rs.core.MediaType;
|
|
import jakarta.ws.rs.core.Response;
|
|
import jodd.net.MimeTypes;
|
|
import org.jboss.logging.Logger;
|
|
|
|
import java.io.*;
|
|
import java.net.URISyntaxException;
|
|
import java.net.URLConnection;
|
|
import java.nio.file.Files;
|
|
import java.util.Arrays;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.Future;
|
|
|
|
|
|
public class Utils {
|
|
private static final Logger LOGGER = Logger.getLogger(Utils.class);
|
|
|
|
public static int getSaison() {
|
|
return getSaison(new Date());
|
|
}
|
|
|
|
public static int getSaison(Date date) {
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.setTime(date);
|
|
|
|
if (calendar.get(Calendar.MONTH) >= Calendar.SEPTEMBER) {
|
|
return calendar.get(Calendar.YEAR);
|
|
} else {
|
|
return calendar.get(Calendar.YEAR) - 1;
|
|
}
|
|
}
|
|
|
|
public static Uni<String> moveMedia(long idSrc, long idDest, String media, String dirSrc, String dirDst) {
|
|
return Uni.createFrom().nullItem().map(__ -> {
|
|
File dirFile = new File(media, dirSrc);
|
|
if (!dirFile.exists())
|
|
return "Not found";
|
|
|
|
File dirDestFile = new File(media, dirDst);
|
|
if (!dirDestFile.exists())
|
|
if (!dirDestFile.mkdirs())
|
|
return "Fail to create directory " + dirDestFile;
|
|
|
|
FilenameFilter filter = (directory, filename) -> filename.startsWith(idSrc + ".");
|
|
File[] files = dirFile.listFiles(filter);
|
|
if (files == null || files.length == 0)
|
|
return "Not found";
|
|
|
|
FilenameFilter filter2 = (directory, filename) -> filename.startsWith(idDest + ".");
|
|
File[] files2 = dirDestFile.listFiles(filter2);
|
|
if (files2 != null) {
|
|
for (File file : files2) {
|
|
//noinspection ResultOfMethodCallIgnored
|
|
file.delete();
|
|
}
|
|
}
|
|
|
|
for (File file : files) {
|
|
//noinspection ResultOfMethodCallIgnored
|
|
file.renameTo(new File(dirDestFile,
|
|
file.getName().replaceFirst(String.valueOf(idSrc), String.valueOf(idDest))));
|
|
}
|
|
|
|
return "Ok";
|
|
});
|
|
}
|
|
|
|
public static Future<String> replacePhoto(long id, byte[] input, String media, String dir) {
|
|
return CompletableFuture.supplyAsync(() -> {
|
|
if (input == null || input.length == 0)
|
|
return "OK";
|
|
|
|
try (InputStream is = new BufferedInputStream(new ByteArrayInputStream(input))) {
|
|
String mimeType = URLConnection.guessContentTypeFromStream(is);
|
|
System.out.println(mimeType);
|
|
/*try {
|
|
mimeType = Magic.getMagicMatch(input, false).getMimeType();
|
|
} catch (MagicParseException | MagicMatchNotFoundException | MagicException e) {
|
|
mimeType = URLConnection.guessContentTypeFromStream(is);
|
|
}*/
|
|
String[] detectedExtensions = MimeTypes.findExtensionsByMimeTypes(mimeType, false);
|
|
System.out.println(Arrays.toString(detectedExtensions));
|
|
if (detectedExtensions.length == 0)
|
|
throw new IOException("Fail to detect file extension for MIME type " + mimeType);
|
|
|
|
File dirFile = new File(media, dir);
|
|
if (!dirFile.exists())
|
|
if (!dirFile.mkdirs())
|
|
throw new IOException("Fail to create directory " + dir);
|
|
|
|
FilenameFilter filter = (directory, filename) -> filename.startsWith(id + ".");
|
|
File[] files = dirFile.listFiles(filter);
|
|
if (files != null) {
|
|
for (File file : files) {
|
|
//noinspection ResultOfMethodCallIgnored
|
|
file.delete();
|
|
}
|
|
}
|
|
|
|
String extension = "." + detectedExtensions[0];
|
|
Files.write(new File(dirFile, id + extension).toPath(), input);
|
|
return "OK";
|
|
} catch (IOException e) {
|
|
return e.getMessage();
|
|
}
|
|
});
|
|
}
|
|
|
|
public static Uni<Response> getMediaFile(long id, String media, String dirname,
|
|
Uni<?> uniBase) throws URISyntaxException {
|
|
return getMediaFile(id, media, dirname, null, uniBase);
|
|
}
|
|
|
|
public static Uni<Response> getMediaFile(long id, String media, String dirname, String out_filename,
|
|
Uni<?> uniBase) throws URISyntaxException {
|
|
Future<Pair<File, byte[]>> future = CompletableFuture.supplyAsync(() -> {
|
|
FilenameFilter filter = (directory, filename) -> filename.startsWith(id + ".");
|
|
File[] files = new File(media, dirname).listFiles(filter);
|
|
if (files != null && files.length > 0) {
|
|
File file = files[0];
|
|
try {
|
|
byte[] data = Files.readAllBytes(file.toPath());
|
|
return new Pair<>(file, data);
|
|
} catch (IOException ignored) {
|
|
}
|
|
}
|
|
return null;
|
|
});
|
|
|
|
Future<byte[]> future2 = CompletableFuture.supplyAsync(() -> {
|
|
try (InputStream st = Utils.class.getClassLoader().getResourceAsStream("asset/blank-profile-picture.png")) {
|
|
if (st == null)
|
|
return null;
|
|
return st.readAllBytes();
|
|
} catch (IOException ignored) {
|
|
}
|
|
return null;
|
|
});
|
|
|
|
return uniBase.chain(__ -> Uni.createFrom().future(future)
|
|
.chain(filePair -> {
|
|
if (filePair == null) {
|
|
return Uni.createFrom().future(future2).map(data -> {
|
|
if (data == null)
|
|
return Response.noContent().build();
|
|
|
|
String mimeType = "image/apng";
|
|
Response.ResponseBuilder resp = Response.ok(data);
|
|
resp.type(MediaType.APPLICATION_OCTET_STREAM);
|
|
resp.header(HttpHeaders.CONTENT_LENGTH, data.length);
|
|
resp.header(HttpHeaders.CONTENT_TYPE, mimeType);
|
|
resp.header(HttpHeaders.CONTENT_DISPOSITION,
|
|
"inline; " + ((out_filename == null) ? "" : "filename=\"" + out_filename + "\""));
|
|
return resp.build();
|
|
});
|
|
} else {
|
|
return Uni.createFrom().item(() -> {
|
|
String mimeType = URLConnection.guessContentTypeFromName(filePair.getKey().getName());
|
|
|
|
Response.ResponseBuilder resp = Response.ok(filePair.getValue());
|
|
resp.type(MediaType.APPLICATION_OCTET_STREAM);
|
|
resp.header(HttpHeaders.CONTENT_LENGTH, filePair.getValue().length);
|
|
resp.header(HttpHeaders.CONTENT_TYPE, mimeType);
|
|
resp.header(HttpHeaders.CONTENT_DISPOSITION,
|
|
"inline; " + ((out_filename == null) ? "" : "filename=\"" + out_filename + "\""));
|
|
return resp.build();
|
|
});
|
|
}
|
|
}));
|
|
}
|
|
|
|
public static Uni<?> deleteMedia(long id, String media, String dir) {
|
|
return Uni.createFrom().nullItem().map(__ -> {
|
|
File dirFile = new File(media, dir);
|
|
if (!dirFile.exists())
|
|
return "OK";
|
|
|
|
FilenameFilter filter = (directory, filename) -> filename.startsWith(id + ".");
|
|
File[] files = dirFile.listFiles(filter);
|
|
if (files != null) {
|
|
for (File file : files) {
|
|
//noinspection ResultOfMethodCallIgnored
|
|
file.delete();
|
|
}
|
|
}
|
|
return "Ok";
|
|
});
|
|
}
|
|
}
|