package com.sonic.shark.controller.api;

import com.sonic.common.auth.IgnoreAuth;
import com.sonic.common.rpc.Result;
import com.sonic.shark.domain.bo.NsfwRecognitionResult;
import com.sonic.shark.domain.input.NsfwRecognitionInput;
import com.sonic.shark.domain.input.UploadAwsS3Input;
import com.sonic.shark.enums.S3BucketNameEnum;
import com.sonic.shark.service.ImageCheckService;
import com.sonic.shark.service.NsfwConfigService;
import com.sonic.shark.service.OssService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * OSS文件上传内部API接口
 *
 * @Author zzhan
 * @Date 2020/9/23 21:41
 * @Version 1.0
 */
@RestController
@Slf4j
public class S3Api {

    @Autowired
    private OssService ossService;
    @Autowired
    private NsfwConfigService nsfwConfigService;
    @Autowired
    private ImageCheckService imageCheckService;


    @IgnoreAuth
    @ApiOperation(value = "通过文件地址上传到亚马逊s3", tags = {"API-文件持久化"})
    @PostMapping(value = "/api/oss/upload/aws/s3")
    public Result uploadToAwsS3(@Validated @RequestBody UploadAwsS3Input uploadAwsS3Input) {
        String s3Url = ossService.uploadFileBytes(uploadAwsS3Input.getBytes(), uploadAwsS3Input.getBizTypeEnum(), uploadAwsS3Input.getSuffix());
        return Result.success(s3Url);
    }

    @IgnoreAuth
    @ApiOperation(value = "图片鉴黄", tags = {"API-文件持久化"})
    @PostMapping(value = "/api/nsfw/recognition")
    public Result<Boolean> nsfwRecognition(@Validated @RequestBody NsfwRecognitionInput input) {
        S3BucketNameEnum s3BucketNameEnum = input.getBizTypeEnum().getS3BucketNameEnum();
        //图片鉴黄操作
        NsfwRecognitionResult nsfwRecognitionResult = nsfwConfigService.nsfwRecognition(s3BucketNameEnum, input.getFileFullPath());
        //鉴黄没通过
        if (nsfwRecognitionResult.getBl() != null && !nsfwRecognitionResult.getBl()) {
            imageCheckService.addImageCheckResult(s3BucketNameEnum.getRootUrl() + input.getFileFullPath());
        }
        return Result.success(nsfwRecognitionResult.getBl());
    }
}
