package cn.reghao.oss.console.app.controller;

import cn.reghao.jutil.jdk.result.WebResult;
import cn.reghao.oss.console.app.model.dto.ObjectsScopeDto;
import cn.reghao.oss.console.app.rpc.OssObjectService;
import cn.reghao.oss.storeapi.dto.ObjectInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2024-02-23 09:27:28
 */
@Api(tags = "对象接口")
@RestController
@RequestMapping("/api/oss/object")
public class ObjectController {
    private final OssObjectService ossObjectService;

    public ObjectController(OssObjectService ossObjectService) {
        this.ossObjectService = ossObjectService;
    }

    @ApiOperation(value = "设置对象列表可见范围")
    @PostMapping(value = "/scope/objects", produces = MediaType.APPLICATION_JSON_VALUE)
    public String setObjectsScope(ObjectsScopeDto objectsScopeDto) throws Exception {
        ossObjectService.setObjectsScope(objectsScopeDto);
        return WebResult.success();
    }

    @ApiOperation(value = "根据 object_id 删除对象")
    @PostMapping(value = "/delete/ids", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteByObjects(@RequestParam("channelId") Integer channelId,
                                  @RequestParam("objectIds") List<String> objectIds) throws Exception {
        ossObjectService.deleteByObjectIds(channelId, objectIds);
        return WebResult.success();
    }

    @ApiOperation(value = "根据 object_url 删除对象")
    @PostMapping(value = "/delete/url", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteByObjectUrl(@RequestParam("objectUrl") String objectUrl) throws Exception {
        ossObjectService.deleteByObjectUrl(objectUrl);
        return WebResult.success();
    }

    @ApiOperation(value = "获取对象签名 url")
    @GetMapping(value = "/url", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getObjectUrl(@RequestParam("channelId") Integer channelId,
                               @RequestParam("objectId") String objectId) throws Exception {
        String signedUrl = ossObjectService.getSignedUrl(channelId, objectId);
        return WebResult.success(signedUrl);
    }

    @ApiOperation(value = "获取对象信息")
    @GetMapping(value = "/info", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getObjectInfo(@RequestParam("channelId") int channelId,
                                @RequestParam("objectId") String objectId) throws Exception {
        ObjectInfo objectInfo = ossObjectService.getObjectInfo(channelId, objectId);
        return WebResult.success(objectInfo);
    }
}
