package de.flux.playground.deckcompare.fileupload;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import de.flux.playground.deckcompare.Deckcompare;

@Controller
public class FileUploadController {

    @RequestMapping(method = RequestMethod.GET, value = "/upload")
    public String provideUploadInfo(Model model) {
        File rootFolder = new File(Deckcompare.ROOT);
        List<String> fileNames = Arrays.stream(rootFolder.listFiles()).map(f -> f.getName())
                                       .collect(Collectors.toList());

        model.addAttribute("files",
                           Arrays.stream(rootFolder.listFiles())
                                 .sorted(Comparator.comparingLong(f -> -1 * f.lastModified())).map(f -> f.getName())
                                 .collect(Collectors.toList()));

        return "uploadForm";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/upload")
    public String handleFileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file,
                                   RedirectAttributes redirectAttributes) {
        if (name.contains("/")) {
            redirectAttributes.addFlashAttribute("message", "Folder separators not allowed");
            return "redirect:upload";
        }
        if (name.contains("/")) {
            redirectAttributes.addFlashAttribute("message", "Relative pathnames not allowed");
            return "redirect:upload";
        }

        if (!file.isEmpty()) {
            try {
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(Deckcompare.ROOT
                                                                                                     + "/" + name)));
                FileCopyUtils.copy(file.getInputStream(), stream);
                stream.close();
                redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + name + "!");
            } catch (Exception e) {
                redirectAttributes.addFlashAttribute("message",
                                                     "You failed to upload " + name + " => " + e.getMessage());
            }
        } else {
            redirectAttributes.addFlashAttribute("message",
                                                 "You failed to upload " + name + " because the file was empty");
        }

        return "redirect:upload";
    }

}

@@ -194,31 +194,8 @@
 or;%0A
-import java.util.List;%0A
 impo
@@ -951,171 +951,8 @@
 OT);
-%0A        List%3CString%3E fileNames = Arrays.stream(rootFolder.listFiles()).map(f -%3E f.getName())%0A                                       .collect(Collectors.toList());
 %0A%0A  

Remove unused local variable