package io.controller;

import io.common.utils.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;


@CrossOrigin
@RestController
@RequestMapping("/api/upload")
@Tag(name = "上传文件")
public class UploadController {
    @Value("${upload.path}")
    private String path;

    @PostMapping()
    public Result upload(HttpServletRequest request,MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return new Result().error("文件不能为空！");
        }
        //把文件的内容存储到本地磁盘上
        String originFileName = file.getOriginalFilename();
        file.transferTo(new File(path + originFileName));
        String url =  "http://localhost:8081/front/upload/" + originFileName;
        HashMap<String, String> map = new HashMap<>();
        map.put("url",url);
        return new Result<>().ok(url);
    }
}
