LocalStorageServiceImpl.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.stereotype.Service;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import java.io.File;
  31. import java.io.IOException;
  32. import java.util.ArrayList;
  33. import java.util.LinkedHashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. import org.springframework.data.domain.Page;
  37. import org.springframework.data.domain.Pageable;
  38. import org.springframework.web.multipart.MultipartFile;
  39. import javax.servlet.http.HttpServletResponse;
  40. /**
  41. * @author Zheng Jie
  42. * @date 2019-09-05
  43. */
  44. @Service
  45. @RequiredArgsConstructor
  46. public class LocalStorageServiceImpl implements LocalStorageService {
  47. private final LocalStorageRepository localStorageRepository;
  48. private final LocalStorageMapper localStorageMapper;
  49. private final FileProperties properties;
  50. @Override
  51. public Object queryAll(LocalStorageQueryCriteria criteria, Pageable pageable){
  52. Page<LocalStorage> page = localStorageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
  53. return PageUtil.toPage(page.map(localStorageMapper::toDto));
  54. }
  55. @Override
  56. public List<LocalStorageDto> queryAll(LocalStorageQueryCriteria criteria){
  57. return localStorageMapper.toDto(localStorageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
  58. }
  59. @Override
  60. public LocalStorageDto findById(Long id){
  61. LocalStorage localStorage = localStorageRepository.findById(id).orElseGet(LocalStorage::new);
  62. ValidationUtil.isNull(localStorage.getId(),"LocalStorage","id",id);
  63. return localStorageMapper.toDto(localStorage);
  64. }
  65. @Override
  66. @Transactional(rollbackFor = Exception.class)
  67. public LocalStorage create(String name, MultipartFile multipartFile) {
  68. //计算文件大小
  69. FileUtil.checkSize(properties.getMaxSize(), multipartFile.getSize());
  70. //获取文件后缀名
  71. String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
  72. //获取文件类型
  73. String type = FileUtil.getFileType(suffix);
  74. //开始上传
  75. File file = FileUtil.upload(multipartFile, properties.getPath().getPath() + File.separator);
  76. if(ObjectUtil.isNull(file)){
  77. throw new BadRequestException("上传失败");
  78. }
  79. String url = "https://smartpark.caih.com/static/zkxtres/"+file.getName();
  80. try {
  81. name = StringUtils.isBlank(name) ? FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename()) : name;
  82. LocalStorage localStorage = new LocalStorage(
  83. url,
  84. file.getName(),
  85. name,
  86. suffix,
  87. file.getPath(),
  88. type,
  89. FileUtil.getSize(multipartFile.getSize())
  90. );
  91. return localStorageRepository.save(localStorage);
  92. }catch (Exception e){
  93. FileUtil.del(file);
  94. throw e;
  95. }
  96. }
  97. @Override
  98. @Transactional(rollbackFor = Exception.class)
  99. public void update(LocalStorage resources) {
  100. LocalStorage localStorage = localStorageRepository.findById(resources.getId()).orElseGet(LocalStorage::new);
  101. ValidationUtil.isNull( localStorage.getId(),"LocalStorage","id",resources.getId());
  102. localStorage.copy(resources);
  103. localStorageRepository.save(localStorage);
  104. }
  105. @Override
  106. @Transactional(rollbackFor = Exception.class)
  107. public void deleteAll(Long[] ids) {
  108. for (Long id : ids) {
  109. LocalStorage storage = localStorageRepository.findById(id).orElseGet(LocalStorage::new);
  110. FileUtil.del(storage.getPath());
  111. localStorageRepository.delete(storage);
  112. }
  113. }
  114. @Override
  115. public void download(List<LocalStorageDto> queryAll, HttpServletResponse response) throws IOException {
  116. List<Map<String, Object>> list = new ArrayList<>();
  117. for (LocalStorageDto localStorageDTO : queryAll) {
  118. Map<String,Object> map = new LinkedHashMap<>();
  119. map.put("文件名", localStorageDTO.getRealName());
  120. map.put("备注名", localStorageDTO.getName());
  121. map.put("文件类型", localStorageDTO.getType());
  122. map.put("文件大小", localStorageDTO.getSize());
  123. map.put("创建者", localStorageDTO.getCreateBy());
  124. map.put("创建日期", localStorageDTO.getCreateTime());
  125. list.add(map);
  126. }
  127. FileUtil.downloadExcel(list, response);
  128. }
  129. @Override
  130. @Transactional(rollbackFor = Exception.class)
  131. public int deleteByUrl(String oldName) {
  132. return this.localStorageRepository.deleteByUrl(oldName);
  133. }
  134. @Override
  135. public Map<String, Object> findByRealName(String avatarName) {
  136. return this.localStorageRepository.findByRealName(avatarName);
  137. }
  138. }