feat: 开发中...

This commit is contained in:
2023-04-08 15:02:31 +08:00
parent 8a48f5ee3a
commit ae3a2f9c5d
11 changed files with 321 additions and 163 deletions

View File

@@ -6,6 +6,7 @@ import cn.hamster3.application.blog.vo.ResponseVO;
import cn.hamster3.application.blog.vo.attach.AttachInfoResponseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.core.io.InputStreamResource;
@@ -24,9 +25,9 @@ public class AttachController {
@Resource
private IAttachService attachService;
@PostMapping("/")
@PostMapping(value = "/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "新建附件")
public ResponseVO<Long> createAttach(@RequestParam MultipartFile file) throws IOException {
public ResponseVO<Long> createAttach(@Schema(type = "_file") @RequestPart("file") MultipartFile file) throws IOException {
return attachService.createAttach(file);
}
@@ -51,9 +52,9 @@ public class AttachController {
return attachService.getAttachList(PageRequest.of(page, size));
}
@PutMapping("/{attachID}/")
@PutMapping(value = "/{attachID}/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "更新附件")
public ResponseVO<Void> updateAttach(@PathVariable Long attachID, @RequestParam MultipartFile file) throws IOException {
public ResponseVO<Void> updateAttach(@PathVariable Long attachID, @RequestPart("file") MultipartFile file) throws IOException {
return attachService.updateAttach(attachID, file);
}

View File

@@ -15,12 +15,12 @@ public interface AttachRepository extends JpaRepository<AttachEntity, Long>, Jpa
boolean existsByIdAndCreator_Id(Long id, UUID id1);
@Query("select a from AttachEntity a where a.id = ?1")
AttachEntity findByIdWithContent(Long id);
AttachEntity findByIdWithData(Long id);
@Transactional
@Modifying
@Query("update AttachEntity a set a.data = ?1, a.contentType = ?2 where a.id = ?3")
void updateDataAndContentTypeById(byte[] data, String contentType, Long id);
@Query("update AttachEntity a set a.filename = ?1, a.contentType = ?2, a.data = ?3 where a.id = ?4")
void updateFilenameAndContentTypeAndDataById(String filename, String contentType, byte[] data, Long id);
@Query("select a from AttachEntity a where a.creator.id = ?1 order by a.createTime DESC")
Page<AttachEntity> findByCreator_IdOrderByCreateTimeDesc(UUID id, Pageable pageable);

View File

@@ -77,7 +77,7 @@ public class AttachService implements IAttachService {
if (localCacheFile.exists()) {
entity = attachRepo.findById(attachID).orElse(null);
} else {
entity = attachRepo.findByIdWithContent(attachID);
entity = attachRepo.findByIdWithData(attachID);
}
if (entity == null) {
return ResponseEntity.notFound().build();
@@ -113,7 +113,12 @@ public class AttachService implements IAttachService {
if (!attachRepo.existsById(attachID)) {
return ResponseVO.notFound();
}
attachRepo.updateDataAndContentTypeById(file.getBytes(), file.getContentType(), attachID);
attachRepo.updateFilenameAndContentTypeAndDataById(
file.getOriginalFilename(),
file.getContentType(),
file.getBytes(),
attachID
);
File localCacheFile = new File(ATTACH_FOLDER, String.valueOf(attachID));
Files.copy(file.getInputStream(), localCacheFile.toPath(), StandardCopyOption.REPLACE_EXISTING);