/* * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. * * https://www.mall4j.com/ * * 未经允许,不可做商业用途! * * 版权所有,侵权必究! */ package com.yami.shop.api.controller; import com.yami.shop.bean.app.dto.ResourcesInfoDto; import com.yami.shop.bean.model.AttachFile; import com.yami.shop.common.bean.AliOss; import com.yami.shop.common.bean.HuaWeiOss; import com.yami.shop.common.config.Constant; import com.yami.shop.common.response.ServerResponseEntity; import com.yami.shop.config.ShopConfig; import com.yami.shop.service.AttachFileService; import com.yami.shop.service.SysConfigService; import io.swagger.v3.oas.annotations.Operation; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; /** * 文件上传 controller * * @author lgh */ @RestController @RequestMapping("/p/file") public class FileController { @Autowired private AttachFileService attachFileService; @Autowired private ShopConfig shopConfig; @Autowired private SysConfigService sysConfigService; @PostMapping("/upload") @Operation(summary = "文件上传接口" , description = "上传文件,返回文件路径与域名") public ServerResponseEntity uploadFile(@RequestParam("file") MultipartFile file) throws IOException { if (file.isEmpty()) { return ServerResponseEntity.success(); } String fileName = attachFileService.uploadFile(file.getBytes(), file.getOriginalFilename()); String resourcesUrl = getResourcesUrl(); // String resourcesUrl = shopConfig.getDomain().getResourcesDomainName() + "/"; ResourcesInfoDto resourcesInfoDto = new ResourcesInfoDto(); resourcesInfoDto.setResourcesUrl(resourcesUrl); resourcesInfoDto.setFilePath(fileName); return ServerResponseEntity.success(resourcesInfoDto); } @PostMapping("/uploadImFile") @Operation(summary = "聊天文件上传接口" , description = "上传文件,返回文件路径与域名") public ServerResponseEntity uploadImFile(@RequestParam("file") MultipartFile file) throws IOException { if (file.isEmpty()) { return ServerResponseEntity.success(); } String fileName = attachFileService.uploadImFile(file.getBytes(), file.getOriginalFilename()); String resourcesUrl = getResourcesUrl(); // String resourcesUrl = shopConfig.getDomain().getResourcesDomainName() + "/"; ResourcesInfoDto resourcesInfoDto = new ResourcesInfoDto(); resourcesInfoDto.setResourcesUrl(resourcesUrl); resourcesInfoDto.setFilePath(fileName); return ServerResponseEntity.success(resourcesInfoDto); } private @NotNull String getResourcesUrl() { HuaWeiOss aliOss = sysConfigService.getSysConfigObject(Constant.HUAWEI_OBS_CONFIG, HuaWeiOss.class); String bucketName = aliOss.getBucketName();//shjw2024 String endpoint = aliOss.getEndpoint(); //https://oss-cn-shanghai.aliyuncs.com String pre = endpoint.substring(0, 8); String fix = endpoint.substring(8); String path = pre + bucketName + "." + fix; return path + "/"; } @GetMapping("/get_file_by_id") @Operation(summary = "根据文件id获取文件信息") public ServerResponseEntity getFileById(@RequestParam("fileId") Long fileId) { AttachFile attachFile = attachFileService.getById(fileId); return ServerResponseEntity.success(attachFile); } }