From efdb99f8cecc4afb592afad79c761081d5d5cf22 Mon Sep 17 00:00:00 2001 From: lee <4766465@qq.com> Date: Wed, 18 Dec 2024 13:27:00 +0800 Subject: [PATCH] init --- yami-shop-api/src/main/java/com/yami/shop/api/controller/FileController.java | 95 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 95 insertions(+), 0 deletions(-) diff --git a/yami-shop-api/src/main/java/com/yami/shop/api/controller/FileController.java b/yami-shop-api/src/main/java/com/yami/shop/api/controller/FileController.java new file mode 100644 index 0000000..9fb2992 --- /dev/null +++ b/yami-shop-api/src/main/java/com/yami/shop/api/controller/FileController.java @@ -0,0 +1,95 @@ +/* + * 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<ResourcesInfoDto> 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<ResourcesInfoDto> 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<AttachFile> getFileById(@RequestParam("fileId") Long fileId) { + AttachFile attachFile = attachFileService.getById(fileId); + return ServerResponseEntity.success(attachFile); + } +} -- Gitblit v1.9.3