Commit 02a8d3decabbdb340ae5be41533355a00af552d5
1 parent
22cc603d
1、添加鲁班前端上传图片,视频,作品封面图对应的后端服务
Showing
9 changed files
with
513 additions
and
15 deletions
src/main/java/com/bsth/luban_springboot2/controller/UploadController.java
| 1 | 1 | package com.bsth.luban_springboot2.controller; |
| 2 | 2 | |
| 3 | -import org.springframework.web.bind.annotation.PostMapping; | |
| 4 | -import org.springframework.web.bind.annotation.RequestMapping; | |
| 5 | -import org.springframework.web.bind.annotation.RequestParam; | |
| 6 | -import org.springframework.web.bind.annotation.RestController; | |
| 3 | +import com.bsth.luban_springboot2.dto.UploadFileDto; | |
| 4 | +import com.bsth.luban_springboot2.dto.request.UploadFileQueryRequest; | |
| 5 | +import com.bsth.luban_springboot2.service.UploadFileService; | |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 7 | +import org.springframework.data.domain.PageRequest; | |
| 8 | +import org.springframework.data.domain.Sort; | |
| 9 | +import org.springframework.http.CacheControl; | |
| 10 | +import org.springframework.http.HttpHeaders; | |
| 11 | +import org.springframework.http.MediaType; | |
| 12 | +import org.springframework.http.ResponseEntity; | |
| 13 | +import org.springframework.web.bind.WebDataBinder; | |
| 14 | +import org.springframework.web.bind.annotation.*; | |
| 15 | +import org.springframework.web.context.request.WebRequest; | |
| 7 | 16 | import org.springframework.web.multipart.MultipartFile; |
| 8 | 17 | |
| 18 | +import javax.validation.Valid; | |
| 19 | +import java.util.ArrayList; | |
| 20 | +import java.util.List; | |
| 21 | +import java.util.concurrent.TimeUnit; | |
| 22 | + | |
| 9 | 23 | /** |
| 10 | 24 | * 上传文件控制器 |
| 11 | 25 | * TODO:swagger标注后面再加 |
| ... | ... | @@ -13,14 +27,80 @@ import org.springframework.web.multipart.MultipartFile; |
| 13 | 27 | @RestController |
| 14 | 28 | @RequestMapping(value = "/upload") |
| 15 | 29 | public class UploadController { |
| 30 | + @Autowired | |
| 31 | + private UploadFileService uploadFileService; | |
| 32 | + | |
| 33 | + @InitBinder | |
| 34 | + protected void initBinder(WebDataBinder binder) { | |
| 35 | + // springMVC请求参数绑定对象属性时,忽略前缀是下划线的名字,如_ocx这种参数 | |
| 36 | + // 1、不使用下划线 | |
| 37 | + // 2、使用initBinder定义没有前缀 | |
| 38 | + binder.setFieldMarkerPrefix(null); | |
| 39 | + } | |
| 16 | 40 | |
| 41 | + // 上传文件 | |
| 17 | 42 | @PostMapping |
| 18 | - public void upload( | |
| 43 | + public List<UploadFileDto> upload( | |
| 19 | 44 | @RequestParam(name = "files") MultipartFile file, |
| 20 | - @RequestParam(name = "workId") Long workId) { | |
| 21 | - // TODO:后面再做上传 | |
| 45 | + @RequestParam(name = "workId", required = false) Long workId) { | |
| 46 | + List<UploadFileDto> rst = new ArrayList<>(); | |
| 47 | + rst.add(this.uploadFileService.upload(file, workId)); | |
| 48 | + return rst; | |
| 49 | + } | |
| 50 | + | |
| 51 | + // 获取文件 | |
| 52 | + @GetMapping(value = "/getFile/{hash}") | |
| 53 | + public ResponseEntity<byte[]> getUploadFile(@PathVariable("hash") String hash, WebRequest webRequest) { | |
| 54 | + if (webRequest.checkNotModified(hash)) { // 使用请求头If-None-Match 比较etag,一致说明未改变,返回null 304 | |
| 55 | + return null; | |
| 56 | + } | |
| 57 | + | |
| 58 | + UploadFileDto uploadFileDto = this.uploadFileService.getUploadFile(hash); | |
| 59 | + // 设置http头 | |
| 60 | + HttpHeaders httpHeaders = new HttpHeaders(); | |
| 61 | + httpHeaders.setCacheControl(CacheControl.maxAge(60L, TimeUnit.SECONDS)); // 设置 cache-control | |
| 62 | + httpHeaders.setContentType(new MediaType(uploadFileDto.getMimeType(), uploadFileDto.getMimeSubType())); // 设置 mime | |
| 63 | + return ResponseEntity | |
| 64 | + .ok() // 注意:如果使用HttpStatus.CREATED,ie下会有问题,HttpStatus.Ok表示服务端已经创建了文件,不是下载文件 | |
| 65 | + .headers(httpHeaders) | |
| 66 | + .eTag(uploadFileDto.getHash()) // 设置 etag | |
| 67 | + .body(uploadFileDto.getBytes()); | |
| 68 | + | |
| 69 | + } | |
| 70 | + | |
| 71 | + class UploadFilesCount { | |
| 72 | + private Long count; | |
| 73 | + | |
| 74 | + public Long getCount() { | |
| 75 | + return count; | |
| 76 | + } | |
| 77 | + | |
| 78 | + public void setCount(Long count) { | |
| 79 | + this.count = count; | |
| 80 | + } | |
| 81 | + } | |
| 82 | + // 文件数量 | |
| 83 | + @RequestMapping(value = "/files/count", method = RequestMethod.GET) | |
| 84 | + public UploadFilesCount countFiles() { | |
| 85 | + UploadFileDto uploadFileDto = UploadFileDto.builder() | |
| 86 | + .cover(Boolean.FALSE) | |
| 87 | + .build(); | |
| 88 | + Long count = this.uploadFileService.count(uploadFileDto); | |
| 89 | + UploadFilesCount uploadFilesCount = new UploadFilesCount(); | |
| 90 | + uploadFilesCount.setCount(count); | |
| 91 | + return uploadFilesCount; | |
| 92 | + } | |
| 22 | 93 | |
| 94 | + @RequestMapping(value = "/files", method = RequestMethod.GET) | |
| 95 | + public List<UploadFileDto> listFiles(@Valid @ModelAttribute UploadFileQueryRequest request) { | |
| 96 | + PageRequest pageRequest = PageRequest.of( | |
| 97 | + request.get_start(), request.get_limit(), | |
| 98 | + Sort.Direction.DESC, "createTime"); | |
| 99 | + UploadFileDto uploadFileDto = UploadFileDto.builder() | |
| 100 | + .cover(Boolean.FALSE) | |
| 101 | + .mime(request.getMime()) | |
| 102 | + .build(); | |
| 103 | + return this.uploadFileService.listFiles(uploadFileDto, pageRequest); | |
| 23 | 104 | |
| 24 | - System.out.println(workId); | |
| 25 | 105 | } |
| 26 | 106 | } | ... | ... |
src/main/java/com/bsth/luban_springboot2/dto/UploadFileDto.java
0 → 100644
| 1 | +package com.bsth.luban_springboot2.dto; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.JsonIgnore; | |
| 4 | +import com.fasterxml.jackson.annotation.JsonProperty; | |
| 5 | +import lombok.Builder; | |
| 6 | +import lombok.Data; | |
| 7 | + | |
| 8 | +import java.io.Serializable; | |
| 9 | +import java.util.Date; | |
| 10 | +import java.util.List; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * 上传文件dto。 | |
| 14 | + */ | |
| 15 | +@Data | |
| 16 | +@Builder | |
| 17 | +public class UploadFileDto implements Serializable { | |
| 18 | + private static final long serialVersionUID = 5081335904680172897L; | |
| 19 | + | |
| 20 | + /** Id */ | |
| 21 | + private Long id; | |
| 22 | + /** 文件名 */ | |
| 23 | + private String name; | |
| 24 | + /** 文件大小 */ | |
| 25 | + private String size; | |
| 26 | + /** 文件名hash值 */ | |
| 27 | + private String hash; | |
| 28 | + /** 文件后缀名 */ | |
| 29 | + private String ext; | |
| 30 | + /** 文件mime类型 */ | |
| 31 | + private String mime; | |
| 32 | + /** 获取文件url */ | |
| 33 | + private String url; | |
| 34 | + /** 创建时间 */ | |
| 35 | + @JsonProperty(value = "created_at") | |
| 36 | + private Date createTime; | |
| 37 | + /** 更新时间 */ | |
| 38 | + @JsonProperty(value = "updated_at") | |
| 39 | + private Date updateTime; | |
| 40 | + | |
| 41 | + /** 以下属性鲁班h5前端可能需要 */ | |
| 42 | + private String provider; | |
| 43 | + @JsonProperty(value = "provider_metadata") | |
| 44 | + private String providerMetadata; | |
| 45 | + private List related; | |
| 46 | + private String sha256; | |
| 47 | + | |
| 48 | + /** 字节 */ | |
| 49 | + @JsonIgnore | |
| 50 | + private byte[] bytes; | |
| 51 | + /** mime主类型 如:image */ | |
| 52 | + @JsonIgnore | |
| 53 | + private String mimeType; | |
| 54 | + /** mime子类型 如:png */ | |
| 55 | + @JsonIgnore | |
| 56 | + private String mimeSubType; | |
| 57 | + /** 是否是作品封面 */ | |
| 58 | + @JsonIgnore | |
| 59 | + private Boolean cover; | |
| 60 | + | |
| 61 | + | |
| 62 | +} | ... | ... |
src/main/java/com/bsth/luban_springboot2/dto/request/UploadFileQueryRequest.java
0 → 100644
| 1 | +package com.bsth.luban_springboot2.dto.request; | |
| 2 | + | |
| 3 | +import lombok.Data; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * 上传文件查询请求request。 | |
| 7 | + */ | |
| 8 | +@Data | |
| 9 | +public class UploadFileQueryRequest { | |
| 10 | + /** 每页记录数 */ | |
| 11 | + private Integer _limit; | |
| 12 | + /** 第几页(从0开始) */ | |
| 13 | + private Integer _start; | |
| 14 | + /** mime类型 */ | |
| 15 | + private String mime; | |
| 16 | +} | ... | ... |
src/main/java/com/bsth/luban_springboot2/entity/UploadFile.java
0 → 100644
| 1 | +package com.bsth.luban_springboot2.entity; | |
| 2 | + | |
| 3 | +import lombok.Data; | |
| 4 | + | |
| 5 | +import javax.persistence.*; | |
| 6 | +import java.io.Serializable; | |
| 7 | +import java.util.Date; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * 上传文件描述entity。 | |
| 11 | + * 手动sql: | |
| 12 | + * DROP TABLE IF EXISTS `upload_file`; | |
| 13 | + * CREATE TABLE `upload_file` ( | |
| 14 | + * `id` int(11) NOT NULL AUTO_INCREMENT, | |
| 15 | + * `name` varchar(255) NOT NULL, | |
| 16 | + * `hash` varchar(255) NOT NULL, | |
| 17 | + * `sha256` varchar(255) DEFAULT NULL, | |
| 18 | + * `ext` varchar(255) DEFAULT NULL, | |
| 19 | + * `mime` varchar(255) NOT NULL, | |
| 20 | + * `size` varchar(255) NOT NULL, | |
| 21 | + * `url` varchar(255) NOT NULL, | |
| 22 | + * `provider` varchar(255) NOT NULL, | |
| 23 | + * `provider_metadata` longtext, | |
| 24 | + * `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, | |
| 25 | + * `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | |
| 26 | + * PRIMARY KEY (`id`), | |
| 27 | + * FULLTEXT KEY `SEARCH_UPLOAD_FILE` (`name`,`hash`,`sha256`,`ext`,`mime`,`size`,`url`,`provider`) | |
| 28 | + * ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
| 29 | + * | |
| 30 | + */ | |
| 31 | +@Data | |
| 32 | +@Entity | |
| 33 | +@Table(name = "upload_file") | |
| 34 | +public class UploadFile implements Serializable { | |
| 35 | + private static final long serialVersionUID = 8914367416120468153L; | |
| 36 | + | |
| 37 | + /** Id */ | |
| 38 | + @Id | |
| 39 | + @GeneratedValue(strategy = GenerationType.AUTO) | |
| 40 | + @Column(length = 11) | |
| 41 | + private Long id; | |
| 42 | + | |
| 43 | + /** 上传的文件名(如:test.png)*/ | |
| 44 | + @Column(nullable = false) | |
| 45 | + private String name; | |
| 46 | + | |
| 47 | + /** 上传的文件名 hash(使用uuid编码)*/ | |
| 48 | + @Column(nullable = false) | |
| 49 | + private String hash; | |
| 50 | + | |
| 51 | + /** TODO:文件编码 */ | |
| 52 | + private String sha256; | |
| 53 | + | |
| 54 | + /** 文件后缀名(如:.png)*/ | |
| 55 | + private String ext; | |
| 56 | + | |
| 57 | + /** 文件mime类型(如:image/png)*/ | |
| 58 | + @Column(nullable = false) | |
| 59 | + private String mime; | |
| 60 | + | |
| 61 | + /** 文件大小,单位k(如:24.5)*/ | |
| 62 | + @Column(nullable = false) | |
| 63 | + private String size; | |
| 64 | + | |
| 65 | + /** 获取文件url(如:/upload/124455.png)*/ | |
| 66 | + @Column(nullable = false) | |
| 67 | + private String url; | |
| 68 | + | |
| 69 | + /** TODO:默认写local,strapi中使用的 */ | |
| 70 | + @Column(nullable = false) | |
| 71 | + private String provider = "local"; | |
| 72 | + /** TODO:和provier配对,strapi中使用的 */ | |
| 73 | + @Lob | |
| 74 | + private String providerMetadata; | |
| 75 | + | |
| 76 | + /** 自定义的新字段 */ | |
| 77 | + @Lob | |
| 78 | + @Column(nullable = false) | |
| 79 | + private String realPath; // 后端保存的真实路径 | |
| 80 | + | |
| 81 | + @Column(nullable = false) | |
| 82 | + private Boolean cover = Boolean.FALSE; // 是否是作品封面图 | |
| 83 | + | |
| 84 | + /** 创建时间 */ | |
| 85 | + @Temporal(TemporalType.TIMESTAMP) | |
| 86 | + @Column(nullable = false) | |
| 87 | + private Date createTime = new Date(); | |
| 88 | + | |
| 89 | + /** 更新时间 */ | |
| 90 | + @Temporal(TemporalType.TIMESTAMP) | |
| 91 | + @Column(nullable = false) | |
| 92 | + private Date updateTime = new Date(); | |
| 93 | +} | ... | ... |
src/main/java/com/bsth/luban_springboot2/repo/UploadFileRepo.java
0 → 100644
| 1 | +package com.bsth.luban_springboot2.repo; | |
| 2 | + | |
| 3 | +import com.bsth.luban_springboot2.entity.UploadFile; | |
| 4 | +import org.springframework.stereotype.Repository; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * 上传文件Repo。 | |
| 8 | + */ | |
| 9 | +@Repository | |
| 10 | +public interface UploadFileRepo extends BaseRepository<UploadFile, Long> { | |
| 11 | + | |
| 12 | + UploadFile findByHash(String hash); | |
| 13 | +} | ... | ... |
src/main/java/com/bsth/luban_springboot2/service/UploadFileService.java
0 → 100644
| 1 | +package com.bsth.luban_springboot2.service; | |
| 2 | + | |
| 3 | +import com.bsth.luban_springboot2.dto.UploadFileDto; | |
| 4 | +import org.springframework.data.domain.Pageable; | |
| 5 | +import org.springframework.web.multipart.MultipartFile; | |
| 6 | + | |
| 7 | +import java.util.List; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * 上传文件Service。 | |
| 11 | + */ | |
| 12 | +public interface UploadFileService { | |
| 13 | + /** | |
| 14 | + * 上传文件。 | |
| 15 | + * @param file | |
| 16 | + * @param workId 作品Id | |
| 17 | + * @return | |
| 18 | + */ | |
| 19 | + UploadFileDto upload(MultipartFile file, Long workId); | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * 获取上传文件 | |
| 23 | + * @param hash 文件名hash值 | |
| 24 | + * @return | |
| 25 | + */ | |
| 26 | + UploadFileDto getUploadFile(String hash); | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * 获取文件数量 | |
| 30 | + * @param uploadFileDto | |
| 31 | + * @return | |
| 32 | + */ | |
| 33 | + Long count(UploadFileDto uploadFileDto); | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * 分页查询文件。 | |
| 37 | + * @param uploadFileDto | |
| 38 | + * @param pageable | |
| 39 | + * @return | |
| 40 | + */ | |
| 41 | + List<UploadFileDto> listFiles(UploadFileDto uploadFileDto, Pageable pageable); | |
| 42 | +} | ... | ... |
src/main/java/com/bsth/luban_springboot2/service/impl/UploadFileServiceImpl.java
0 → 100644
| 1 | +package com.bsth.luban_springboot2.service.impl; | |
| 2 | + | |
| 3 | +import com.bsth.luban_springboot2.dto.UploadFileDto; | |
| 4 | +import com.bsth.luban_springboot2.entity.UploadFile; | |
| 5 | +import com.bsth.luban_springboot2.repo.UploadFileRepo; | |
| 6 | +import com.bsth.luban_springboot2.service.UploadFileService; | |
| 7 | +import org.slf4j.Logger; | |
| 8 | +import org.slf4j.LoggerFactory; | |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | +import org.springframework.beans.factory.annotation.Value; | |
| 11 | +import org.springframework.data.domain.Example; | |
| 12 | +import org.springframework.data.domain.ExampleMatcher; | |
| 13 | +import org.springframework.data.domain.Page; | |
| 14 | +import org.springframework.data.domain.Pageable; | |
| 15 | +import org.springframework.stereotype.Service; | |
| 16 | +import org.springframework.transaction.annotation.Isolation; | |
| 17 | +import org.springframework.transaction.annotation.Propagation; | |
| 18 | +import org.springframework.transaction.annotation.Transactional; | |
| 19 | +import org.springframework.web.multipart.MultipartFile; | |
| 20 | + | |
| 21 | +import java.io.File; | |
| 22 | +import java.nio.file.Files; | |
| 23 | +import java.nio.file.Paths; | |
| 24 | +import java.util.ArrayList; | |
| 25 | +import java.util.List; | |
| 26 | +import java.util.UUID; | |
| 27 | + | |
| 28 | +/** | |
| 29 | + * 上传文件服务实现。 | |
| 30 | + */ | |
| 31 | +@Service | |
| 32 | +public class UploadFileServiceImpl implements UploadFileService { | |
| 33 | + /** 日志记录器 */ | |
| 34 | + private final static Logger LOG = LoggerFactory.getLogger(UploadFileServiceImpl.class); | |
| 35 | + | |
| 36 | + @Autowired | |
| 37 | + private UploadFileRepo uploadFileRepo; | |
| 38 | + | |
| 39 | + @Value("${luban_h5.upload.path}") | |
| 40 | + private String uploadPath; | |
| 41 | + @Value("${luban_h5.upload_url.prefix}") | |
| 42 | + private String upload_file_url_prefix; | |
| 43 | + | |
| 44 | + @Override | |
| 45 | + @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED) | |
| 46 | + public UploadFileDto upload(MultipartFile file, Long workId) { | |
| 47 | + File uploadPath_dir = Paths.get(this.uploadPath).toFile(); | |
| 48 | + if (!uploadPath_dir.exists()) { | |
| 49 | + throw new RuntimeException("配置的服务端上传路径不存在,请管理员后台创建!"); | |
| 50 | + } | |
| 51 | + if (!uploadPath_dir.isDirectory()) { | |
| 52 | + throw new RuntimeException("配置的服务端路径不是目录,请重新配置!"); | |
| 53 | + } | |
| 54 | + | |
| 55 | + UploadFile uploadFile = new UploadFile(); | |
| 56 | + uploadFile.setName(file.getOriginalFilename()); | |
| 57 | + uploadFile.setHash(UUID.randomUUID().toString().replaceAll("-", "")); // 去除UUID中的- | |
| 58 | + uploadFile.setExt(uploadFile.getName().substring(uploadFile.getName().lastIndexOf("."))); | |
| 59 | + uploadFile.setMime(file.getContentType()); | |
| 60 | + uploadFile.setSize(Double.toString(file.getSize() / 1000)); | |
| 61 | + uploadFile.setUrl(Paths.get(upload_file_url_prefix, uploadFile.getHash()).toString()); | |
| 62 | + | |
| 63 | + try { | |
| 64 | + File localFilePath = Paths.get(uploadPath, uploadFile.getHash() + uploadFile.getExt()).toFile(); | |
| 65 | + file.transferTo(localFilePath); | |
| 66 | + uploadFile.setRealPath(localFilePath.toString()); // 后端保存的路径 | |
| 67 | + } catch (Exception exp) { | |
| 68 | + exp.printStackTrace(); | |
| 69 | + throw new RuntimeException("上传文件失败!", exp); | |
| 70 | + } | |
| 71 | + | |
| 72 | + // 确定是否封面图 | |
| 73 | + if (workId == null) { | |
| 74 | + uploadFile.setCover(Boolean.FALSE); | |
| 75 | + } else { | |
| 76 | + uploadFile.setCover(Boolean.TRUE); | |
| 77 | + } | |
| 78 | + | |
| 79 | + uploadFile = uploadFileRepo.save(uploadFile); | |
| 80 | + | |
| 81 | + UploadFileDto uploadFileDto = UploadFileDto.builder() | |
| 82 | + .id(uploadFile.getId()) | |
| 83 | + .name(uploadFile.getName()) | |
| 84 | + .size(uploadFile.getSize()) | |
| 85 | + .hash(uploadFile.getHash()) | |
| 86 | + .ext(uploadFile.getExt()) | |
| 87 | + .mime(uploadFile.getMime()) | |
| 88 | + .url(uploadFile.getUrl()) | |
| 89 | + .createTime(uploadFile.getCreateTime()) | |
| 90 | + .updateTime(uploadFile.getUpdateTime()) | |
| 91 | + .provider(uploadFile.getProvider()) | |
| 92 | + .providerMetadata(uploadFile.getProviderMetadata()) | |
| 93 | + .related(new ArrayList()) | |
| 94 | + .sha256(uploadFile.getSha256()) | |
| 95 | + .build(); | |
| 96 | + | |
| 97 | + return uploadFileDto; | |
| 98 | + } | |
| 99 | + | |
| 100 | + @Override | |
| 101 | + public UploadFileDto getUploadFile(String hash) { | |
| 102 | + UploadFile uploadFile = this.uploadFileRepo.findByHash(hash); | |
| 103 | + if (uploadFile == null) { | |
| 104 | + LOG.warn("上传文件hash={},不存在!", hash); | |
| 105 | + return UploadFileDto.builder().build(); | |
| 106 | + } | |
| 107 | + | |
| 108 | + // 1、获取mime类型 | |
| 109 | + String[] types = uploadFile.getMime().split("/"); | |
| 110 | + String mimeType = types[0]; | |
| 111 | + String mimeSubType = types[1]; | |
| 112 | + | |
| 113 | + // 2、获取上传文件bytes | |
| 114 | + byte[] uploadFileBytes = null; | |
| 115 | + File file = new File(uploadFile.getRealPath()); | |
| 116 | + if (!file.exists()) { | |
| 117 | + LOG.warn("上传文件realPath={},不存在!", uploadFile.getRealPath()); | |
| 118 | + return UploadFileDto.builder().build(); | |
| 119 | + } else { | |
| 120 | + try { | |
| 121 | + uploadFileBytes = Files.readAllBytes(file.toPath()); | |
| 122 | + } catch (Exception exp) { | |
| 123 | + exp.printStackTrace(); | |
| 124 | + } | |
| 125 | + | |
| 126 | + return UploadFileDto.builder() | |
| 127 | + .name(uploadFile.getName()) | |
| 128 | + .hash(uploadFile.getHash()) | |
| 129 | + .bytes(uploadFileBytes) | |
| 130 | + .mimeType(mimeType) | |
| 131 | + .mimeSubType(mimeSubType) | |
| 132 | + .build(); | |
| 133 | + } | |
| 134 | + | |
| 135 | + | |
| 136 | + } | |
| 137 | + | |
| 138 | + @Override | |
| 139 | + public Long count(UploadFileDto uploadFileDto) { | |
| 140 | + // 不包括作品封面图 | |
| 141 | + UploadFile uploadFile = new UploadFile(); | |
| 142 | + uploadFile.setCover(uploadFileDto.getCover()); | |
| 143 | + uploadFile.setCreateTime(null); | |
| 144 | + uploadFile.setUpdateTime(null); | |
| 145 | + Example<UploadFile> example = Example.of(uploadFile); | |
| 146 | + return this.uploadFileRepo.count(example); | |
| 147 | + } | |
| 148 | + | |
| 149 | + @Override | |
| 150 | + public List<UploadFileDto> listFiles(UploadFileDto uploadFileDto, Pageable pageable) { | |
| 151 | + // 不包括作品封面图 | |
| 152 | + UploadFile uploadFile = new UploadFile(); | |
| 153 | + uploadFile.setCover(uploadFileDto.getCover()); | |
| 154 | + uploadFile.setMime(uploadFileDto.getMime() == null ? "image" : uploadFileDto.getMime()); | |
| 155 | + uploadFile.setCreateTime(null); | |
| 156 | + uploadFile.setUpdateTime(null); | |
| 157 | + ExampleMatcher matcher = ExampleMatcher.matching() | |
| 158 | + .withMatcher("mime", ExampleMatcher.GenericPropertyMatchers.startsWith()); // like {mime}% | |
| 159 | + Example<UploadFile> example = Example.of(uploadFile, matcher); | |
| 160 | + Page<UploadFile> uploadFilePage = this.uploadFileRepo.findAll(example, pageable); | |
| 161 | + Page<UploadFileDto> uploadFileDtoPage = uploadFilePage.map((uploadFile_source) -> { | |
| 162 | + UploadFileDto uploadFileDto_covert = UploadFileDto.builder() | |
| 163 | + .id(uploadFile_source.getId()) | |
| 164 | + .name(uploadFile_source.getName()) | |
| 165 | + .size(uploadFile_source.getSize()) | |
| 166 | + .hash(uploadFile_source.getHash()) | |
| 167 | + .ext(uploadFile_source.getExt()) | |
| 168 | + .mime(uploadFile_source.getMime()) | |
| 169 | + .url(uploadFile_source.getUrl()) | |
| 170 | + .createTime(uploadFile_source.getCreateTime()) | |
| 171 | + .updateTime(uploadFile_source.getUpdateTime()) | |
| 172 | + .provider(uploadFile_source.getProvider()) | |
| 173 | + .providerMetadata(uploadFile_source.getProviderMetadata()) | |
| 174 | + .related(new ArrayList()) | |
| 175 | + .sha256(uploadFile_source.getSha256()) | |
| 176 | + .build(); | |
| 177 | + return uploadFileDto_covert; | |
| 178 | + }); | |
| 179 | + | |
| 180 | + return uploadFileDtoPage.getContent(); | |
| 181 | + } | |
| 182 | +} | ... | ... |
src/main/resources/application-db.properties
| ... | ... | @@ -15,9 +15,9 @@ spring.config.activate.on-profile=mysql_db_config |
| 15 | 15 | # 数据库url连接字符串 |
| 16 | 16 | # serverTimezone参数解决数据库时区不一致的问题 |
| 17 | 17 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver |
| 18 | -#spring.datasource.url=jdbc:mysql://127.0.0.1/luban_h5?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8 | |
| 19 | -#spring.datasource.username=root | |
| 20 | -#spring.datasource.password= | |
| 21 | -spring.datasource.url=jdbc:mysql://192.168.168.169/bsth-ebus_stop_board?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8 | |
| 18 | +spring.datasource.url=jdbc:mysql://127.0.0.1/luban_h5?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8 | |
| 22 | 19 | spring.datasource.username=root |
| 23 | -spring.datasource.password=root2jsp | |
| 20 | +spring.datasource.password= | |
| 21 | +#spring.datasource.url=jdbc:mysql://192.168.168.169/bsth-ebus_stop_board?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8 | |
| 22 | +#spring.datasource.username=root | |
| 23 | +#spring.datasource.password=root2jsp | ... | ... |
src/main/resources/application.properties
| ... | ... | @@ -22,11 +22,21 @@ spring.profiles.active=prod |
| 22 | 22 | logging.level.com.zaxxer.hikari.HikariConfig=DEBUG |
| 23 | 23 | # logging.level.com.zaxxer.hikari=TRACE |
| 24 | 24 | |
| 25 | +# 文件上传单个文件大小 | |
| 26 | +spring.servlet.multipart.enabled=true | |
| 27 | +spring.servlet.multipart.file-size-threshold=0 | |
| 28 | +spring.servlet.multipart.max-file-size=100MB | |
| 29 | +spring.servlet.multipart.max-request-size=100MB | |
| 30 | + | |
| 25 | 31 | #--------------- 鲁班h5相关操作配置 ------------# |
| 26 | 32 | ## zip打包来源文件根路径(目录) |
| 27 | 33 | luban_h5.zip_page.source_root_path=/Users/xu/resource/project_code/bsth_project/bsth_ebus/luban_springboot2/src/main/resources/static |
| 28 | 34 | ## 目标zip文件存放路径(目录) |
| 29 | -luban_h5.zip_page.dest_path=/Users/xu/resource/project_code/runtime_temp/zip_pages | |
| 35 | +luban_h5.zip_page.dest_path=/Users/xu/resource/project_code/bsth_project/bsth_ebus/temp_files/zip_pages | |
| 36 | +## 上传文件的存放路径 | |
| 37 | +luban_h5.upload.path=/Users/xu/resource/project_code/bsth_project/bsth_ebus/temp_files/upload_files | |
| 38 | +## 上传文件获取url前缀 | |
| 39 | +luban_h5.upload_url.prefix=/upload/getFile | |
| 30 | 40 | |
| 31 | 41 | #luban_h5.zip_page.source_root_path=D:/apache-tomcat-8.5.64/webapps/lubanh5/WEB-INF/classes/static |
| 32 | -#luban_h5.zip_page.dest_path=D:/zip_pages | |
| 33 | 42 | \ No newline at end of file |
| 43 | +#luban_h5.zip_page.dest_path=D:/zip_pages | ... | ... |