package org.lym.pom.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.lym.pom.service.ImageStorageService;
import org.shoulder.core.dto.response.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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;

/**
 * 图片上传下载
 *
 * @author lym
 */
@Validated
@RestController
@RequestMapping("/api/images")
public class ImageController {

    @Autowired
    private ImageStorageService storageService;

    @PostMapping("upload")
    public BaseResult<String> uploadImage(@RequestParam("file") MultipartFile file) {
        String imageUrl = storageService.uploadImage(file);
        return BaseResult.success(imageUrl);
    }

    @GetMapping("{resourceIdOrPath}")
    public void downloadImage(@PathVariable("resourceIdOrPath") String imageId, HttpServletResponse response) throws IOException {
        storageService.downloadImage(imageId, response);
    }

    @DeleteMapping("{resourceIdOrPath}")
    public BaseResult<Boolean> deleteImage(@PathVariable("resourceIdOrPath") String imageId) throws IOException {
        return BaseResult.success(storageService.deleteImage(imageId));
    }

}
   