package com.cpop.system.business.controller.backstage;

import com.cpop.core.base.entity.R;
import com.cpop.core.config.CpopConfig;
import com.cpop.core.config.ServerConfig;
import com.cpop.core.utils.file.FileUploadUtils;
import com.cpop.core.utils.file.FileUtils;
import com.cpop.system.business.service.StoreService;
import com.cpop.system.business.vo.SysFileVo;
import com.cpop.system.framework.handler.TencentCosHandler;
import com.qcloud.cos.model.UploadResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * @author DB
 * @createTime 2023/09/13 15:10
 * @description 通用接口
 */
@Slf4j
@Tag(name = "通用接口")
@RestController
@RequestMapping("/backstage/sysCommon")
public class BackstageSysCommonController {

    @Autowired
    private TencentCosHandler tencentCosHandler;

    /**
     * cdn加速上传
     * @author DB
     * @since 2023/10/31 17:24
     * @param file 文件
     * @return com.jambox.core.base.R<com.qcloud.cos.model.UploadResult>
     */
    @Operation(summary = "cdn上传")
    @PostMapping("/cdnUpload")
    public R<SysFileVo> cdnUpload(MultipartFile file) {
        UploadResult uploadResult = tencentCosHandler.cdnUpload(file);
        String filename = FileUploadUtils.getInstance().extractFilename(file);
        SysFileVo sysFileVo = new SysFileVo();
        sysFileVo.setUrl( "https://" + uploadResult.getBucketName() + tencentCosHandler.getCdnUrl() + uploadResult.getKey())
                .setFileName(filename)
                .setNewFileName(FileUtils.getInstance().getName(filename))
                .setOriginalFilename(file.getOriginalFilename());
        return R.ok(sysFileVo);
    }

}
