package com.nbclass.activity.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
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.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.nbclass.activity.constant.Const;
import com.nbclass.aliyun.sdk.AliyunOSSUtils;
import com.nbclass.exception.ParameterException;
import com.nbclass.system.controller.BaseController;
import com.nbclass.util.CommonUtils;
import com.nbclass.vo.base.Result;

import lombok.extern.slf4j.Slf4j;

/**
 * 阿里云OSS上传工具
 * @author Leon
 * @datetime 2020年5月27日 下午6:51:55
 */
@Slf4j
@RestController
@RequestMapping("/console/tool/oss")
public class OssUploadController extends BaseController{

	/**
	 * i上传文件到阿里云OSS
	 * @param file
	 * @param category 文件类别【icon：案例列表icon图，attachment：案例附件，image：案例正文图集，video：视频】
	 * @return
	 */
    @PostMapping("/uploadFile")
    public Result fileUploadTest(@RequestParam(required=false, value="file")MultipartFile file, String caseType, String category){
    	if(file==null || file.isEmpty()){
    		throw new ParameterException("请选择文件");
    	}
    	/*if(StringUtils.isBlank(caseType)) {
    		throw new ParameterException("案例类型不能为空");
    	}else if(!Arrays.<String> asList("ppqa,ggqa,design,video,h5,ldhd,others".split(",")).contains(caseType)) {
			throw new ParameterException("案例类型参数有误");
		}*/
    	if(StringUtils.isBlank(category)) {
    		throw new ParameterException("文件类别不能为空");
    	}
    	category = category.toLowerCase().trim();
    	if(!Arrays.<String> asList("icon,attachment,image,video".split(",")).contains(category)) {
			throw new ParameterException("文件类别参数有误");
		}
		try {
			String suffix = "", ori_name = file.getOriginalFilename();
			if(StringUtils.isNotBlank(ori_name)){
				suffix = ori_name.substring(ori_name.lastIndexOf("."), ori_name.length());
				suffix = suffix.toLowerCase();
			}
			if(("icon".equals(category)||"image".equals(category))
					&& !Arrays.<String> asList(Const.UPLOAD_IMAGE_SUFFIX.split(",")).contains(suffix)) {
				throw new ParameterException("请上传正确的图片");
			}

			if("video".equals(category) && !Arrays.<String> asList(Const.UPLOAD_VIDEO_SUFFIX.split(",")).contains(suffix)) {
				throw new ParameterException("请上传正确的视频");
			}

			String daydir = CommonUtils.getSimpleDate(new Date(), "yyyyMM");
			if("icon".equals(category)) { // icon图上传一年1个目录
				daydir = CommonUtils.getSimpleDate(new Date(), "yyyy");
			}else if("video".equals(category)) { // 视频也是按一年1个目录
				daydir = CommonUtils.getSimpleDate(new Date(), "yyyy");
			}

			String filename = CommonUtils.getSimpleDate(new Date(), "yyyyMMddHHmmss")+"_"+CommonUtils.getCode(5)+suffix;
			// 组成的文件key eg. /casetype-h5/icons/xxx.jpg
			String url = AliyunOSSUtils.uploadBytes("casetype-"+caseType+"/"+category+"s"+"/"+daydir, filename, file.getBytes());
			JSONObject fileObj = new JSONObject();
			fileObj.put("url", url);
			fileObj.put("ori_name", ori_name);
			return Result.success(fileObj);
		} catch (IOException e) {
			log.error("上传到OSS文件异常", e);
			return Result.error(e.getMessage()!=null ? e.getMessage() :"上传文件失败");
		}
    }

}
