Skip to content
Snippets Groups Projects
Commit 003950f1 authored by Léo Banno-Cloutier's avatar Léo Banno-Cloutier
Browse files

FileHandlerServlet: add cache-control header

Change-Id: I7e70297c0247594f0f04ea10e270d7eb4b9cf8a6
parent 4a747b90
No related branches found
No related tags found
No related merge requests found
import React from "react";
import React, { FC, MouseEventHandler } from "react";
import { makeStyles } from "@mui/styles";
import Dropzone from "react-dropzone";
import Dropzone, { Accept } from "react-dropzone";
import UploadIcon from "@mui/icons-material/CloudUpload";
import CloseIcon from "@mui/icons-material/Close";
......@@ -32,12 +32,27 @@ const useStyles = makeStyles({
},
});
function CustomImgDropZone(props) {
const { onDrop, accept, maxFiles, label, file, handleDelete, ...rest } =
props;
interface CustomImgDropZoneProps {
onDrop: (acceptedFiles: File[]) => void;
accept?: Accept;
maxFiles?: number;
label: string;
file: string;
handleDelete: MouseEventHandler<HTMLButtonElement>;
}
const CustomImgDropZone: FC<CustomImgDropZoneProps> = ({
onDrop,
accept,
maxFiles,
label,
file,
handleDelete,
...rest
}) => {
const classes = useStyles();
const handleDrop = (acceptedFiles) => {
const handleDrop = (acceptedFiles: File[]) => {
if (acceptedFiles.length > 0) {
onDrop(acceptedFiles);
}
......@@ -63,7 +78,7 @@ function CustomImgDropZone(props) {
)}
</Dropzone>
);
}
};
CustomImgDropZone.defaultProps = {
accept: { "image/*": [".png", ".jpeg", ".jpg", ".svg"] },
......
......@@ -14,12 +14,7 @@ import GridContainer from "components/Grid/GridContainer";
import { hexToRgb, blackColor } from "assets/jss/material-dashboard-react";
import {
api_path_get_image,
api_path_post_image,
url_path,
url_port,
} from "../../globalUrls";
import { api_path_post_image, url_path, url_port } from "../../globalUrls";
import dashboardStyle from "assets/jss/material-dashboard-react/views/dashboardStyle";
......@@ -146,7 +141,7 @@ export default function EditBlueprintUi({ blueprintName }) {
updatePolicyData("uiCustomization", newUiCustomization);
};
const handleImgDrop = (acceptedFiles: string[], imgType: string) => {
const handleImgDrop = (acceptedFiles: any[], imgType: string) => {
const formData = new FormData();
formData.append("file", acceptedFiles[0]);
......@@ -156,8 +151,9 @@ export default function EditBlueprintUi({ blueprintName }) {
method: "POST",
body: formData,
})
.then(() => {
const newUrl = `${api_path_get_image}/${blueprintName}/${imgType}/${acceptedFiles[0].name}`;
.then((res) => res.json())
.then((res) => {
const newUrl = res.url;
if (imgType === "background") {
handleUpdateUi({
......
......@@ -182,10 +182,6 @@ const CustomBackgroundForm = ({
handleImgDrop,
bgFile,
}) => {
const setBgFile = (value) => {
handleUpdateUi("bgFile", value);
};
return (
<>
<span>
......@@ -225,7 +221,6 @@ const CustomBackgroundForm = ({
.t("upload_an_image", "Upload an image")
.toUpperCase()}
file={bgFile}
setFile={setBgFile}
handleDelete={(e) => {
handleUpdateUi("backgroundUrl", "");
e.preventDefault();
......@@ -316,10 +311,6 @@ const LogoForm = ({
handleUpdateUi,
logoFile,
}) => {
const setLogoFile = (value) => {
handleUpdateUi("logoFile", value);
};
return (
<>
<span>
......@@ -336,7 +327,6 @@ const LogoForm = ({
.t("upload_a_logotype", "Upload a logotype")
.toUpperCase()}
file={logoFile}
setFile={setLogoFile}
handleDelete={(e) => {
handleDeleteLogo(e);
handleUpdateUi("logoUrl", null);
......
......@@ -9,6 +9,7 @@ import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;
import lombok.extern.slf4j.Slf4j;
import net.jami.jams.common.serialization.adapters.GsonFactory;
import java.io.File;
import java.io.IOException;
......@@ -16,6 +17,10 @@ import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
@MultipartConfig
@Slf4j
......@@ -23,18 +28,7 @@ import java.nio.file.Paths;
public class FileHandlerServlet extends HttpServlet {
private static final String IMAGES_DIR = "images";
private static final String SAVE_DIR =
".."
+ File.separator
+ ".."
+ File.separator
+ ".."
+ File.separator
+ ".."
+ File.separator
+ ".."
+ File.separator
+ IMAGES_DIR;
private static final String SAVE_DIR = "../../../../../" + IMAGES_DIR;
/**
* @apiVersion 1.0.0
......@@ -76,44 +70,29 @@ public class FileHandlerServlet extends HttpServlet {
File imagesFolder = new File(IMAGES_DIR + File.separator + blueprintName);
if (!imagesFolder.exists()) {
imagesFolder.mkdirs();
File imagesFolderLogo =
new File(
IMAGES_DIR
+ File.separator
+ blueprintName
+ File.separator
+ "logo");
File imagesFolderBackground =
new File(
IMAGES_DIR
+ File.separator
+ blueprintName
+ File.separator
+ "background");
imagesFolderLogo.mkdirs();
imagesFolderBackground.mkdirs();
Paths.get(IMAGES_DIR, blueprintName, "logo").toFile().mkdirs();
Paths.get(IMAGES_DIR, blueprintName, "background").toFile().mkdirs();
}
Part filePart = request.getPart("file"); // Retrieves <input type="file" name="file">
String fileName = filePart.getSubmittedFileName();
String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
for (Part part : request.getParts()) {
part.write(
SAVE_DIR
+ File.separator
+ blueprintName
+ File.separator
+ imageType
+ File.separator
+ fileName);
long now = System.currentTimeMillis();
fileName = now + "." + ext;
Path path = Paths.get(SAVE_DIR, blueprintName, imageType, fileName);
part.write(path.toString());
}
Map<String, String> map = new HashMap<>();
String url = "/api/image/filehandler/" + blueprintName + "/" + imageType + "/" + fileName;
map.put("url", url);
Gson gson = GsonFactory.createGson();
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().write("File upload was successful.");
response.getWriter().write(gson.toJson(map));
} catch (Exception e) {
log.error("Error during file upload: " + e.getMessage());
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
......@@ -174,6 +153,7 @@ public class FileHandlerServlet extends HttpServlet {
}
response.setContentType(mimeType);
response.setHeader("Cache-Control", "public, max-age=31536000");
try (OutputStream out = response.getOutputStream()) {
out.write(imageData);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment