package com.style24.scm.biz.web;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.style24.core.support.message.TscMessageByLocale;
import com.style24.scm.support.controller.TssBaseController;

import lombok.extern.slf4j.Slf4j;

import com.gagaframework.web.util.GagaFileUploadUtil;
import com.gagaframework.web.util.GagaUploadedFileInfo;

/**
 * Dextupload Controller
 *
 * @author gagamel
 * @since 2020. 12. 4
 */
@Controller
@RequestMapping("/dext")
@Slf4j
public class TssDextuploadController extends TssBaseController {

	@Autowired
	private Environment env;

	@Autowired
	private TscMessageByLocale message;

	/**
	 * dextupload를 이용한 파일 업로드
	 * @param policy - 업로드정책
	 * @param files - 파일 목록 (dx5는 DEXTUploadX5_FileData 명칭으로 넘어옴)
	 * @return 업로드된 파일 목록
	 * @throws Exception
	 * @author gagamel
	 * @since 2020. 12. 4
	 */
	@PostMapping("/files/upload/{policy}")
	@ResponseBody
	public Collection<GagaUploadedFileInfo> uploadFiles(@PathVariable("policy") String policy, @RequestParam(value = "DEXTUploadX5_FileData") List<MultipartFile> files) throws Exception {
		if (files == null || files.isEmpty()) {
			throw new IllegalStateException(message.getMessage("FAIL_1001"));
		}

		GagaFileUploadUtil fuUtil = new GagaFileUploadUtil(env.getProperty("upload.dext.target.path"), Long.parseLong(env.getProperty("upload." + policy + ".max.size")), env.getProperty("upload." + policy + ".allow.extension"), env.getProperty("upload." + policy + ".view"));

		return fuUtil.uploadFiles(files);
	}

}
