package com.sigma.gclc.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.text.MessageFormat;

import javax.annotation.PostConstruct;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
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;

@Controller
@RequestMapping("/backoffice/upload")
public class UploadController {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Value("${uploadImagesDirectory}")
	private File uploadImagesDirectory;

	@RequestMapping(produces=MediaType.TEXT_HTML_VALUE)
	public String uploadForm() {
		return "upload";
	}
	
	@PostConstruct
	public void postInit() {
		if (!uploadImagesDirectory.exists()) {
			if (!uploadImagesDirectory.mkdirs()) {
				throw new IllegalArgumentException(MessageFormat.format("Impossible de créer le répertoire {0}", uploadImagesDirectory));
			}
		}
	}
	
	@RequestMapping(method=RequestMethod.POST, consumes=MediaType.MULTIPART_FORM_DATA_VALUE)
	public String upload(@RequestParam(value="name", required=false) String name, @RequestParam(value="file", required=true) MultipartFile file) throws Exception {
		
		if (StringUtils.isBlank(name)) {
			name = file.getOriginalFilename();
		}
		if (StringUtils.isBlank(name)) {
			name = "Nom de fichier non déterminé";
		}
		
		if (file.isEmpty()) {
			logger.info("Fichier {} vide", name);
			throw new IllegalArgumentException(MessageFormat.format("Fichier {} est vide", name));
		}
		
		File uploadedFile = new File(uploadImagesDirectory, name);
		try (BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(uploadedFile)); BufferedInputStream input=new BufferedInputStream(file.getInputStream())) {
			IOUtils.copy(input, output);
			output.flush();
		}
		
		return "upload-success";
		
	}
}

@@ -125,173 +125,46 @@
 .io.
-BufferedReader;%0D%0Aimport java.io.BufferedWriter;%0D%0Aimport java.io.File;%0D%0Aimport java.io.FileOutputStream;%0D%0Aimport java.io.FileWriter;%0D%0Aimport java.io.InputStreamReader
+File;%0D%0Aimport java.io.FileOutputStream
 ;%0D%0Ai
@@ -1266,41 +1266,18 @@
 %09%09%09%09
-throw new IllegalArgumentExceptio
+logger.war
 n(Me
@@ -1362,16 +1362,61 @@
 ory));%0D%0A
+%09%09%09%09//throw new IllegalArgumentException();%0D%0A
 %09%09%09%7D%0D%0A%09%09
@@ -2017,16 +2017,228 @@
 %09%7D%0D%0A%09%09%0D%0A
+%09%09if (!uploadImagesDirectory.exists() && !uploadImagesDirectory.mkdirs()) %7B%0D%0A%09%09%09throw new IllegalArgumentException(MessageFormat.format(%22Impossible de cr%C3%A9er le r%C3%A9pertoire %7B0%7D%22, uploadImagesDirectory));%0D%0A%09%09%7D%0D%0A%09%09%0D%0A
 %09%09File u

fix UploadController et dossier n'existant pas au demarrage context