let the server access contact sheets by @Jafea7

This commit is contained in:
reusedname 2025-02-21 17:53:23 +05:00
parent a72ca1e677
commit 04f8a74cd8
1 changed files with 22 additions and 0 deletions

View File

@ -7,6 +7,9 @@ import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.nio.file.Files;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Optional;
@ -24,6 +27,7 @@ public class ImageServlet extends AbstractCtbrecServlet {
public static final String BASE_URL = "/image";
private static final Pattern URL_PATTERN_PORTRAIT_BY_ID = Pattern.compile(BASE_URL + "/portrait/([0-9a-fA-F]{8}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{12})");
private static final Pattern URL_PATTERN_PORTRAIT_BY_URL = Pattern.compile(BASE_URL + "/portrait/url/(.*)");
private static final Pattern URL_PATTERN_RECORDING_IMAGE = Pattern.compile(BASE_URL + "/recording/(.*)");
private final PortraitStore portraitStore;
private final Config config;
@ -45,6 +49,9 @@ public class ImageServlet extends AbstractCtbrecServlet {
String modelUrl = URLDecoder.decode(m.group(1), UTF_8);
String portraitId = config.getSettings().modelPortraits.get(modelUrl);
servePortrait(resp, portraitId);
} else if ((m = URL_PATTERN_RECORDING_IMAGE.matcher(requestURI)).matches()) {
String filename = m.group(1);
serveRecordingImage(resp, filename);
}
} catch (Exception e) {
log.error(INTERNAL_SERVER_ERROR, e);
@ -66,6 +73,21 @@ public class ImageServlet extends AbstractCtbrecServlet {
}
}
private void serveRecordingImage(HttpServletResponse resp, String filename) throws IOException {
String recordingsDir = this.config.getSettings().recordingsDir;
File imageFile = new File(recordingsDir, filename);
if (!imageFile.exists() || !imageFile.isFile()) {
sendResponse(resp, SC_NOT_FOUND, "Image not found");
return;
}
resp.setStatus(SC_OK);
resp.setContentType(MIMETYPE_IMAGE_JPG);
Files.copy(imageFile.toPath(), resp.getOutputStream());
resp.getOutputStream().flush();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
String requestURI = req.getRequestURI().substring(req.getContextPath().length());