-1

I have to store an image for a few minutes after user upload it to show a preview, before confirmation, after confirmation i need get it back and persist.

I would like to know the best pratice to do this.

I saw about Cache and Caffeine, but i don't know if this is the best pratice and how store in Cache with a random hash to get it back after

[EDIT]

Maybe I was overestimating the problem

Following the @Robert suggestion i'll use temporary files, but i still need some way to garantee that files will be deleted. So i created a new question and i'll keep this to help others that do search with these terms.

Follow the link

How guarantee the file will be deleted after automatically some time?

Community
  • 1
  • 1

1 Answers1

0

I do this in one of my apps.

  • In the upload POST, I save the image to a temporary file and then store the temporary file name in a session attribute. I use the session attribute because the image being previewed shouldn't be visible to any other users until it has been written to persistent storage.
  • In the subsequent GET, I pull the temporary file name out of the session and stream it out to the response, deleting it when finished. I don't bother keeping the file after the preview is rendered as I don't need it anymore.

See the full implementation below:

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/imagePreview")
public class ImagePreviewController
{
    @PostMapping
    public ResponseEntity<?> post(HttpSession session, @RequestPart MultipartFile file) throws IOException
    {
        if (file.getContentType() != null && file.getContentType().startsWith("image/")) {
            Path tempFile = Files.createTempFile("", file.getOriginalFilename());
            file.transferTo(tempFile.toFile());
            session.setAttribute("previewImage", tempFile.toFile().getPath());
            session.setAttribute("previewImageContentType", file.getContentType());
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } else {
            return ResponseEntity.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE).build();
        }
    }

    @GetMapping
    public void get(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        HttpSession session = request.getSession(false);
        if (session == null) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        String path = (String) session.getAttribute("previewImage");
        String contentType = (String) session.getAttribute("previewImageContentType");
        if (path == null || contentType == null) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        response.setContentType(contentType);
        try (OutputStream out = response.getOutputStream()) {
            Files.copy(Paths.get(path), out);
        } finally {
            Files.deleteIfExists(Paths.get(path));
        }
    }
}
  • oh right, but what do you do when user don't call the get method? When this file will be deleted? This is my problem, how can i be sure that the file will be deleted? – Vinicius Cunha Apr 17 '17 at 22:27
  • thanks for idea @robert. I created another question about work with temporary files http://stackoverflow.com/questions/43483750/how-guarantee-the-file-will-be-deleted-after-automatically-some-time – Vinicius Cunha Apr 18 '17 at 23:20