-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathFileSystemStorageService.java
More file actions
200 lines (174 loc) · 6.52 KB
/
Copy pathFileSystemStorageService.java
File metadata and controls
200 lines (174 loc) · 6.52 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
package com.microfocus.example.service;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import com.microfocus.example.config.StorageProperties;
import com.microfocus.example.exception.StorageException;
import com.microfocus.example.exception.StorageFileNotFoundException;
import org.apache.tika.Tika;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileSystemUtils;
import org.springframework.web.multipart.MultipartFile;
@Service
public class FileSystemStorageService implements StorageService {
private static final Logger log = LoggerFactory.getLogger(FileSystemStorageService.class);
private final Path rootLocation;
@Autowired
public FileSystemStorageService(StorageProperties properties) {
this.rootLocation = Paths.get(properties.getLocation());
if (!Files.exists(this.rootLocation)) {
log.debug("Creating storage service directory: " + rootLocation.toString());
try {
Files.createDirectory(rootLocation);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void store(MultipartFile file) {
try {
if (file.isEmpty()) {
throw new StorageException("Failed to store empty file.");
}
Path destinationFile = this.rootLocation.resolve(
Paths.get(file.getOriginalFilename()))
.normalize().toAbsolutePath();
if (!destinationFile.getParent().equals(this.rootLocation.toAbsolutePath())) {
// This is a security check
throw new StorageException(
"Cannot store file outside current directory.");
}
try (InputStream inputStream = file.getInputStream()) {
Files.copy(inputStream, destinationFile,
StandardCopyOption.REPLACE_EXISTING);
}
}
catch (IOException e) {
throw new StorageException("Failed to store file.", e);
}
}
@Override
public void store(Path path, String dstFileName) {
try {
if (Objects.isNull(path) || path.toFile().length() < 1L) {
throw new StorageException("Failed to store empty file.");
}
Path destinationFile = Paths.get(dstFileName)
.normalize().toAbsolutePath();
if (!destinationFile.getParent().equals(this.rootLocation.toAbsolutePath())) {
// This is a security check
throw new StorageException(
"Cannot store file outside current directory.");
}
try (InputStream inputStream = Files.newInputStream(path)) {
Files.copy(inputStream, destinationFile,
StandardCopyOption.REPLACE_EXISTING);
}
}
catch (IOException e) {
throw new StorageException("Failed to store file.", e);
}
}
private boolean checkMimeType(Path p, List<String> mimeTypeList) {
Tika tika = new Tika();
String fileMimeType = "";
try {
fileMimeType = tika.detect(p.toFile());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (mimeTypeList.contains(fileMimeType))
return true;
return false;
}
@Override
public Stream<Path> loadAll(List<String> mimeTypeList) {
try {
return Files.walk(this.rootLocation, 1)
.filter(path -> !path.equals(this.rootLocation))
.filter(path -> checkMimeType(path, mimeTypeList))
.map(this.rootLocation::relativize);
}
catch (IOException e) {
throw new StorageException("Failed to read stored files", e);
}
}
@Override
public Stream<Path> loadAll() {
try {
return Files.walk(this.rootLocation, 1)
.filter(path -> !path.equals(this.rootLocation))
.map(this.rootLocation::relativize);
}
catch (IOException e) {
throw new StorageException("Failed to read stored files", e);
}
}
@Override
public Path load(String filename) {
Path resolvedPath = rootLocation.resolve(filename).normalize().toAbsolutePath();
if (!resolvedPath.getParent().equals(this.rootLocation.toAbsolutePath())) {
throw new StorageException("Cannot access file outside current directory.");
}
return resolvedPath;
}
@Override
public Resource loadAsResource(String filename) {
return loadAsResource(filename, false);
}
/*
* To expose OWASP A01:2021 - Broken Access Control
*/
@Override
public Resource loadAsResource(String filename, boolean traverse) {
try {
Path file = null;
if (traverse) {
file = rootLocation.resolve(filename).normalize();
if (!file.startsWith(rootLocation.toAbsolutePath().normalize())) {
throw new StorageException("Cannot access file outside current directory.");
}
} else {
file = load(filename);
}
Resource resource = new UrlResource(file.toUri());
if (resource.exists() || resource.isReadable()) {
return resource;
}
else {
throw new StorageFileNotFoundException(
"Could not read file: " + filename);
}
}
catch (MalformedURLException e) {
throw new StorageFileNotFoundException("Could not read file: " + filename, e);
}
}
@Override
public void deleteAll() {
FileSystemUtils.deleteRecursively(rootLocation.toFile());
}
@Override
public void init() {
try {
Files.createDirectories(rootLocation);
}
catch (IOException e) {
throw new StorageException("Could not initialize storage", e);
}
}
}