package com.ruoyi.dk.controller.app;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.dk.dto.common.FileUploadRes;
import com.ruoyi.dk.oss.ali.StsOssKit;
import com.ruoyi.dk.oss.ali.StsResult;
import com.ruoyi.component.core.domain.R;
import com.ruoyi.component.core.util.MessageUtils;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.ISysOssService;
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.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>created on 2021/7/13</p>
 * @author zhangliang
 */
@RestController
@Tag(name = "通用接口")
@RequestMapping("/app/v2/common")
@Slf4j
public class V2CommonController {
	@Autowired
	private StsOssKit stsOssKit;
	@Autowired
	private ISysOssService iSysOssService;

	@GetMapping("/sts")
	@Operation(summary = "文件上传")
	public R getSts(){
		StsResult stsToken = stsOssKit.getStsToken();
		if(stsToken == null){
			return R.fail(400, MessageUtils.message("dk.file.upload.init.error"));
		}
		return R.ok(stsToken);
	}

	@PostMapping("/upload")
	@Operation(summary = "文件上传")
	public R uploadFile(MultipartFile file) {
		if (ObjectUtil.isNull(file)) {
			return R.fail("上传文件不能为空");
		}
		SysOssVo oss = iSysOssService.upload(file);
		FileUploadRes resp = new FileUploadRes();
		resp.setUrl(oss.getUrl());
		resp.setPath(oss.getFileName());
		resp.setFileName(oss.getOriginalName());
		return R.ok(resp);
	}

}
