-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileServiceImpl.java
More file actions
211 lines (157 loc) · 7.4 KB
/
FileServiceImpl.java
File metadata and controls
211 lines (157 loc) · 7.4 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.example.caselabproject.services.implementations;
import com.example.caselabproject.exceptions.*;
import com.example.caselabproject.models.DTOs.response.FileDownloadResponseDto;
import com.example.caselabproject.models.DTOs.response.FileResponseDto;
import com.example.caselabproject.models.entities.Document;
import com.example.caselabproject.models.entities.File;
import com.example.caselabproject.models.enums.RecordState;
import com.example.caselabproject.repositories.DocumentRepository;
import com.example.caselabproject.repositories.FileRepository;
import com.example.caselabproject.repositories.UserRepository;
import com.example.caselabproject.services.FileService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FileUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.List;
@Service
@RequiredArgsConstructor
@Validated
public class FileServiceImpl implements FileService {
private final FileRepository fileRepository;
private final DocumentRepository documentRepository;
private final UserRepository userRepository;
@Override
public List<FileResponseDto> addFile(String username, MultipartFile multipartFile, Long documentId) {
if (!userRepository.existsByUsernameAndDocuments_id(username, documentId)) {
throw new DocumentAccessException(username);
}
Document document = documentRepository.findById(documentId)
.orElseThrow(() -> new DocumentDoesNotExistException(documentId));
if (document.getRecordState().equals(RecordState.DELETED)) {
throw new DocumentDoesNotExistException(documentId);
}
File file = new File();
file.setDocument(document);
try {
multipartFileToFile(multipartFile, file);
FileUtils.writeByteArrayToFile(
new java.io.File(file.getPath()),
multipartFile.getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
List<File> files = document.getFiles();
// Эта строка нужна для того, чтобы в выводе списка файлов не было дубликтов
files.size();
files.add(file);
document.setFiles(files);
document.setUpdateDate(LocalDateTime.now());
try {
documentRepository.save(document);
} catch (Exception e) {
throw new FileConnectToDocumentException();
}
return document.getFiles().stream().map(FileResponseDto::mapFromEntity).toList();
}
@Override
public List<FileResponseDto> getFiles(Long documentId, Pageable pageable) {
if (documentChecker(documentId)) {
throw new DocumentDoesNotExistException(documentId);
}
Page<File> files = fileRepository.findAllByDocument_Id(documentId, pageable);
if (files.isEmpty()) {
throw new NoFilesPageFoundException(pageable.getPageNumber());
}
return files.map(FileResponseDto::mapFromEntity).toList();
}
@Override
public FileDownloadResponseDto downloadFile(Long documentId, Long fileId) throws IOException {
if (!documentRepository.existsByIdAndRecordState(documentId, RecordState.ACTIVE)) {
throw new DocumentDoesNotExistException(documentId);
}
File file = fileRepository.findById(fileId).orElseThrow(
() -> new FileNotExistException(fileId)
);
java.io.File downloadFile = new java.io.File(file.getPath());
FileDownloadResponseDto responseDto = FileDownloadResponseDto.mapFromEntity(file);
responseDto.setBytes(FileUtils.readFileToByteArray(downloadFile));
return responseDto;
}
@Override
public List<FileResponseDto> updateFile(String username, MultipartFile file,
Long documentId, Long fileId) {
if (!userRepository.existsByUsernameAndDocuments_id(username, documentId)) {
throw new DocumentAccessException(username);
}
if (documentChecker(documentId)) {
throw new DocumentDoesNotExistException(documentId);
}
File updateFile = fileRepository.findById(fileId).orElseThrow(
() -> new FileNotExistException(fileId)
);
try {
FileUtils.writeByteArrayToFile(new java.io.File(
"src\\main\\resources\\fileBase\\"
+ updateFile.getDocument().getCreator().getUsername() + "\\"
+ "docId_" + updateFile.getDocument().getId() + "_"
+ file.getOriginalFilename()), file.getBytes());
multipartFileToFile(file, updateFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
fileRepository.save(updateFile);
Document document = documentRepository.findById(documentId)
.orElseThrow(() -> new DocumentDoesNotExistException(documentId));
document.setUpdateDate(LocalDateTime.now());
documentRepository.save(document);
return document.getFiles().stream().map(FileResponseDto::mapFromEntity).toList();
}
@Override
public boolean deleteFile(String username, Long documentId, Long fileId) {
if (!userRepository.existsByUsernameAndDocuments_id(username, documentId)) {
throw new DocumentAccessException(username);
}
Document document = documentRepository.findById(documentId)
.orElseThrow(() -> new DocumentDoesNotExistException(documentId));
if (document.getRecordState().equals(RecordState.DELETED)) {
throw new DocumentDoesNotExistException(documentId);
}
File file = fileRepository.findById(fileId)
.orElseThrow(() -> new FileNotExistException(fileId));
List<File> files = document.getFiles();
Path filePath = Paths.get(file.getPath());
try {
Files.delete(filePath);
} catch (IOException e) {
throw new RuntimeException("Failed to delete file from the filesystem");
}
files.remove(file);
document.setFiles(files);
document.setUpdateDate(LocalDateTime.now());
documentRepository.save(document);
return true;
}
private void multipartFileToFile(MultipartFile multipartFile, File file) throws IOException {
file.setName(multipartFile.getOriginalFilename());
file.setType(multipartFile.getContentType());
file.setSize(multipartFile.getSize());
file.setPath("src\\main\\resources\\fileBase\\"
+ file.getDocument().getCreator().getUsername() + "\\"
+ "docId_" + file.getDocument().getId() + "_"
+ multipartFile.getOriginalFilename());
}
private boolean documentChecker(Long documentId) {
Document document = documentRepository.findById(documentId)
.orElseThrow(() -> new DocumentDoesNotExistException(documentId));
return (document.getRecordState().equals(RecordState.DELETED));
}
}