package com.qxgmat.controller.admin;

import com.nuliji.tools.Response;
import com.nuliji.tools.ResponseHelp;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
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.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController("AdminCommonController")
@RequestMapping("/admin/common")
public class CommonController {

    @Value("${upload.local_path}")
    private String localPath;

    @Value("${upload.web_url}")
    private String webUrl;

    @RequestMapping(value = "/upload/video", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE, method = RequestMethod.POST)
    @ApiOperation(value = "上传视频", notes = "上传至本地服务器", httpMethod = "POST")
    public Response<String> uploadVideo(@RequestParam("file") MultipartFile multipartFile)  {
        if (multipartFile.isEmpty()) {
            throw new ParameterException("上传文件为空");
        }
        String filename = multipartFile.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf(".")+1);
        String contentType = multipartFile.getContentType();
        if (!contentType.contains("")) {
            throw new ParameterException("文件类型错误");
        }
        // todo 随机文件名
        String file = UUID.randomUUID().toString()+"."+suffix;
        try {
            File dir = new File(localPath);
            File dest = new File(dir.getAbsolutePath() + File.separator+file);
            multipartFile.transferTo(dest);
            return ResponseHelp.success(webUrl+file);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseHelp.exception(new SystemException("视频上传失败"));
        }
    }
    @RequestMapping(value = "/upload/image", method = RequestMethod.POST)
    @ApiOperation(value = "上传文件", notes = "上传至本地服务器", httpMethod = "POST")
    public Response<String> uploadImage(@RequestParam("file") MultipartFile multipartFile)  {
        if (multipartFile.isEmpty()) {
            throw new ParameterException("上传文件为空");
        }
        String filename = multipartFile.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf(".")+1);
        String contentType = multipartFile.getContentType();
        if (!contentType.contains("")) {
            throw new ParameterException("文件类型错误");
        }
        // todo 随机文件名
        String file = UUID.randomUUID().toString()+"."+suffix;
        try {
            File dir = new File(localPath);
            File dest = new File(dir.getAbsolutePath() + File.separator+file);

            multipartFile.transferTo(dest);
            return ResponseHelp.success(webUrl+file);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseHelp.exception(new SystemException("图片上传失败"));
        }
    }
}
