package com.kodcu.controller;

import com.kodcu.other.Current;
import com.kodcu.service.DirectoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * Created by usta on 25.12.2014.
 */
@Controller
public class ImageController {

    private Logger logger = LoggerFactory.getLogger(ImageController.class);

    @Autowired
    private Current current;

    @Autowired
    private DirectoryService directoryService;

    @Autowired
    private ApplicationController controller;

    @RequestMapping(value = {"/**/{extension:(?:\\w|\\W)+\\.(?:jpg|bmp|gif|jpeg|png|webp)$}"}, method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<byte[]> images(HttpServletRequest request, @PathVariable("extension") String extension) {

        String uri = request.getRequestURI();
        byte[] temp = new byte[]{};
        if (uri.startsWith("/"))
            uri = uri.substring(1);

        Path imageFile = null;

        if (current.currentPath().isPresent()) {
            imageFile = current.currentPath().map(Path::getParent).get().resolve(uri);
        } else {
            imageFile = directoryService.getWorkingDirectory().get().resolve(uri);
        }

        try {
            temp = Files.readAllBytes(imageFile);
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
        }

        return new ResponseEntity<>(temp, HttpStatus.OK);
    }
}

@@ -77,89 +77,65 @@
 dcu.
-service.DirectoryService;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
+other.IOHelper;%0Aimport com.kodcu.service.DirectoryService
 ;%0Aim
@@ -635,36 +635,8 @@
 st;%0A
-import java.nio.file.Files;%0A
 impo
@@ -749,85 +749,8 @@
  %7B%0A%0A
-    private Logger logger = LoggerFactory.getLogger(ImageController.class);%0A%0A
     
@@ -1169,24 +1169,48 @@
 tension) %7B%0A%0A
+        Path imageFile;%0A
         Stri
@@ -1247,44 +1247,9 @@
 ();%0A
-        byte%5B%5D temp = new byte%5B%5D%7B%7D;
 %0A
+
     
@@ -1318,40 +1318,8 @@
 );%0A%0A
-        Path imageFile = null;%0A%0A
     
@@ -1573,149 +1573,55 @@
     
-try %7B%0A            temp = Files.readAllBytes(imageFile);%0A        %7D catch (Exception e) %7B%0A            logger.info(e.getMessage(), e);%0A        %7D
+byte%5B%5D temp = IOHelper.readAllBytes(imageFile);
 %0A%0A  

Update image controller