package org.dromara.main.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.system.domain.vo.SysOssUploadVo;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 企业入驻图片上传 Controller（公开接口，无需登录）
 *
 * <p>企业入驻图片上传与其他上传链路隔离，避免互相影响。</p>
 *
 * @author sj
 */
@SaIgnore
@RequiredArgsConstructor
@RestController
@RequestMapping("/portal/company/upload")
public class PortalCompanyUploadController {

    private final ISysOssService ossService;

    /**
     * 企业入驻图片上传
     *
     * @param file 图片文件
     */
    @PostMapping(value = "/image", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<SysOssUploadVo> uploadImage(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file) || file.isEmpty()) {
            return R.fail("上传文件不能为空");
        }
        String contentType = file.getContentType();
        if (contentType == null || !contentType.startsWith("image/")) {
            return R.fail("仅支持上传图片文件");
        }

        SysOssVo oss = ossService.upload(file);
        SysOssUploadVo uploadVo = new SysOssUploadVo();
        uploadVo.setUrl(oss.getUrl());
        uploadVo.setFileName(oss.getOriginalName());
        uploadVo.setOssId(oss.getOssId().toString());
        return R.ok(uploadVo);
    }
}
