34 lines
1.2 KiB
Java
34 lines
1.2 KiB
Java
package ru.charm.back.controller;
|
|
|
|
import jakarta.servlet.ServletException;
|
|
import jakarta.servlet.annotation.WebServlet;
|
|
import jakarta.servlet.http.HttpServlet;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import ru.charm.back.service.ContentService;
|
|
|
|
import java.io.IOException;
|
|
|
|
import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND;
|
|
|
|
@WebServlet("/content/*")
|
|
@Slf4j
|
|
public class ContentController extends HttpServlet {
|
|
|
|
private final ContentService service = ContentService.getInstance();
|
|
|
|
@Override
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
try {
|
|
String contentPath = req.getRequestURI().replace("/content/", "");
|
|
resp.setContentType("application/octet-stream");
|
|
service.download(contentPath, resp.getOutputStream());
|
|
log.info("Content successfully downloaded: {}", contentPath);
|
|
} catch (IOException e) {
|
|
log.error("Content download failed: {}", e.getMessage());
|
|
resp.sendError(SC_NOT_FOUND);
|
|
}
|
|
}
|
|
}
|