refactor(基础模块): 使用FileManager来存储静态文件 (#484)
This commit is contained in:
parent
0074468c29
commit
a6281edb78
|
|
@ -72,5 +72,12 @@
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hswebframework.web</groupId>
|
||||||
|
<artifactId>hsweb-system-file</artifactId>
|
||||||
|
<version>${hsweb.framework.version}</version>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
@ -2,8 +2,12 @@ package org.jetlinks.community.io.file;
|
||||||
|
|
||||||
import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository;
|
import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository;
|
||||||
import org.hswebframework.web.crud.annotation.EnableEasyormRepository;
|
import org.hswebframework.web.crud.annotation.EnableEasyormRepository;
|
||||||
|
import org.hswebframework.web.file.FileServiceConfiguration;
|
||||||
|
import org.hswebframework.web.file.service.FileStorageService;
|
||||||
import org.jetlinks.community.config.ConfigManager;
|
import org.jetlinks.community.config.ConfigManager;
|
||||||
import org.jetlinks.core.rpc.RpcManager;
|
import org.jetlinks.core.rpc.RpcManager;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
@ -22,4 +26,14 @@ public class FileManagerConfiguration {
|
||||||
return new ClusterFileManager(rpcManager,properties,repository,configManager);
|
return new ClusterFileManager(rpcManager,properties,repository,configManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@AutoConfiguration(before = FileServiceConfiguration.class)
|
||||||
|
@ConditionalOnClass(FileStorageService.class)
|
||||||
|
static class StorageServiceConfiguration{
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FileStorageService fileStorageService(FileManager fileManager) {
|
||||||
|
return new FileManagerStorageService(fileManager);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
package org.jetlinks.community.io.file;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.hswebframework.web.file.service.FileStorageService;
|
||||||
|
import org.hswebframework.web.id.IDGenerator;
|
||||||
|
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||||
|
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||||
|
import org.springframework.http.codec.multipart.FilePart;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
import reactor.core.scheduler.Schedulers;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class FileManagerStorageService implements FileStorageService {
|
||||||
|
|
||||||
|
private final FileManager fileManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<String> saveFile(FilePart filePart) {
|
||||||
|
|
||||||
|
return fileManager
|
||||||
|
.saveFile(filePart, FileOption.publicAccess)
|
||||||
|
.map(FileInfo::getAccessUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<String> saveFile(InputStream inputStream, String fileType) {
|
||||||
|
return fileManager
|
||||||
|
.saveFile(IDGenerator.RANDOM.generate() + "." + fileType,
|
||||||
|
DataBufferUtils
|
||||||
|
.readInputStream(
|
||||||
|
() -> inputStream,
|
||||||
|
DefaultDataBufferFactory.sharedInstance,
|
||||||
|
64 * 1024)
|
||||||
|
.subscribeOn(Schedulers.boundedElastic()),
|
||||||
|
FileOption.publicAccess)
|
||||||
|
.map(FileInfo::getAccessUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue