LocalStorageServiceImpl.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2019-2020 Zheng Jie
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package me.zhengjie.service.impl;
  17. import cn.hutool.core.util.ObjectUtil;
  18. import lombok.RequiredArgsConstructor;
  19. import me.zhengjie.config.FileProperties;
  20. import me.zhengjie.domain.LocalStorage;
  21. import me.zhengjie.service.dto.LocalStorageDto;
  22. import me.zhengjie.service.dto.LocalStorageQueryCriteria;
  23. import me.zhengjie.service.mapstruct.LocalStorageMapper;
  24. import me.zhengjie.exception.BadRequestException;
  25. import me.zhengjie.utils.*;
  26. import me.zhengjie.repository.LocalStorageRepository;
  27. import me.zhengjie.service.LocalStorageService;
  28. import org.springframework.beans.factory.annotation.Value;
  29. import org.springframework.stereotype.Service;
  30. import org.springframework.transaction.annotation.Transactional;
  31. import java.io.File;
  32. import java.io.IOException;
  33. import java.util.ArrayList;
  34. import java.util.LinkedHashMap;
  35. import java.util.List;
  36. import java.util.Map;
  37. import org.springframework.data.domain.Page;
  38. import org.springframework.data.domain.Pageable;
  39. import org.springframework.web.multipart.MultipartFile;
  40. import javax.servlet.http.HttpServletResponse;
  41. /**
  42. * @author Zheng Jie
  43. * @date 2019-09-05
  44. */
  45. @Service
  46. @RequiredArgsConstructor
  47. public class LocalStorageServiceImpl implements LocalStorageService {
  48. @Value("${localUrl}")
  49. private String localUrl;
  50. private final LocalStorageRepository localStorageRepository;
  51. private final LocalStorageMapper localStorageMapper;
  52. private final FileProperties properties;
  53. @Override
  54. public Object queryAll(LocalStorageQueryCriteria criteria, Pageable pageable){
  55. Page<LocalStorage> page = localStorageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
  56. return PageUtil.toPage(page.map(localStorageMapper::toDto));
  57. }
  58. @Override
  59. public List<LocalStorageDto> queryAll(LocalStorageQueryCriteria criteria){
  60. return localStorageMapper.toDto(localStorageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
  61. }
  62. @Override
  63. public LocalStorageDto findById(Long id){
  64. LocalStorage localStorage = localStorageRepository.findById(id).orElseGet(LocalStorage::new);
  65. ValidationUtil.isNull(localStorage.getId(),"LocalStorage","id",id);
  66. return localStorageMapper.toDto(localStorage);
  67. }
  68. @Override
  69. @Transactional(rollbackFor = Exception.class)
  70. public LocalStorage create(String name, MultipartFile multipartFile) {
  71. FileUtil.checkSize(properties.getMaxSize(), multipartFile.getSize());
  72. String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
  73. String type = FileUtil.getFileType(suffix);
  74. File file = FileUtil.upload(multipartFile, properties.getPath().getPath() + File.separator);
  75. if(ObjectUtil.isNull(file)){
  76. throw new BadRequestException("上传失败");
  77. }
  78. String url = localUrl + file.getName();
  79. try {
  80. name = StringUtils.isBlank(name) ? FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename()) : name;
  81. LocalStorage localStorage = new LocalStorage(
  82. file.getName(),
  83. name,
  84. suffix,
  85. file.getPath(),
  86. url,
  87. type,
  88. FileUtil.getSize(multipartFile.getSize())
  89. );
  90. return localStorageRepository.save(localStorage);
  91. }catch (Exception e){
  92. FileUtil.del(file);
  93. throw e;
  94. }
  95. }
  96. @Override
  97. @Transactional(rollbackFor = Exception.class)
  98. public void update(LocalStorage resources) {
  99. LocalStorage localStorage = localStorageRepository.findById(resources.getId()).orElseGet(LocalStorage::new);
  100. ValidationUtil.isNull( localStorage.getId(),"LocalStorage","id",resources.getId());
  101. localStorage.copy(resources);
  102. localStorageRepository.save(localStorage);
  103. }
  104. @Override
  105. @Transactional(rollbackFor = Exception.class)
  106. public void deleteAll(Long[] ids) {
  107. for (Long id : ids) {
  108. LocalStorage storage = localStorageRepository.findById(id).orElseGet(LocalStorage::new);
  109. FileUtil.del(storage.getPath());
  110. localStorageRepository.delete(storage);
  111. }
  112. }
  113. @Override
  114. public void download(List<LocalStorageDto> queryAll, HttpServletResponse response) throws IOException {
  115. List<Map<String, Object>> list = new ArrayList<>();
  116. for (LocalStorageDto localStorageDTO : queryAll) {
  117. Map<String,Object> map = new LinkedHashMap<>();
  118. map.put("文件名", localStorageDTO.getRealName());
  119. map.put("备注名", localStorageDTO.getName());
  120. map.put("文件类型", localStorageDTO.getType());
  121. map.put("文件大小", localStorageDTO.getSize());
  122. map.put("创建者", localStorageDTO.getCreateBy());
  123. map.put("创建日期", localStorageDTO.getCreateTime());
  124. list.add(map);
  125. }
  126. FileUtil.downloadExcel(list, response);
  127. }
  128. }