package com.cirle.scientific.controller.miniapp;


import com.cirle.scientific.pojo.BizCodeEnum;
import com.cirle.scientific.pojo.Result;
import com.cirle.scientific.utils.OSSPathUtil;
import com.cirle.scientific.utils.OSSUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
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.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequiredArgsConstructor
@RequestMapping("/file")
@Api("文件相关接口")
public class FileController {

    @PostMapping("/upload")
    @ResponseBody
    @ApiOperation(value = "上传接口 ")
    public Result<String> uploadOperationVideo(@ApiParam(name = "file", value = "上传的文件") @RequestParam("file") MultipartFile file
    ) {
        String filename = file.getOriginalFilename();
        String url = OSSUtils.uploadMultipartFile(file, OSSUtils.getKey(filename));

        //从个人中心修改用户信息处来
        return Result.success(url);
    }

    @PostMapping("/imgDelete")
    @ResponseBody
    @ApiOperation(value = "删除图片接口")
    public Result imgDelete(@RequestParam(name = "url") String url){
        if (url == null || "".equals(url)){
            return Result.failed("未查询到传值");
        }
        try {
            int pos = url.indexOf(OSSPathUtil.FILE_PATHS);
            if(pos != -1){
                OSSUtils.deleteFile(url.substring(OSSPathUtil.FILE_PATHS.length()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.success(BizCodeEnum.SUCCESS);
    }
}
