package com.ycwl.basic.controller;


import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.enums.BizCodeEnum;
import com.ycwl.basic.storage.StorageFactory;
import com.ycwl.basic.utils.ApiResponse;
import lombok.extern.slf4j.Slf4j;
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.RestController;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @Author: songmingsong
 * @CreateTime: 2024-12-05
 * @Description: 文件接口
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/file/v1")
@Slf4j
// 文件接口
public class FileController {

    // 上传文件
    @PostMapping("/upload")
    @IgnoreToken
    public ApiResponse<?> upload(@RequestParam(value = "file") MultipartFile file) throws IOException {
        String[] split = file.getOriginalFilename().split("\\.");
        String ext = split[split.length - 1];
        String url = StorageFactory.use().uploadFile(file, "web", UUID.randomUUID() + "." + ext);
        return ApiResponse.success(url);
    }

    // 删除文件
    @PostMapping("/delete")
    @IgnoreToken
    public ApiResponse<?> delete(@RequestParam(value = "fileName") String fileName) throws IOException {
        boolean flag = StorageFactory.use().deleteFile("web", fileName);
        return flag ? ApiResponse.success(BizCodeEnum.REQUEST_OK) : ApiResponse.fail(BizCodeEnum.FAIL.getMessage());
    }
}
