1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.store.filesystem.internal; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.util.Arrays; |
24 |
|
import java.util.Date; |
25 |
|
import java.util.HashMap; |
26 |
|
import java.util.Map; |
27 |
|
import java.util.concurrent.locks.ReadWriteLock; |
28 |
|
|
29 |
|
import javax.inject.Inject; |
30 |
|
import javax.inject.Named; |
31 |
|
import javax.inject.Singleton; |
32 |
|
|
33 |
|
import org.apache.commons.lang3.RandomStringUtils; |
34 |
|
import org.xwiki.component.annotation.Component; |
35 |
|
import org.xwiki.component.phase.Initializable; |
36 |
|
import org.xwiki.context.Execution; |
37 |
|
import org.xwiki.environment.Environment; |
38 |
|
import org.xwiki.model.reference.DocumentReference; |
39 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
40 |
|
import org.xwiki.store.locks.LockProvider; |
41 |
|
|
42 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
43 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Singleton |
|
|
| 22.9% |
Uncovered Elements: 74 (96) |
Complexity: 31 |
Complexity Density: 0.53 |
|
54 |
|
public class DefaultFilesystemStoreTools implements FilesystemStoreTools, Initializable |
55 |
|
{ |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private static final String STORAGE_DIR_NAME = "storage"; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
private static final String DOCUMENT_DIR_NAME = "~this"; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
private static final String ATTACHMENT_DIR_NAME = "attachments"; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
private static final String DELETED_ATTACHMENT_DIR_NAME = "deleted-attachments"; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
private static final String DELETED_ATTACHMENT_NAME_SEPARATOR = "-"; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
private static final String BACKUP_FILE_SUFFIX = "~bak"; |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
private static final String TEMP_FILE_SUFFIX = "~tmp"; |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
@Inject |
102 |
|
@Named("path") |
103 |
|
private EntityReferenceSerializer<String> pathSerializer; |
104 |
|
|
105 |
|
@Inject |
106 |
|
private FilesystemAttachmentsConfiguration config; |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
@Inject |
112 |
|
private Execution exec; |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
@Inject |
120 |
|
@Named("dummy") |
121 |
|
private LockProvider lockProvider; |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
@Inject |
127 |
|
private Environment environment; |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
private File storageDir; |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
@param |
138 |
|
@param |
139 |
|
@param |
140 |
|
|
141 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
142 |
9 |
public DefaultFilesystemStoreTools(final EntityReferenceSerializer<String> pathSerializer,... |
143 |
|
final File storageDir, |
144 |
|
final LockProvider lockProvider) |
145 |
|
{ |
146 |
9 |
this.pathSerializer = pathSerializer; |
147 |
9 |
this.storageDir = storageDir; |
148 |
9 |
this.lockProvider = lockProvider; |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
154 |
0 |
public DefaultFilesystemStoreTools()... |
155 |
|
{ |
156 |
|
} |
157 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
158 |
0 |
@Override... |
159 |
|
public void initialize() |
160 |
|
{ |
161 |
0 |
this.storageDir = new File(this.environment.getPermanentDirectory(), STORAGE_DIR_NAME); |
162 |
0 |
if (config.cleanOnStartup()) { |
163 |
0 |
final File dir = this.storageDir; |
164 |
0 |
new Thread(new Runnable() { |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
165 |
0 |
public void run()... |
166 |
|
{ |
167 |
0 |
deleteEmptyDirs(dir, 0); |
168 |
|
} |
169 |
|
}).start(); |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
@param |
179 |
|
@param |
180 |
|
@return |
181 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 8 |
Complexity Density: 0.8 |
|
182 |
0 |
private static boolean deleteEmptyDirs(final File location, int depth)... |
183 |
|
{ |
184 |
0 |
if (location != null && location.exists() && location.isDirectory()) { |
185 |
0 |
final File[] dirs = location.listFiles(); |
186 |
0 |
boolean empty = true; |
187 |
0 |
for (int i = 0; i < dirs.length; i++) { |
188 |
0 |
if (!deleteEmptyDirs(dirs[i], depth + 1)) { |
189 |
0 |
empty = false; |
190 |
|
} |
191 |
|
} |
192 |
0 |
if (empty && depth != 0) { |
193 |
0 |
location.delete(); |
194 |
0 |
return true; |
195 |
|
} |
196 |
|
} |
197 |
0 |
return false; |
198 |
|
} |
199 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
200 |
22 |
@Override... |
201 |
|
public File getBackupFile(final File storageFile) |
202 |
|
{ |
203 |
|
|
204 |
|
|
205 |
|
|
206 |
22 |
return new File(storageFile.getAbsolutePath() + BACKUP_FILE_SUFFIX + RandomStringUtils.randomAlphanumeric(8)); |
207 |
|
} |
208 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
209 |
16 |
@Override... |
210 |
|
public File getTempFile(final File storageFile) |
211 |
|
{ |
212 |
16 |
return new File(storageFile.getAbsolutePath() + TEMP_FILE_SUFFIX + RandomStringUtils.randomAlphanumeric(8)); |
213 |
|
} |
214 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
215 |
0 |
@Override... |
216 |
|
public DeletedAttachmentFileProvider getDeletedAttachmentFileProvider(final XWikiAttachment attachment, |
217 |
|
final Date deleteDate) |
218 |
|
{ |
219 |
0 |
return new DefaultDeletedAttachmentFileProvider( |
220 |
|
this.getDeletedAttachmentDir(attachment, deleteDate), attachment.getFilename()); |
221 |
|
} |
222 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
223 |
0 |
@Override... |
224 |
|
public DeletedAttachmentFileProvider getDeletedAttachmentFileProvider(final String pathToDirectory) |
225 |
|
{ |
226 |
0 |
final File attachDir = new File(this.storageDir, this.getStorageLocationPath()); |
227 |
0 |
return new DefaultDeletedAttachmentFileProvider( |
228 |
|
attachDir, getFilenameFromDeletedAttachmentDirectory(attachDir)); |
229 |
|
} |
230 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
|
231 |
0 |
@Override... |
232 |
|
public Map<String, Map<Date, DeletedAttachmentFileProvider>> deletedAttachmentsForDocument( |
233 |
|
final DocumentReference docRef) |
234 |
|
{ |
235 |
0 |
final File docDir = getDocumentDir(docRef, this.storageDir, this.pathSerializer); |
236 |
0 |
final File deletedAttachmentsDir = new File(docDir, DELETED_ATTACHMENT_DIR_NAME); |
237 |
0 |
final Map<String, Map<Date, DeletedAttachmentFileProvider>> out = |
238 |
|
new HashMap<String, Map<Date, DeletedAttachmentFileProvider>>(); |
239 |
|
|
240 |
0 |
if (!deletedAttachmentsDir.exists()) { |
241 |
0 |
return out; |
242 |
|
} |
243 |
|
|
244 |
0 |
for (File file : Arrays.asList(deletedAttachmentsDir.listFiles())) { |
245 |
0 |
final String currentName = getFilenameFromDeletedAttachmentDirectory(file); |
246 |
0 |
if (out.get(currentName) == null) { |
247 |
0 |
out.put(currentName, new HashMap<Date, DeletedAttachmentFileProvider>()); |
248 |
|
} |
249 |
0 |
out.get(currentName).put(getDeleteDateFromDeletedAttachmentDirectory(file), |
250 |
|
new DefaultDeletedAttachmentFileProvider(file, |
251 |
|
getFilenameFromDeletedAttachmentDirectory(file))); |
252 |
|
} |
253 |
0 |
return out; |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
|
@param |
258 |
|
@return |
259 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
260 |
0 |
private static String getFilenameFromDeletedAttachmentDirectory(final File directory)... |
261 |
|
{ |
262 |
0 |
final String name = directory.getName(); |
263 |
0 |
final String encodedOut = name.substring(0, name.lastIndexOf(DELETED_ATTACHMENT_NAME_SEPARATOR)); |
264 |
0 |
return GenericFileUtils.getURLDecoded(encodedOut); |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
|
@param |
269 |
|
@return |
270 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
271 |
0 |
private static Date getDeleteDateFromDeletedAttachmentDirectory(final File directory)... |
272 |
|
{ |
273 |
0 |
final String name = directory.getName(); |
274 |
|
|
275 |
0 |
long time = Long.parseLong(name.substring(name.lastIndexOf(DELETED_ATTACHMENT_NAME_SEPARATOR) + 1)); |
276 |
0 |
return new Date(time); |
277 |
|
} |
278 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
279 |
0 |
@Override... |
280 |
|
public String getStorageLocationPath() |
281 |
|
{ |
282 |
0 |
return this.storageDir.getAbsolutePath(); |
283 |
|
} |
284 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
285 |
0 |
@Override... |
286 |
|
public File getGlobalFile(final String name) |
287 |
|
{ |
288 |
0 |
return new File(this.storageDir, "~GLOBAL_" + GenericFileUtils.getURLEncoded(name)); |
289 |
|
} |
290 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
291 |
24 |
@Override... |
292 |
|
public AttachmentFileProvider getAttachmentFileProvider(final XWikiAttachment attachment) |
293 |
|
{ |
294 |
24 |
return new DefaultAttachmentFileProvider(this.getAttachmentDir(attachment), |
295 |
|
attachment.getFilename()); |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
@param |
303 |
|
@return |
304 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
305 |
24 |
private File getAttachmentDir(final XWikiAttachment attachment)... |
306 |
|
{ |
307 |
24 |
final XWikiDocument doc = attachment.getDoc(); |
308 |
24 |
if (doc == null) { |
309 |
0 |
throw new NullPointerException("Could not store attachment because it is not " |
310 |
|
+ "associated with a document."); |
311 |
|
} |
312 |
24 |
final File docDir = getDocumentDir(doc.getDocumentReference(), |
313 |
|
this.storageDir, |
314 |
|
this.pathSerializer); |
315 |
24 |
final File attachmentsDir = new File(docDir, ATTACHMENT_DIR_NAME); |
316 |
24 |
return new File(attachmentsDir, GenericFileUtils.getURLEncoded(attachment.getFilename())); |
317 |
|
} |
318 |
|
|
319 |
|
|
320 |
|
|
321 |
|
|
322 |
|
|
323 |
|
|
324 |
|
|
325 |
|
@param |
326 |
|
@param |
327 |
|
@return |
328 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
329 |
0 |
private File getDeletedAttachmentDir(final XWikiAttachment attachment,... |
330 |
|
final Date deleteDate) |
331 |
|
{ |
332 |
0 |
final XWikiDocument doc = attachment.getDoc(); |
333 |
0 |
if (doc == null) { |
334 |
0 |
throw new NullPointerException("Could not store deleted attachment because " |
335 |
|
+ "it is not attached to any document."); |
336 |
|
} |
337 |
0 |
final File docDir = getDocumentDir(doc.getDocumentReference(), |
338 |
|
this.storageDir, |
339 |
|
this.pathSerializer); |
340 |
0 |
final File deletedAttachmentsDir = new File(docDir, DELETED_ATTACHMENT_DIR_NAME); |
341 |
0 |
final String fileName = |
342 |
|
attachment.getFilename() + DELETED_ATTACHMENT_NAME_SEPARATOR + deleteDate.getTime(); |
343 |
0 |
return new File(deletedAttachmentsDir, GenericFileUtils.getURLEncoded(fileName)); |
344 |
|
} |
345 |
|
|
346 |
|
|
347 |
|
|
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
@param |
354 |
|
@param |
355 |
|
@param |
356 |
|
|
357 |
|
@return |
358 |
|
|
359 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
360 |
24 |
private static File getDocumentDir(final DocumentReference docRef,... |
361 |
|
final File storageDir, |
362 |
|
final EntityReferenceSerializer<String> pathSerializer) |
363 |
|
{ |
364 |
24 |
final File path = new File(storageDir, pathSerializer.serialize(docRef)); |
365 |
24 |
return new File(path, DOCUMENT_DIR_NAME); |
366 |
|
} |
367 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
368 |
24 |
@Override... |
369 |
|
public ReadWriteLock getLockForFile(final File toLock) |
370 |
|
{ |
371 |
24 |
return this.lockProvider.getLock(toLock); |
372 |
|
} |
373 |
|
} |