first commit
							parent
							
								
									c91a7a7919
								
							
						
					
					
						commit
						3ee856a360
					
				| @ -0,0 +1,13 @@ | ||||
| package org.ils.file; | ||||
| 
 | ||||
| /** | ||||
|  * Hello world! | ||||
|  * | ||||
|  */ | ||||
| public class App  | ||||
| { | ||||
|     public static void main( String[] args ) | ||||
|     { | ||||
|         System.out.println( "Hello World!" ); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,18 @@ | ||||
| package org.ils.file; | ||||
| 
 | ||||
| import cn.xuyanwu.spring.file.storage.spring.EnableFileStorage; | ||||
| import org.springframework.boot.SpringApplication; | ||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
| 
 | ||||
| /** | ||||
|  * @author chenjf | ||||
|  * @datetime 2023/08/07 | ||||
|  */ | ||||
| @EnableFileStorage | ||||
| @SpringBootApplication | ||||
| public class IlsFileStorageTestApplication { | ||||
| 
 | ||||
|     public static void main(String[] args) { | ||||
|         SpringApplication.run(IlsFileStorageTestApplication.class,args); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,34 @@ | ||||
| package org.ils.file.config; | ||||
| 
 | ||||
| 
 | ||||
| import com.baomidou.mybatisplus.annotation.DbType; | ||||
| import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; | ||||
| import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; | ||||
| import org.mybatis.spring.annotation.MapperScan; | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| import org.springframework.transaction.annotation.EnableTransactionManagement; | ||||
| 
 | ||||
| import java.util.Collections; | ||||
| 
 | ||||
| @EnableTransactionManagement | ||||
| @Configuration | ||||
| @MapperScan("org.ils.file.mapper") | ||||
| public class MyBatisPlusConfig { | ||||
|     @Bean | ||||
|     public PaginationInnerInterceptor paginationInnerInterceptor() { | ||||
|         PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor(); | ||||
|         // 设置最大单页限制数量,默认 500 条,-1 不受限制
 | ||||
|         paginationInterceptor.setMaxLimit(-1L); | ||||
|         paginationInterceptor.setDbType(DbType.MYSQL); | ||||
|         // 开启 count 的 join 优化,只针对部分 left join
 | ||||
|         paginationInterceptor.setOptimizeJoin(true); | ||||
|         return paginationInterceptor; | ||||
|     } | ||||
|     @Bean | ||||
|     public MybatisPlusInterceptor mybatisPlusInterceptor(){ | ||||
|         MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); | ||||
|         mybatisPlusInterceptor.setInterceptors(Collections.singletonList(paginationInnerInterceptor())); | ||||
|         return mybatisPlusInterceptor; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,16 @@ | ||||
| package org.ils.file.config; | ||||
| 
 | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| import org.springframework.web.client.RestTemplate; | ||||
| 
 | ||||
| /** | ||||
|  * @author chenjf | ||||
|  */ | ||||
| @Configuration | ||||
| public class RestTemplateConfig { | ||||
|     @Bean | ||||
|     public RestTemplate restTemplate() { | ||||
|         return new RestTemplate(); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,70 @@ | ||||
| package org.ils.file.config; | ||||
| 
 | ||||
| 
 | ||||
| import io.swagger.annotations.ApiOperation; | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
| import springfox.documentation.builders.ParameterBuilder; | ||||
| import springfox.documentation.builders.PathSelectors; | ||||
| import springfox.documentation.builders.RequestHandlerSelectors; | ||||
| import springfox.documentation.schema.ModelRef; | ||||
| import springfox.documentation.service.ApiInfo; | ||||
| import springfox.documentation.service.Parameter; | ||||
| import springfox.documentation.spi.DocumentationType; | ||||
| import springfox.documentation.spring.web.plugins.Docket; | ||||
| import springfox.documentation.swagger2.annotations.EnableSwagger2; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * | ||||
|  * @author chenjf | ||||
|  */ | ||||
| 
 | ||||
| @Configuration | ||||
| @EnableSwagger2 | ||||
| public class SwaggerConfig { | ||||
| 
 | ||||
|     //配置了 Swagger 的Docket的bean实例
 | ||||
|     @Bean | ||||
|     public Docket docket(){ | ||||
| 
 | ||||
|         ParameterBuilder ticketPar = new ParameterBuilder(); | ||||
|         List<Parameter> pars = new ArrayList<>(); | ||||
|         ticketPar.name("Authorization").description("token") | ||||
|                 .modelRef(new ModelRef("string")).parameterType("header") | ||||
|                 .required(false).build(); | ||||
|         pars.add(ticketPar.build()); | ||||
| 
 | ||||
|         return new Docket(DocumentationType.SWAGGER_2) | ||||
|                 .apiInfo(apiInfo()) | ||||
|                 .select() | ||||
|                 //RequestHandlerSelectors, 配置要扫描接口的方式
 | ||||
|                 //basePackage:指定要扫描的包
 | ||||
|                 //any():扫描全部
 | ||||
|                 //withClassAnnotation: 扫描类上的注解
 | ||||
|                 //withMethodAnnotation: 扫描方法上的注解
 | ||||
|                 .apis(RequestHandlerSelectors.basePackage("org.ils.file")) | ||||
|                 .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) | ||||
|                 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) | ||||
|                 .paths(PathSelectors.any()) | ||||
|                 .build() | ||||
|                 .globalOperationParameters(pars); | ||||
|     } | ||||
| 
 | ||||
|     //配置Swagger 信息=apiInfo
 | ||||
|     private ApiInfo apiInfo(){ | ||||
|         return new ApiInfo( | ||||
|                 "ils fileStorage Api", | ||||
|                 "ils文件上传下载接口", | ||||
|                 "v1.0", | ||||
|                 "127.0.0.1:9098/", | ||||
|                 null,//contact
 | ||||
|                 "Apache 2.0", | ||||
|                 "http://www.apache.org/licenses/LICENSE-2.0", | ||||
|                 new ArrayList() | ||||
|         ); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,24 @@ | ||||
| package org.ils.file.config; | ||||
| 
 | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | ||||
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||||
| 
 | ||||
| /** | ||||
|  * @author chenjf | ||||
|  */ | ||||
| 
 | ||||
| 
 | ||||
| @Configuration | ||||
| public class WebMvcConfig  implements WebMvcConfigurer { | ||||
|     @Override | ||||
|     public void addResourceHandlers(ResourceHandlerRegistry registry) { | ||||
|         registry.addResourceHandler("/**").addResourceLocations("classpath:/static/"); | ||||
|         registry.addResourceHandler("swagger-ui.html") | ||||
|                 .addResourceLocations("classpath:/META-INF/resources/"); | ||||
|         registry.addResourceHandler("/webjars/**") | ||||
|                 .addResourceLocations("classpath:/META-INF/resources/webjars/"); | ||||
|         registry.addResourceHandler("doc.html") | ||||
|                 .addResourceLocations("classpath:/META-INF/resources/"); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,82 @@ | ||||
| package org.ils.file.entity; | ||||
| 
 | ||||
| import cn.hutool.core.date.DateTime; | ||||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||||
| import io.swagger.annotations.ApiModelProperty; | ||||
| import lombok.Data; | ||||
| import lombok.NoArgsConstructor; | ||||
| 
 | ||||
| import java.io.Serializable; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| /** | ||||
|  * @author chenjf | ||||
|  * @datatime 2023/08/07 | ||||
|  */ | ||||
| @Data | ||||
| @NoArgsConstructor | ||||
| public class FileDetail implements Serializable { | ||||
| 
 | ||||
|     @ApiModelProperty(value = "主键id",  name = "id" ) | ||||
|     private String id; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "文件访问地址",  name = "url " ) | ||||
|     private String url; | ||||
| 
 | ||||
| 
 | ||||
|     @ApiModelProperty(value = "文件大小,单位字节",  name = "size" ) | ||||
|     private Long size; | ||||
| 
 | ||||
| 
 | ||||
|     @ApiModelProperty(value = "文件名称",  name = "filename" ) | ||||
|     private String filename; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "原始文件名",  name = "originalFilename" ) | ||||
|     private String originalFilename; | ||||
| 
 | ||||
| 
 | ||||
|     @ApiModelProperty(value = "基础存储路径",  name = "basePath" ) | ||||
|     private String basePath; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "存储路径",  name = "path" ) | ||||
|     private String path; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "文件扩展名",  name = "ext" ) | ||||
|     private String ext; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "MIME类型",  name = "contentType" ) | ||||
|     private String contentType; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "存储平台",  name = "platform" ) | ||||
|     private String platform; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "缩略图访问路径",  name = "thUrl" ) | ||||
|     private String thUrl; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "缩略图名称",  name = "thFilename" ) | ||||
|     private String thFilename; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "缩略图大小,单位字节",  name = "thSize" ) | ||||
|     private Long thSize; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "缩略图MIME类型",  name = "thContentType" ) | ||||
|     private String thContentType; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "文件所属对象id",  name = "objectId" ) | ||||
|     private String objectId; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "文件所属对象类型,例如用户头像,评价图片",  name = "objectType" ) | ||||
|     private String objectType; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "附加属性",  name = "attr" ) | ||||
|     private String attr; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "文件ACL",  name = "fileAcl" ) | ||||
|     private String fileAcl; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "缩略图文件ACL",  name = "thFileAcl" ) | ||||
|     private String thFileAcl; | ||||
| 
 | ||||
|     @ApiModelProperty(value = "创建时间",  name = "createTime" ) | ||||
|     private Date createTime; | ||||
| } | ||||
| @ -0,0 +1,9 @@ | ||||
| package org.ils.file.mapper; | ||||
| 
 | ||||
| import com.baomidou.mybatisplus.core.mapper.BaseMapper; | ||||
| import org.apache.ibatis.annotations.Mapper; | ||||
| import org.ils.file.entity.FileDetail; | ||||
| 
 | ||||
| @Mapper | ||||
| public interface FileDetailMapper extends BaseMapper<FileDetail> { | ||||
| } | ||||
| @ -0,0 +1,66 @@ | ||||
| package org.ils.file.service.impl; | ||||
| 
 | ||||
| import cn.hutool.core.bean.BeanUtil; | ||||
| import cn.hutool.core.lang.Dict; | ||||
| import cn.hutool.core.util.StrUtil; | ||||
| import cn.xuyanwu.spring.file.storage.FileInfo; | ||||
| import cn.xuyanwu.spring.file.storage.recorder.FileRecorder; | ||||
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||||
| import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | ||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||
| import lombok.SneakyThrows; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.ils.file.entity.FileDetail; | ||||
| import org.ils.file.mapper.FileDetailMapper; | ||||
| import org.springframework.stereotype.Service; | ||||
| 
 | ||||
| 
 | ||||
| /** | ||||
|  * 基础 service | ||||
|  * @author chenjf | ||||
|  */ | ||||
| @Slf4j | ||||
| @Service | ||||
| public class FileDetailService extends ServiceImpl<FileDetailMapper, FileDetail> implements FileRecorder { | ||||
| 
 | ||||
| 
 | ||||
|     @SneakyThrows | ||||
|     @Override | ||||
|     public boolean save(FileInfo info){ | ||||
|         FileDetail detail = BeanUtil.copyProperties(info, FileDetail.class,"attr"); | ||||
| 
 | ||||
|         //这是手动获 取附加属性字典 并转成 json 字符串,方便存储在数据库中
 | ||||
|         if (info.getAttr() != null) { | ||||
|             detail.setAttr(new ObjectMapper().writeValueAsString(info.getAttr())); | ||||
|         } | ||||
|         boolean  b = save(detail); | ||||
|         if (b) { | ||||
|             info.setId(detail.getId()); | ||||
|         } | ||||
|         return b; | ||||
|     } | ||||
|     /** | ||||
|      * 根据 url 查询文件信息 | ||||
|      */ | ||||
|     @SneakyThrows | ||||
|     @Override | ||||
|     public FileInfo getByUrl(String url) { | ||||
|         FileDetail detail = getOne(new QueryWrapper<FileDetail>().eq("url", url)); | ||||
|         FileInfo info = BeanUtil.copyProperties(detail, FileInfo.class,"attr"); | ||||
| 
 | ||||
|         //这是手动获取数据库中的 json 字符串 并转成 附加属性字典,方便使用
 | ||||
|         if (StrUtil.isNotBlank(detail.getAttr())) { | ||||
|             info.setAttr(new ObjectMapper().readValue(detail.getAttr(), Dict.class)); | ||||
|         } | ||||
|         return info; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 根据 url 删除文件信息 | ||||
|      */ | ||||
|     @Override | ||||
|     public boolean delete(String url) { | ||||
|         return remove(new QueryWrapper<FileDetail>().eq("url", url)); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,5 @@ | ||||
| spring: | ||||
|   application: | ||||
|     name: ils-file-storage | ||||
|   profiles: | ||||
|     active: 'dev' | ||||
| @ -0,0 +1,38 @@ | ||||
| package org.ils.file; | ||||
| 
 | ||||
| import junit.framework.Test; | ||||
| import junit.framework.TestCase; | ||||
| import junit.framework.TestSuite; | ||||
| 
 | ||||
| /** | ||||
|  * Unit test for simple App. | ||||
|  */ | ||||
| public class AppTest  | ||||
|     extends TestCase | ||||
| { | ||||
|     /** | ||||
|      * Create the test case | ||||
|      * | ||||
|      * @param testName name of the test case | ||||
|      */ | ||||
|     public AppTest( String testName ) | ||||
|     { | ||||
|         super( testName ); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * @return the suite of tests being tested | ||||
|      */ | ||||
|     public static Test suite() | ||||
|     { | ||||
|         return new TestSuite( AppTest.class ); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Rigourous Test :-) | ||||
|      */ | ||||
|     public void testApp() | ||||
|     { | ||||
|         assertTrue( true ); | ||||
|     } | ||||
| } | ||||
					Loading…
					
					
				
		Reference in New Issue