package com.izouma.awesomeadmin.web;

import com.izouma.awesomeadmin.dto.Result;
import com.izouma.awesomeadmin.service.OSSFileService;
import org.springframework.beans.factory.annotation.Autowired;
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.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by xiongzhu on 2017/3/8.
 */
@Controller
@RequestMapping("/assets")
public class AssetsController {

    @Autowired
    private OSSFileService OSSFileService;

    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    @ResponseBody
    public Result uploadImg(@RequestParam("base64") String base64) {

        try {
            String path = OSSFileService.uploadImg(base64);
            return new Result(true, path);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new Result(false, "上传失败");
    }

    @RequestMapping(value = "/uploadFile")
    @ResponseBody
    public Result uploadFile(HttpServletRequest request) {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if (multipartResolver.isMultipart(request)) {
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            Iterator it = multiRequest.getFileNames();
            List<String> paths = new ArrayList<>();
            while (it.hasNext()) {
                MultipartFile file = multiRequest.getFile(it.next().toString());
                if (file != null) {
                    try {
                        Random random = new Random();
                        StringBuilder randomCode = new StringBuilder();
                        for (int i = 0; i < 8; i++) {
                            randomCode.append(Integer.toString(random.nextInt(36), 36));
                        }
                        String ext = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                        String fileType = file.getContentType().substring(0, file.getContentType().indexOf("/"));
                        String uploadPath = String.format("%s/%s-%s%s", fileType, new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(new Date()), randomCode, ext);
                        String path = OSSFileService.upload(file.getInputStream(), uploadPath);
                        paths.add(path);
                    } catch (IOException e) {
                        e.printStackTrace();
                        return new Result(false, "上传失败");
                    }
                }
            }
            return new Result(true, paths);
        }
        return new Result(false, "上传失败");
    }


}
