package com.xingxi.web.controller.system.controller;

import com.xingxi.common.core.controller.BaseController;
import com.xingxi.common.core.domain.AjaxResult;
import com.xingxi.common.enums.EUploadFileType;
import com.xingxi.common.exception.file.InvalidExtensionException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传
 *
 * @author xingxi
 */

@RestController
@RequestMapping("/common/upload/file")
public class SysFileUploadController extends BaseController {
    @PostMapping("/image")
    public AjaxResult image(MultipartFile imageFile, @RequestHeader(required = false, name = "X-File-Type") String xFileType) {
        EUploadFileType eUploadFileType = EUploadFileType.IMAGE;
        EUploadFileType[] eUploadFileTypes = EUploadFileType.values();
        for (EUploadFileType uploadFileType : eUploadFileTypes) {
            if (uploadFileType.getVal().equals(xFileType)) {
                eUploadFileType = uploadFileType;
                break;
            }
        }

        AjaxResult ajaxResult = AjaxResult.success();
        try {
            ajaxResult.put("imageUrl", this.upload(imageFile, eUploadFileType));
        } catch (Exception ex) {
            if (ex instanceof InvalidExtensionException) {
                ajaxResult = AjaxResult.error("文件格式错误，请选择图片文件");
            } else {
                ajaxResult = AjaxResult.error();
            }
        }
        return ajaxResult;
    }

    @PostMapping("/avatar")
    public AjaxResult designerAvatar(MultipartFile imageFile, @RequestHeader(required = false, name = "X-File-Type") String xFileType) {
        EUploadFileType eUploadFileType = EUploadFileType.AVATAR;
        EUploadFileType[] eUploadFileTypes = EUploadFileType.values();
        for (EUploadFileType uploadFileType : eUploadFileTypes) {
            if (uploadFileType.getVal().equals(xFileType)) {
                eUploadFileType = uploadFileType;
                break;
            }
        }

        AjaxResult ajaxResult = AjaxResult.success();
        try {
            ajaxResult.put("imageUrl", this.upload(imageFile, eUploadFileType));
        } catch (Exception ex) {
            if (ex instanceof InvalidExtensionException) {
                ajaxResult = AjaxResult.error("文件格式错误，请选择图片文件");
            } else {
                ajaxResult = AjaxResult.error();
            }
        }
        return ajaxResult;
    }

    @PostMapping("/video")
    public AjaxResult video(MultipartFile videoFile) {
        AjaxResult ajaxResult = AjaxResult.success();
        try {
            ajaxResult.put("videoUrl", this.upload(videoFile, EUploadFileType.VIDEO));
        } catch (Exception ex) {
            if (ex instanceof InvalidExtensionException) {
                ajaxResult = AjaxResult.error("文件格式错误，请选择视频文件");
            } else {
                ajaxResult = AjaxResult.error();
            }
        }
        return ajaxResult;
    }
}
