feat: 增加链接上传

This commit is contained in:
2024-12-14 10:38:51 +08:00
parent c0b9887851
commit b55931a438
5 changed files with 86 additions and 56 deletions

93
pom.xml
View File

@@ -57,6 +57,13 @@
<version>5.8.31</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>5.8.31</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ocr_api20210707</artifactId>
@@ -404,13 +411,13 @@
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<!-- <dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.maxmind.geoip2</groupId>-->
<!-- <artifactId>geoip2</artifactId>-->
<!-- <version>2.15.0</version>-->
<!-- <groupId>com.maxmind.geoip2</groupId>-->
<!-- <artifactId>geoip2</artifactId>-->
<!-- <version>2.15.0</version>-->
<!-- </dependency>-->
<!-- </dependency>-->
<dependency>
<groupId>com.google.googlejavaformat</groupId>
<artifactId>google-java-format</artifactId>
@@ -444,14 +451,14 @@
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.hazelcast</groupId>-->
<!-- <artifactId>hazelcast</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.hazelcast</groupId>-->
<!-- <artifactId>hazelcast-spring</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.hazelcast</groupId>-->
<!-- <artifactId>hazelcast</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.hazelcast</groupId>-->
<!-- <artifactId>hazelcast-spring</artifactId>-->
<!-- </dependency>-->
</dependencies>
@@ -510,36 +517,36 @@
</plugin>
<!-- <plugin>-->
<!-- <groupId>com.cosium.code</groupId>-->
<!-- <artifactId>git-code-format-maven-plugin</artifactId>-->
<!-- <version>${git-code-format-maven-plugin.version}</version>-->
<!-- <executions>-->
<!-- &lt;!&ndash; On commit, format the modified files &ndash;&gt;-->
<!-- <execution>-->
<!-- <id>install-formatter-hook</id>-->
<!-- <goals>-->
<!-- <goal>install-hooks</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- &lt;!&ndash; On Maven verify phase, fail if any file-->
<!-- (including unmodified) is badly formatted &ndash;&gt;-->
<!-- <execution>-->
<!-- <id>validate-code-format</id>-->
<!-- <goals>-->
<!-- <goal>validate-code-format</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- <dependencies>-->
<!-- &lt;!&ndash; Enable https://github.com/google/google-java-format &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.cosium.code</groupId>-->
<!-- <artifactId>google-java-format</artifactId>-->
<!-- <version>${git-code-format-maven-plugin.version}</version>-->
<!-- </dependency>-->
<!-- </dependencies>-->
<!-- </plugin>-->
<!-- <plugin>-->
<!-- <groupId>com.cosium.code</groupId>-->
<!-- <artifactId>git-code-format-maven-plugin</artifactId>-->
<!-- <version>${git-code-format-maven-plugin.version}</version>-->
<!-- <executions>-->
<!-- &lt;!&ndash; On commit, format the modified files &ndash;&gt;-->
<!-- <execution>-->
<!-- <id>install-formatter-hook</id>-->
<!-- <goals>-->
<!-- <goal>install-hooks</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- &lt;!&ndash; On Maven verify phase, fail if any file-->
<!-- (including unmodified) is badly formatted &ndash;&gt;-->
<!-- <execution>-->
<!-- <id>validate-code-format</id>-->
<!-- <goals>-->
<!-- <goal>validate-code-format</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- <dependencies>-->
<!-- &lt;!&ndash; Enable https://github.com/google/google-java-format &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.cosium.code</groupId>-->
<!-- <artifactId>google-java-format</artifactId>-->
<!-- <version>${git-code-format-maven-plugin.version}</version>-->
<!-- </dependency>-->
<!-- </dependencies>-->
<!-- </plugin>-->
</plugins>
</build>

View File

@@ -31,14 +31,15 @@ public class FileController {
@PostMapping("/upload")
public FileDto upload(
@RequestParam("file") MultipartFile file,
@RequestParam(value = "file", required = false) MultipartFile file,
@RequestParam("name") String name,
@RequestParam("dir") String dir,
@RequestParam(value = "url", required = false) String url,
@RequestParam(value = "sortKey", required = false) String sortKey,
@RequestParam(value = "code", required = false) String code,
@RequestParam(value = "remarks", required = false) String remarks
) {
return this.service.upload(file, name, dir, sortKey, code, remarks);
return this.service.upload(file, name, dir, sortKey, code, remarks, url);
}
@PostMapping("/update")

View File

@@ -1,5 +1,6 @@
package cn.lihongjie.coal.file.service;
import cn.hutool.http.HttpUtil;
import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.service.BaseService;
@@ -34,6 +35,8 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.UUID;
@@ -106,10 +109,27 @@ public class FileService extends BaseService<FileEntity, FileRepository> {
String dir,
String sortKey,
String code,
String remarks) {
String remarks,
String url) {
long size = 0;
InputStream fis = null;
if (file != null) {
size = file.getSize();
fis = file.getInputStream();
} else if (StringUtils.isNotEmpty(url)) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
HttpUtil.download(url, out, true);
fis = new ByteArrayInputStream(out.toByteArray());
size = out.size();
} else {
throw new BizException("文件为空");
}
FileEntity fileEntity = new FileEntity();
fileEntity.setFileName(name);
fileEntity.setFileSize(file.getSize());
fileEntity.setFileSize(size);
fileEntity.setCode(code);
fileEntity.setRemarks(remarks);
try {
@@ -125,7 +145,7 @@ public class FileService extends BaseService<FileEntity, FileRepository> {
activateProfile,
Ctx.currentUser().getOrganizationId(),
StringUtils.defaultIfBlank(dir, "public")));
InputStream inputStream = file.getInputStream();
InputStream inputStream = fis;
fileEntity.setObjectId(
UUID.randomUUID() + "." + FilenameUtils.getExtension(name.replace(":", "_")));
try (inputStream) {
@@ -134,7 +154,7 @@ public class FileService extends BaseService<FileEntity, FileRepository> {
String objectKey = getObjectKey(fileEntity);
fileEntity.setPublicUrl(uploadToOSS(objectKey, file.getInputStream()));
fileEntity.setPublicUrl(uploadToOSS(objectKey, fis));
this.save(fileEntity);
@@ -147,9 +167,7 @@ public class FileService extends BaseService<FileEntity, FileRepository> {
com.aliyun.oss.model.PutObjectResult objectResult =
ossClient.putObject(
aliyunProperty.getOSS().getBucketName(),
objectKey,
inputStream);
aliyunProperty.getOSS().getBucketName(), objectKey, inputStream);
} catch (Exception e) {
log.warn("上传文件失败", e);
@@ -224,8 +242,7 @@ public class FileService extends BaseService<FileEntity, FileRepository> {
try {
OSSObject object =
ossClient.getObject(aliyunProperty.getOSS().getBucketName(), objectKey);
return
object.getObjectContent();
return object.getObjectContent();
} catch (Exception e) {
log.warn("下载文件失败", e);
throw new BizException("下载文件失败 %s".formatted(e.getMessage()));

View File

@@ -4,6 +4,7 @@ import cn.lihongjie.coal.goodsSearch.dto.GoodsSearchRequest;
import cn.lihongjie.coal.goodsSearch.dto.GoodsSearchResponse;
import cn.lihongjie.coal.goodsSearch.service.GoodsSearchService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/goodsSearch")
@@ -18,7 +19,7 @@ public class GoodsSearchController {
}
@RequestMapping("/search")
public GoodsSearchResponse search(GoodsSearchRequest request) {
public GoodsSearchResponse search(@RequestBody GoodsSearchRequest request) {
return

View File

@@ -21,8 +21,11 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets;
@Service
@Slf4j
public class GoodsSearchService {
@@ -44,11 +47,12 @@ public class GoodsSearchService {
}
@SneakyThrows
@Cacheable(cacheNames = "goodsSearch", key = "#request.code", condition = "#request.code != null", unless = "#result.flag.equals('true')")
public GoodsSearchResponse search(GoodsSearchRequest request) {
ApiResponse response = Client.getInstance().search(request.getCode());
String bodyStr = response.getBodyStr();
String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8);
ObjectNode jsonNodes = objectMapper.readValue(bodyStr, ObjectNode.class);