1919import jakarta .annotation .PostConstruct ;
2020import org .slf4j .Logger ;
2121import org .slf4j .LoggerFactory ;
22- import org .springframework .beans .factory .annotation .Autowired ;
2322import org .springframework .boot .autoconfigure .AutoConfiguration ;
24- import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
2523import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
2624import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
2725import org .springframework .boot .autoconfigure .web .servlet .MultipartProperties ;
2826import org .springframework .boot .context .properties .EnableConfigurationProperties ;
27+ import org .springframework .context .ApplicationContext ;
2928import org .springframework .context .annotation .Bean ;
30- import org .springframework .context .annotation .Configuration ;
3129import org .springframework .context .annotation .Import ;
30+ import top .continew .starter .storage .annotation .PlatformProcessor ;
3231import top .continew .starter .storage .autoconfigure .properties .StorageProperties ;
3332import top .continew .starter .storage .core .FileStorageService ;
34- import top .continew .starter .storage .core .ProcessorRegistry ;
35- import top .continew .starter .storage .core .StrategyProxyFactory ;
36- import top .continew .starter .storage .prehandle .*;
37- import top .continew .starter .storage .prehandle .impl .*;
38- import top .continew .starter .storage .router .StorageStrategyRegistrar ;
39- import top .continew .starter .storage .router .StorageStrategyRouter ;
33+ import top .continew .starter .storage .engine .StorageDecoratorManager ;
34+ import top .continew .starter .storage .processor .registry .ProcessorRegistry ;
35+ import top .continew .starter .storage .processor .preprocess .*;
36+ import top .continew .starter .storage .processor .preprocess .impl .*;
37+ import top .continew .starter .storage .engine .StorageStrategyRegistrar ;
38+ import top .continew .starter .storage .engine .StorageStrategyRouter ;
39+ import top .continew .starter .storage .service .FileProcessor ;
4040import top .continew .starter .storage .service .FileRecorder ;
4141import top .continew .starter .storage .service .impl .DefaultFileRecorder ;
42- import top .continew .starter .storage .strategy .StorageStrategyOverride ;
4342
4443import java .util .List ;
44+ import java .util .Map ;
4545
4646/**
4747 * 存储自动配置
5151 */
5252@ AutoConfiguration
5353@ EnableConfigurationProperties (StorageProperties .class )
54- @ Import ({ProcessorRegistry .class , StrategyProxyFactory . class })
54+ @ Import ({ProcessorRegistry .class })
5555public class StorageAutoConfiguration {
5656
5757 private static final Logger log = LoggerFactory .getLogger (StorageAutoConfiguration .class );
5858
5959 private final StorageProperties properties ;
60+ private final ApplicationContext applicationContext ;
6061
61- public StorageAutoConfiguration (StorageProperties properties ) {
62+ public StorageAutoConfiguration (StorageProperties properties , ApplicationContext applicationContext ) {
6263 this .properties = properties ;
64+ this .applicationContext = applicationContext ;
6365 }
6466
6567 /**
@@ -70,17 +72,27 @@ public StorageAutoConfiguration(StorageProperties properties) {
7072 */
7173 @ Bean
7274 public StorageStrategyRouter strategyRouter (List <StorageStrategyRegistrar > registrars ) {
73- return new StorageStrategyRouter (registrars );
75+ return new StorageStrategyRouter (registrars , properties , storageDecoratorManager () );
7476 }
7577
7678 /**
77- * S3存储自动配置
79+ * 存储装饰器管理器
7880 *
79- * @return {@link S3StorageAutoConfiguration }
81+ * @return {@link StorageDecoratorManager }
8082 */
8183 @ Bean
82- public S3StorageAutoConfiguration s3StorageAutoConfiguration () {
83- return new S3StorageAutoConfiguration (properties );
84+ public StorageDecoratorManager storageDecoratorManager () {
85+ return new StorageDecoratorManager (applicationContext );
86+ }
87+
88+ /**
89+ * oss存储自动配置
90+ *
91+ * @return {@link OssStorageAutoConfiguration }
92+ */
93+ @ Bean
94+ public OssStorageAutoConfiguration ossStorageAutoConfiguration () {
95+ return new OssStorageAutoConfiguration (properties );
8496 }
8597
8698 /**
@@ -99,16 +111,15 @@ public LocalStorageAutoConfiguration localStorageAutoConfiguration() {
99111 * @param router 路由
100112 * @param storageProperties 存储属性
101113 * @param processorRegistry 处理器注册表
102- * @param proxyFactory 代理工厂
114+ * @param fileRecorder 文件记录器
103115 * @return {@link FileStorageService }
104116 */
105117 @ Bean
106118 public FileStorageService fileStorageService (StorageStrategyRouter router ,
107119 StorageProperties storageProperties ,
108120 ProcessorRegistry processorRegistry ,
109- StrategyProxyFactory proxyFactory ,
110121 FileRecorder fileRecorder ) {
111- return new FileStorageService (router , storageProperties , processorRegistry , proxyFactory , fileRecorder );
122+ return new FileStorageService (router , storageProperties , processorRegistry , fileRecorder );
112123 }
113124
114125 /**
@@ -122,125 +133,74 @@ public FileRecorder fileRecorder() {
122133 return new DefaultFileRecorder ();
123134 }
124135
136+ /**
137+ * 处理器注册中心
138+ */
139+ @ Bean
140+ public ProcessorRegistry processorRegistry () {
141+ ProcessorRegistry registry = new ProcessorRegistry ();
142+
143+ // 自动发现并注册所有 FileProcessor 实现
144+ Map <String , FileProcessor > processors = applicationContext .getBeansOfType (FileProcessor .class );
145+ processors .values ().forEach (processor -> {
146+ // 检查是否有平台注解
147+ PlatformProcessor annotation = processor .getClass ().getAnnotation (PlatformProcessor .class );
148+ if (annotation != null ) {
149+ for (String platform : annotation .platforms ()) {
150+ registry .register (processor , platform );
151+ }
152+ } else {
153+ // 注册为全局处理器
154+ registry .register (processor );
155+ }
156+ });
157+ return registry ;
158+ }
159+
125160 /**
126161 * 默认文件名生成器
127- *
128- * @param registry 登记处
129- * @return {@link FileNameGenerator }
130162 */
131163 @ Bean
132- @ ConditionalOnMissingBean (name = "defaultFileNameGenerator" )
133- public FileNameGenerator defaultFileNameGenerator (ProcessorRegistry registry ) {
134- DefaultFileNameGenerator generator = new DefaultFileNameGenerator ();
135- registry .registerGlobalNameGenerator (generator );
136- return generator ;
164+ @ ConditionalOnMissingBean (FileNameGenerator .class )
165+ public FileNameGenerator defaultFileNameGenerator () {
166+ return new DefaultFileNameGenerator ();
137167 }
138168
139169 /**
140- * 默认文件路径生成器
141- *
142- * @param registry 注册
143- * @return {@link FilePathGenerator }
170+ * 默认路径生成器
144171 */
145172 @ Bean
146- @ ConditionalOnMissingBean (name = "defaultFilePathGenerator" )
147- public FilePathGenerator defaultFilePathGenerator (ProcessorRegistry registry ) {
148- DefaultFilePathGenerator generator = new DefaultFilePathGenerator ();
149- registry .registerGlobalPathGenerator (generator );
150- return generator ;
173+ @ ConditionalOnMissingBean (FilePathGenerator .class )
174+ public FilePathGenerator defaultFilePathGenerator () {
175+ return new DefaultFilePathGenerator ();
151176 }
152177
153178 /**
154179 * 默认缩略图处理器
155- *
156- * @param registry 注册
157- * @return {@link ThumbnailProcessor }
158180 */
159181 @ Bean
160- @ ConditionalOnMissingBean (name = "defaultThumbnailProcessor" )
182+ @ ConditionalOnMissingBean (ThumbnailProcessor . class )
161183 @ ConditionalOnClass (name = "net.coobird.thumbnailator.Thumbnails" )
162- public ThumbnailProcessor defaultThumbnailProcessor (ProcessorRegistry registry ) {
163- DefaultThumbnailProcessor processor = new DefaultThumbnailProcessor ();
164- registry .registerGlobalThumbnailProcessor (processor );
165- return processor ;
184+ public ThumbnailProcessor defaultThumbnailProcessor () {
185+ return new DefaultThumbnailProcessor ();
166186 }
167187
168188 /**
169189 * 文件大小验证器
170- *
171- * @param registry 注册
172- * @return {@link FileValidator }
173190 */
174191 @ Bean
175192 @ ConditionalOnMissingBean (name = "fileSizeValidator" )
176- public FileValidator fileSizeValidator (ProcessorRegistry registry , MultipartProperties multipartProperties ) {
177- FileSizeValidator validator = new FileSizeValidator (multipartProperties );
178- registry .registerGlobalValidator (validator );
179- return validator ;
193+ public FileValidator fileSizeValidator (MultipartProperties multipartProperties ) {
194+ return new FileSizeValidator (multipartProperties );
180195 }
181196
182197 /**
183198 * 文件类型验证器
184- *
185- * @param registry 注册
186- * @return {@link FileValidator }
187199 */
188200 @ Bean
189201 @ ConditionalOnMissingBean (name = "fileTypeValidator" )
190- public FileValidator fileTypeValidator (ProcessorRegistry registry ) {
191- FileTypeValidator validator = new FileTypeValidator ();
192- registry .registerGlobalValidator (validator );
193- return validator ;
194- }
195-
196- /**
197- * 策略重写自动注册
198- */
199- @ Configuration
200- @ ConditionalOnBean (StorageStrategyOverride .class )
201- public static class StrategyOverrideConfiguration {
202-
203- /**
204- * 注册覆盖
205- */
206- @ Autowired
207- public void registerOverrides (List <StorageStrategyOverride <?>> overrides , StrategyProxyFactory proxyFactory ) {
208- for (StorageStrategyOverride <?> override : overrides ) {
209- proxyFactory .registerOverride (override );
210- }
211- }
212- }
213-
214- /**
215- * 处理器自动注册
216- */
217- @ Configuration
218- public static class ProcessorAutoConfiguration {
219- @ Autowired (required = false )
220- public void registerGlobalProcessors (List <FileNameGenerator > nameGenerators ,
221- List <FilePathGenerator > pathGenerators ,
222- List <ThumbnailProcessor > thumbnailProcessors ,
223- List <FileValidator > validators ,
224- List <UploadCompleteProcessor > completeProcessors ,
225- ProcessorRegistry registry ) {
226-
227- // 注册全局处理器
228- if (nameGenerators != null ) {
229- nameGenerators .forEach (registry ::registerGlobalNameGenerator );
230- }
231- if (pathGenerators != null ) {
232- pathGenerators .forEach (registry ::registerGlobalPathGenerator );
233- }
234- if (thumbnailProcessors != null ) {
235- thumbnailProcessors .forEach (registry ::registerGlobalThumbnailProcessor );
236- }
237- if (validators != null ) {
238- validators .forEach (registry ::registerGlobalValidator );
239- }
240- if (completeProcessors != null ) {
241- completeProcessors .forEach (registry ::registerGlobalCompleteProcessor );
242- }
243- }
202+ public FileValidator fileTypeValidator () {
203+ return new FileTypeValidator ();
244204 }
245205
246206 @ PostConstruct
0 commit comments