AttachmentServiceImpl.java
5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package isa.qa.service.impl;
import isa.qa.core.ServiceException;
import isa.qa.dao.ApplicationDao;
import isa.qa.dao.AttachmentDao;
import isa.qa.dto.response.AttachmentResponseDTO;
import isa.qa.entity.Application;
import isa.qa.entity.Attachment;
import isa.qa.properties.FileProperties;
import isa.qa.properties.JwtProperties;
import isa.qa.service.AttachmentService;
import isa.qa.utils.FileUtils;
import isa.qa.utils.JwtTokenUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import static com.google.common.base.Preconditions.checkArgument;
import static java.io.File.separator;
import static java.util.stream.Collectors.toList;
/**
* Attachment service implements
*
* @author May
* @version 1.0
* @date 2018/12/28 16:41
*/
@Service
@AllArgsConstructor
public class AttachmentServiceImpl implements AttachmentService {
private final FileProperties fileProperties;
private final AttachmentDao attachmentDao;
private final ApplicationDao applicationDao;
private final JwtTokenUtil jwtTokenUtil;
private final JwtProperties jwtProperties;
@Override
@Transactional(rollbackFor = TransactionException.class)
public AttachmentResponseDTO uploadFile(String accessKey, MultipartFile file) throws IOException {
String appKey = jwtTokenUtil.getAppKeyFromToken(accessKey.substring(jwtProperties.getTokenHead().length()));
Application application = applicationDao.findById(appKey).orElseThrow(() -> new ServiceException("Application miss"));
Date now = Date.from(Instant.now());
String relativePath = application.getName() + separator;
String fileName = application.getName() + now.toInstant().toEpochMilli() + FileUtils.getFileSuffix(file.getOriginalFilename());
Attachment attachment = new Attachment();
attachment.setSize(file.getSize());
file.transferTo(new File(fileProperties.getBasePath() + relativePath + fileName));
attachment.setContentType(file.getContentType());
attachment.setName(fileName);
attachment.setOriginalName(file.getOriginalFilename());
attachment.setRelativePath(relativePath);
attachment.setAccessUrl(fileProperties.getBaseAccessUrl() + application.getName() + "/" + fileName);
attachment.setCreateTime(now);
attachment.setUpdateTime(now);
attachmentDao.save(attachment);
return new AttachmentResponseDTO(attachment.getId(), attachment.getName(), attachment.getAccessUrl());
}
@Override
@Transactional(rollbackFor = TransactionException.class)
public List<AttachmentResponseDTO> uploadFiles(String accessKey, List<MultipartFile> files) throws IOException {
String appKey = jwtTokenUtil.getAppKeyFromToken(accessKey.substring(jwtProperties.getTokenHead().length()));
Application application = applicationDao.findById(appKey).orElseThrow(() -> new ServiceException("Application miss"));
Date now = Date.from(Instant.now());
String relativePath = application.getName() + separator;
Integer length = files.size();
List<Attachment> attachments = new ArrayList<>(length);
for (int i = 0; i < length; i++) {
String fileName = application.getName() + now.toInstant().toEpochMilli() + i + FileUtils.getFileSuffix(files.get(i).getOriginalFilename());
Attachment attachment = new Attachment();
attachment.setSize(files.get(i).getSize());
files.get(i).transferTo(new File(fileProperties.getBasePath() + relativePath + fileName));
attachment.setContentType(files.get(i).getContentType());
attachment.setName(fileName);
attachment.setOriginalName(files.get(i).getOriginalFilename());
attachment.setRelativePath(relativePath);
attachment.setAccessUrl(fileProperties.getBaseAccessUrl() + application.getName() + "/" + fileName);
attachment.setCreateTime(now);
attachment.setUpdateTime(now);
attachments.add(attachment);
}
attachmentDao.saveAll(attachments);
return attachments.stream().map(attachment -> new AttachmentResponseDTO(attachment.getId(), attachment.getName(), attachment.getAccessUrl())).collect(toList());
}
@Override
@Transactional(rollbackFor = TransactionException.class)
public boolean deleteFile(String accessKey, String fileName) {
String appKey = jwtTokenUtil.getAppKeyFromToken(accessKey.substring(jwtProperties.getTokenHead().length()));
Attachment attachment = attachmentDao.findByName(fileName).orElseThrow(() -> new ServiceException("File not found"));
Application application = applicationDao.findById(appKey).orElseThrow(() -> new ServiceException("Application miss"));
checkArgument(Objects.equals(attachment.getRelativePath().replace(separator, ""), application.getName()), "Can't remove the other application's attachment");
String filePath = fileProperties.getBasePath() + attachment.getRelativePath() + attachment.getName();
boolean result = FileUtils.delFile(filePath);
attachmentDao.delete(attachment);
return result;
}
@Override
@Transactional(rollbackFor = TransactionException.class)
public boolean deleteFile(String fileName) {
Attachment attachment = attachmentDao.findByName(fileName).orElseThrow(() -> new ServiceException("File not found"));
String filePath = fileProperties.getBasePath() + attachment.getRelativePath() + attachment.getName();
boolean result = FileUtils.delFile(filePath);
attachmentDao.delete(attachment);
return result;
}
}