feat: pdf generation
This commit is contained in:
@@ -1,165 +0,0 @@
|
||||
package fr.titionfire.ffsaf.ws;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.websocket.*;
|
||||
import jakarta.websocket.server.PathParam;
|
||||
import jakarta.websocket.server.ServerEndpoint;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ServerEndpoint("/api/ws/file/{code}")
|
||||
@ApplicationScoped
|
||||
public class FileSocket {
|
||||
private static final Logger logger = Logger.getLogger(FileSocket.class);
|
||||
public static Map<String, FileRecv> sessions = new ConcurrentHashMap<>();
|
||||
|
||||
@ConfigProperty(name = "upload_dir")
|
||||
String media;
|
||||
|
||||
/*@Scheduled(every = "10s")
|
||||
void increment() {
|
||||
sessions.forEach((key, value) -> {
|
||||
if (System.currentTimeMillis() - value.time > 60000) {
|
||||
closeAndDelete(value);
|
||||
if (value.session != null && value.session.isOpen()) {
|
||||
try {
|
||||
value.session.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY, "Timeout"));
|
||||
} catch (IOException e) {
|
||||
StringWriter errors = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(errors));
|
||||
logger.error(errors.toString());
|
||||
}
|
||||
}
|
||||
sessions.remove(key);
|
||||
}
|
||||
});
|
||||
}*/
|
||||
|
||||
@OnOpen
|
||||
public void onOpen(Session session, @PathParam("code") String code) {
|
||||
try {
|
||||
if (sessions.containsKey(code)) {
|
||||
FileRecv fileRecv = sessions.get(code);
|
||||
fileRecv.session = session;
|
||||
fileRecv.file = new File(media + "-ext", "record/" + fileRecv.name);
|
||||
fileRecv.fos = new FileOutputStream(fileRecv.file, false);
|
||||
logger.info("Start reception of file: " + fileRecv.file.getAbsolutePath());
|
||||
} else {
|
||||
session.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY, "File not found"));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
StringWriter errors = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(errors));
|
||||
logger.error(errors.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@OnClose
|
||||
public void onClose(Session session, @PathParam("code") String code) {
|
||||
if (sessions.containsKey(code)) {
|
||||
FileRecv fileRecv = sessions.get(code);
|
||||
if (fileRecv.fos != null) {
|
||||
try {
|
||||
fileRecv.fos.close();
|
||||
} catch (IOException e) {
|
||||
StringWriter errors = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(errors));
|
||||
logger.error(errors.toString());
|
||||
}
|
||||
}
|
||||
logger.info("File received: " + fileRecv.file.getAbsolutePath());
|
||||
sessions.remove(code);
|
||||
}
|
||||
}
|
||||
|
||||
@OnError
|
||||
public void onError(Session session, @PathParam("code") String code, Throwable throwable) {
|
||||
if (sessions.containsKey(code)) {
|
||||
closeAndDelete(sessions.get(code));
|
||||
sessions.remove(code);
|
||||
}
|
||||
logger.error("Error on file reception: " + throwable.getMessage());
|
||||
}
|
||||
|
||||
|
||||
@OnMessage
|
||||
public void onMessage(String message, @PathParam("code") String code) {
|
||||
if (message.equals("cancel")) {
|
||||
if (sessions.containsKey(code)) {
|
||||
closeAndDelete(sessions.get(code));
|
||||
sessions.remove(code);
|
||||
}
|
||||
logger.error("Error file " + code + " are cancel by the client");
|
||||
}
|
||||
}
|
||||
|
||||
private void closeAndDelete(FileRecv fileRecv) {
|
||||
if (fileRecv.fos != null) {
|
||||
try {
|
||||
fileRecv.fos.close();
|
||||
} catch (IOException e) {
|
||||
StringWriter errors = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(errors));
|
||||
logger.error(errors.toString());
|
||||
}
|
||||
}
|
||||
if (fileRecv.file.exists()) {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
fileRecv.file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
@OnMessage
|
||||
public void onMessage(byte[] data, @PathParam("code") String code) {
|
||||
int length = (data[1] << 7) | data[2];
|
||||
|
||||
byte check_sum = 0;
|
||||
for (int j = 3; j < length + 3; j++) {
|
||||
check_sum = (byte) (check_sum ^ data[j]);
|
||||
}
|
||||
// System.out.println(length + " - " + data[1] + " - " + data[0] + " - " + check_sum);
|
||||
|
||||
if (sessions.containsKey(code)) {
|
||||
FileRecv fileRecv = sessions.get(code);
|
||||
|
||||
if (check_sum != data[0]) {
|
||||
fileRecv.session.getAsyncRemote().sendText("Error: Checksum error", result -> {
|
||||
if (result.getException() != null) {
|
||||
logger.error("Unable to send message: " + result.getException());
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
fileRecv.fos.write(data, 3, length);
|
||||
} catch (IOException e) {
|
||||
StringWriter errors = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(errors));
|
||||
logger.error(errors.toString());
|
||||
}
|
||||
|
||||
fileRecv.session.getAsyncRemote().sendText("ok", result -> {
|
||||
if (result.getException() != null) {
|
||||
logger.error("Unable to send message: " + result.getException());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
@RegisterForReflection
|
||||
public static class FileRecv {
|
||||
Session session;
|
||||
String name;
|
||||
File file;
|
||||
FileOutputStream fos;
|
||||
long time;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user