package com.lutao.cloud.oil.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.common.security.annotation.Inner;
import com.lutao.cloud.oil.pojo.entity.UploadFile;
import com.lutao.cloud.oil.pojo.param.FileParam;
import com.lutao.cloud.oil.service.UploadFileService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/sys-file")
@ApiOperation(value = "文件管理",tags = "文件管理")
public class SysFileController {

    @Resource
    private UploadFileService uploadFileService;

    @PostMapping("/uploadFile")
    @ApiOperation(value = "上传文件",notes = "上传文件")
    //@Inner(value = false)
    public MessageInfo uploadFile(@RequestPart("file") MultipartFile file) {
        return uploadFileService.uploadFile(file);
    }

    @GetMapping("/{bucket}/{fileName}")
    @ApiOperation(value = "获取文件",notes = "获取文件")
    @Inner(value = false)
    public void getFile(@PathVariable String bucket, @PathVariable String fileName, HttpServletResponse response){
         uploadFileService.getFile(bucket, fileName, response);
    }

    @DeleteMapping("/deleteFile/{id}")
    @ApiOperation(value = "删除文件",notes = "删除文件")
    //@Inner(value = false)
    public MessageInfo deleteFile(@PathVariable Long id) {
        return MessageInfo.ok(uploadFileService.deleteFile(id));
    }

    @PostMapping("/page")
    @ApiOperation(value = "文件分页",notes = "文件分页")
    //@Inner(value = false)
    public MessageInfo<IPage<UploadFile>> filePage(Page page, @RequestBody FileParam param){
        return uploadFileService.filePage(page, param);
    }


}
