package com.njcn.product.system.fileOperate;


import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;

import com.njcn.common.utils.HttpResultUtil;
import com.njcn.oss.utils.FileStorageUtil;
import com.njcn.product.system.fileOperate.pojo.vo.FileVO;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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 javax.servlet.http.HttpServletResponse;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年10月16日 19:34
 */
@Slf4j
@Api(tags = "文件控制管理器")
@RestController
@RequestMapping("/system-boot/file")
@RequiredArgsConstructor
public class FileController extends BaseController {

    private final FileStorageUtil fileStorageUtil;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("文件上传")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public HttpResult<FileVO> upload(@RequestParam(value = "file") MultipartFile file, @RequestParam("path") String path, @RequestParam(defaultValue = "true") Boolean isReserveName) {
        String methodDescribe = getMethodDescribe("upload");
        String ossPath = fileStorageUtil.uploadMultipart(file, path,isReserveName);
        String url = fileStorageUtil.getFileUrl(ossPath);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, new FileVO(ossPath,url), methodDescribe);
    }


    @ApiOperation("删除文件服务器文件")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public HttpResult<String> delete(String filePath) {
        String methodDescribe = getMethodDescribe("delete");
        fileStorageUtil.deleteFile(filePath);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, CommonResponseEnum.SUCCESS.getMessage(), methodDescribe);
    }

    @ApiOperation("下载文件")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void download(String filePath, HttpServletResponse response) {
        fileStorageUtil.downloadStream(response, filePath);
    }

    @ApiOperation("获取文件的一个短期url")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @RequestMapping(value = "/getFileUrl", method = RequestMethod.GET)
    public HttpResult<String> getFileUrl(String filePath) {
        String methodDescribe = getMethodDescribe("getFileUrl");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, fileStorageUtil.getFileUrl(filePath), methodDescribe);
    }
    @ApiOperation("获取文件的一个短期url及文件名")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @RequestMapping(value = "/getFileVO", method = RequestMethod.GET)
    public HttpResult<FileVO> getFileVO(String filePath) {
        String methodDescribe = getMethodDescribe("getFileVO");
        String url = fileStorageUtil.getFileUrl(filePath);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,  new FileVO(filePath,url), methodDescribe);
    }


}
