1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.store.legacy.store.internal; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.util.List; |
24 |
|
import java.util.concurrent.locks.ReadWriteLock; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Named; |
28 |
|
import javax.inject.Provider; |
29 |
|
import javax.inject.Singleton; |
30 |
|
|
31 |
|
import org.hibernate.Session; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.store.FileDeleteTransactionRunnable; |
34 |
|
import org.xwiki.store.FileSaveTransactionRunnable; |
35 |
|
import org.xwiki.store.StreamProvider; |
36 |
|
import org.xwiki.store.TransactionRunnable; |
37 |
|
import org.xwiki.store.filesystem.internal.FilesystemStoreTools; |
38 |
|
import org.xwiki.store.legacy.doc.internal.FilesystemAttachmentContent; |
39 |
|
import org.xwiki.store.legacy.doc.internal.ListAttachmentArchive; |
40 |
|
|
41 |
|
import com.xpn.xwiki.XWikiContext; |
42 |
|
import com.xpn.xwiki.XWikiException; |
43 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
44 |
|
import com.xpn.xwiki.doc.XWikiAttachmentArchive; |
45 |
|
import com.xpn.xwiki.doc.XWikiAttachmentContent; |
46 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
47 |
|
import com.xpn.xwiki.store.AttachmentVersioningStore; |
48 |
|
import com.xpn.xwiki.store.XWikiAttachmentStoreInterface; |
49 |
|
import com.xpn.xwiki.store.XWikiStoreInterface; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@version |
55 |
|
@since |
56 |
|
|
57 |
|
@Component |
58 |
|
@Named("file") |
59 |
|
@Singleton |
|
|
| 57.7% |
Uncovered Elements: 33 (78) |
Complexity: 25 |
Complexity Density: 0.51 |
|
60 |
|
public class FilesystemAttachmentStore implements XWikiAttachmentStoreInterface |
61 |
|
{ |
62 |
|
@Inject |
63 |
|
private Provider<FilesystemStoreTools> fileToolsProvider; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
private FilesystemStoreTools fileTools; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
@param |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
75 |
6 |
public FilesystemAttachmentStore(final FilesystemStoreTools fileTools)... |
76 |
|
{ |
77 |
6 |
this.fileTools = fileTools; |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
83 |
0 |
public FilesystemAttachmentStore()... |
84 |
|
{ |
85 |
|
} |
86 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
87 |
13 |
private FilesystemStoreTools getFilesystemStoreTools()... |
88 |
|
{ |
89 |
13 |
if (fileTools != null) { |
90 |
13 |
return fileTools; |
91 |
|
} |
92 |
0 |
return fileToolsProvider.get(); |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
@inheritDoc |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
@see |
103 |
|
|
104 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
105 |
0 |
public void saveAttachmentContent(final XWikiAttachment attachment,... |
106 |
|
final XWikiContext context, |
107 |
|
final boolean bTransaction) |
108 |
|
throws XWikiException |
109 |
|
{ |
110 |
0 |
this.saveAttachmentContent(attachment, true, context, bTransaction); |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
|
@inheritDoc |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
@see |
121 |
|
|
122 |
|
|
|
|
| 44.4% |
Uncovered Elements: 5 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
123 |
2 |
public void saveAttachmentContent(final XWikiAttachment attachment,... |
124 |
|
final boolean updateDocument, |
125 |
|
final XWikiContext context, |
126 |
|
final boolean bTransaction) |
127 |
|
throws XWikiException |
128 |
|
{ |
129 |
2 |
final XWikiHibernateTransaction transaction = new XWikiHibernateTransaction(context); |
130 |
2 |
this.getAttachmentContentSaveRunnable(attachment, updateDocument, context).runIn(transaction); |
131 |
2 |
try { |
132 |
2 |
transaction.start(); |
133 |
|
} catch (Exception e) { |
134 |
0 |
if (e instanceof XWikiException) { |
135 |
0 |
throw (XWikiException) e; |
136 |
|
} |
137 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
138 |
|
XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_ATTACHMENT, |
139 |
|
"Exception while saving attachment.", e); |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
@link |
146 |
|
|
147 |
|
@param |
148 |
|
@param |
149 |
|
@param |
150 |
|
@return |
151 |
|
@throws |
152 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
153 |
4 |
private TransactionRunnable<XWikiHibernateTransaction> getAttachmentContentSaveRunnable(... |
154 |
|
final XWikiAttachment attachment, |
155 |
|
final boolean updateDocument, |
156 |
|
final XWikiContext context) |
157 |
|
throws XWikiException |
158 |
|
{ |
159 |
4 |
final XWikiAttachmentContent content = attachment.getAttachment_content(); |
160 |
|
|
161 |
4 |
if (content == null) { |
162 |
|
|
163 |
0 |
return new TransactionRunnable<XWikiHibernateTransaction>(); |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
4 |
final File attachFile = |
168 |
|
getFilesystemStoreTools().getAttachmentFileProvider(attachment).getAttachmentContentFile(); |
169 |
4 |
final FilesystemStoreTools ft = getFilesystemStoreTools(); |
170 |
|
|
171 |
4 |
return new AttachmentSaveTransactionRunnable(attachment, |
172 |
|
updateDocument, |
173 |
|
context, |
174 |
|
attachFile, |
175 |
|
ft.getTempFile(attachFile), |
176 |
|
ft.getBackupFile(attachFile), |
177 |
|
ft.getLockForFile(attachFile)); |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
|
@inheritDoc |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
@see |
188 |
|
|
189 |
|
|
|
|
| 50% |
Uncovered Elements: 9 (18) |
Complexity: 6 |
Complexity Density: 0.5 |
|
190 |
1 |
public void saveAttachmentsContent(final List<XWikiAttachment> attachments,... |
191 |
|
final XWikiDocument doc, |
192 |
|
final boolean updateDocument, |
193 |
|
final XWikiContext context, |
194 |
|
final boolean bTransaction) throws XWikiException |
195 |
|
{ |
196 |
1 |
if (attachments == null || attachments.size() == 0) { |
197 |
0 |
return; |
198 |
|
} |
199 |
|
|
200 |
1 |
try { |
201 |
1 |
final XWikiHibernateTransaction transaction = new XWikiHibernateTransaction(context); |
202 |
|
|
203 |
1 |
for (XWikiAttachment attach : attachments) { |
204 |
2 |
this.getAttachmentContentSaveRunnable(attach, false, context).runIn(transaction); |
205 |
|
} |
206 |
|
|
207 |
|
|
208 |
1 |
if (updateDocument) { |
209 |
0 |
new TransactionRunnable<XWikiHibernateTransaction>() |
210 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
211 |
0 |
protected void onRun() throws Exception... |
212 |
|
{ |
213 |
0 |
context.getWiki().getStore().saveXWikiDoc(doc, context, false); |
214 |
|
} |
215 |
|
} .runIn(transaction); |
216 |
|
} |
217 |
|
|
218 |
1 |
transaction.start(); |
219 |
|
} catch (Exception e) { |
220 |
0 |
if (e instanceof XWikiException) { |
221 |
0 |
throw (XWikiException) e; |
222 |
|
} |
223 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
224 |
|
XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_ATTACHMENT, |
225 |
|
"Exception while saving attachments", e); |
226 |
|
} |
227 |
|
} |
228 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
229 |
1 |
@Override... |
230 |
|
public void loadAttachmentContent(final XWikiAttachment attachment, |
231 |
|
final XWikiContext context, |
232 |
|
final boolean bTransaction) |
233 |
|
throws XWikiException |
234 |
|
{ |
235 |
1 |
final File attachFile = |
236 |
|
getFilesystemStoreTools().getAttachmentFileProvider(attachment).getAttachmentContentFile(); |
237 |
|
|
238 |
1 |
if (attachFile.exists()) { |
239 |
1 |
FilesystemAttachmentContent content = new FilesystemAttachmentContent(attachFile); |
240 |
1 |
content.setContentDirty(false); |
241 |
1 |
attachment.setAttachment_content(content); |
242 |
1 |
return; |
243 |
|
} |
244 |
|
|
245 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
246 |
|
XWikiException.ERROR_XWIKI_STORE_FILENOTFOUND, |
247 |
|
"The attachment could not be found in the filesystem attachment store.\n" |
248 |
|
+ "This can happen if attachment storage is switched from database to " |
249 |
|
+ "filesystem without first moving all of the database attachments over " |
250 |
|
+ "to the filesystem using a script."); |
251 |
|
} |
252 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
253 |
0 |
@Override... |
254 |
|
public void deleteXWikiAttachment(final XWikiAttachment attachment, |
255 |
|
final XWikiContext context, |
256 |
|
final boolean bTransaction) |
257 |
|
throws XWikiException |
258 |
|
{ |
259 |
0 |
this.deleteXWikiAttachment(attachment, true, context, bTransaction); |
260 |
|
} |
261 |
|
|
|
|
| 44.4% |
Uncovered Elements: 5 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
262 |
2 |
@Override... |
263 |
|
public void deleteXWikiAttachment(final XWikiAttachment attachment, |
264 |
|
final boolean parentUpdate, |
265 |
|
final XWikiContext context, |
266 |
|
final boolean bTransaction) |
267 |
|
throws XWikiException |
268 |
|
{ |
269 |
2 |
final XWikiHibernateTransaction transaction = new XWikiHibernateTransaction(context); |
270 |
2 |
this.getAttachmentDeleteRunnable(attachment, parentUpdate, context).runIn(transaction); |
271 |
2 |
try { |
272 |
2 |
transaction.start(); |
273 |
|
} catch (Exception e) { |
274 |
0 |
if (e instanceof XWikiException) { |
275 |
0 |
throw (XWikiException) e; |
276 |
|
} |
277 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, |
278 |
|
XWikiException.ERROR_XWIKI_UNKNOWN, |
279 |
|
"Exception while deleting attachment.", e); |
280 |
|
} |
281 |
|
} |
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
@param |
287 |
|
@param |
288 |
|
@param |
289 |
|
@return |
290 |
|
|
291 |
|
@throws |
292 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
293 |
2 |
private TransactionRunnable<XWikiHibernateTransaction> getAttachmentDeleteRunnable(... |
294 |
|
final XWikiAttachment attachment, |
295 |
|
final boolean updateDocument, |
296 |
|
final XWikiContext context) |
297 |
|
throws XWikiException |
298 |
|
{ |
299 |
2 |
final File attachFile = |
300 |
|
getFilesystemStoreTools().getAttachmentFileProvider(attachment).getAttachmentContentFile(); |
301 |
2 |
final FilesystemStoreTools ft = getFilesystemStoreTools(); |
302 |
|
|
303 |
2 |
return new AttachmentDeleteTransactionRunnable(attachment, |
304 |
|
updateDocument, |
305 |
|
context, |
306 |
|
attachFile, |
307 |
|
ft.getBackupFile(attachFile), |
308 |
|
ft.getLockForFile(attachFile)); |
309 |
|
} |
310 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
311 |
0 |
@Override... |
312 |
|
public void cleanUp(XWikiContext context) |
313 |
|
{ |
314 |
|
|
315 |
|
} |
316 |
|
|
317 |
|
|
318 |
|
|
319 |
|
|
320 |
|
|
321 |
|
|
|
|
| 75% |
Uncovered Elements: 7 (28) |
Complexity: 6 |
Complexity Density: 0.32 |
|
322 |
|
private static class AttachmentSaveTransactionRunnable |
323 |
|
extends TransactionRunnable<XWikiHibernateTransaction> |
324 |
|
{ |
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
private final XWikiAttachment attachment; |
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
|
333 |
|
private final boolean updateDocument; |
334 |
|
|
335 |
|
|
336 |
|
|
337 |
|
|
338 |
|
private final XWikiContext context; |
339 |
|
|
340 |
|
|
341 |
|
|
342 |
|
|
343 |
|
@param |
344 |
|
@param |
345 |
|
@param |
346 |
|
@param |
347 |
|
@param |
348 |
|
@param |
349 |
|
@param |
350 |
|
@throws@link |
351 |
|
@link |
352 |
|
|
353 |
|
|
|
|
| 69.6% |
Uncovered Elements: 7 (23) |
Complexity: 4 |
Complexity Density: 0.24 |
|
354 |
4 |
AttachmentSaveTransactionRunnable(final XWikiAttachment attachment,... |
355 |
|
final boolean updateDocument, |
356 |
|
final XWikiContext context, |
357 |
|
final File attachFile, |
358 |
|
final File tempFile, |
359 |
|
final File backupFile, |
360 |
|
final ReadWriteLock lock) |
361 |
|
throws XWikiException |
362 |
|
{ |
363 |
4 |
final StreamProvider provider = new AttachmentContentStreamProvider(attachment, context); |
364 |
4 |
new FileSaveTransactionRunnable(attachFile, tempFile, backupFile, lock, provider).runIn(this); |
365 |
|
|
366 |
|
|
367 |
4 |
final AttachmentVersioningStore avs = context.getWiki().getAttachmentVersioningStore(); |
368 |
4 |
final XWikiAttachmentArchive archive = attachment.getAttachment_archive(); |
369 |
4 |
if (avs instanceof FilesystemAttachmentVersioningStore) { |
370 |
0 |
final FilesystemAttachmentVersioningStore favs = (FilesystemAttachmentVersioningStore) avs; |
371 |
|
|
372 |
|
|
373 |
0 |
if (archive == null) { |
374 |
0 |
favs.getArchiveSaveRunnable(new ListAttachmentArchive(attachment), context).runIn(this); |
375 |
|
} else { |
376 |
0 |
favs.getArchiveSaveRunnable(archive, context).runIn(this); |
377 |
|
} |
378 |
|
} else { |
379 |
4 |
new TransactionRunnable<XWikiHibernateTransaction>() |
380 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
381 |
4 |
protected void onRun() throws XWikiException... |
382 |
|
{ |
383 |
4 |
avs.saveArchive(archive, context, false); |
384 |
|
} |
385 |
|
} .runIn(this); |
386 |
|
} |
387 |
|
|
388 |
|
|
389 |
4 |
if (updateDocument) { |
390 |
1 |
final XWikiStoreInterface store = context.getWiki().getStore(); |
391 |
1 |
final XWikiDocument doc = attachment.getDoc(); |
392 |
1 |
new TransactionRunnable<XWikiHibernateTransaction>() |
393 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
394 |
1 |
protected void onRun() throws XWikiException... |
395 |
|
{ |
396 |
1 |
store.saveXWikiDoc(doc, context, false); |
397 |
|
} |
398 |
|
} .runIn(this); |
399 |
|
} |
400 |
|
|
401 |
4 |
this.attachment = attachment; |
402 |
4 |
this.updateDocument = updateDocument; |
403 |
4 |
this.context = context; |
404 |
|
} |
405 |
|
} |
406 |
|
|
407 |
|
|
408 |
|
|
409 |
|
|
|
|
| 84.4% |
Uncovered Elements: 5 (32) |
Complexity: 7 |
Complexity Density: 0.33 |
|
410 |
|
private static class AttachmentDeleteTransactionRunnable |
411 |
|
extends TransactionRunnable<XWikiHibernateTransaction> |
412 |
|
{ |
413 |
|
|
414 |
|
|
415 |
|
|
416 |
|
private final XWikiAttachment attachment; |
417 |
|
|
418 |
|
|
419 |
|
|
420 |
|
|
421 |
|
private final boolean updateDocument; |
422 |
|
|
423 |
|
|
424 |
|
|
425 |
|
|
426 |
|
private final XWikiContext context; |
427 |
|
|
428 |
|
|
429 |
|
|
430 |
|
|
431 |
|
@param |
432 |
|
@param |
433 |
|
@param |
434 |
|
@param |
435 |
|
@param |
436 |
|
@param |
437 |
|
@throws |
438 |
|
|
|
|
| 72.7% |
Uncovered Elements: 3 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
439 |
2 |
AttachmentDeleteTransactionRunnable(final XWikiAttachment attachment,... |
440 |
|
final boolean updateDocument, |
441 |
|
final XWikiContext context, |
442 |
|
final File attachFile, |
443 |
|
final File tempFile, |
444 |
|
final ReadWriteLock lock) |
445 |
|
throws XWikiException |
446 |
|
{ |
447 |
2 |
new FileDeleteTransactionRunnable(attachFile, tempFile, lock).runIn(this); |
448 |
|
|
449 |
|
|
450 |
2 |
final AttachmentVersioningStore avs = context.getWiki().getAttachmentVersioningStore(); |
451 |
|
|
452 |
2 |
if (avs instanceof FilesystemAttachmentVersioningStore) { |
453 |
0 |
final FilesystemAttachmentVersioningStore favs = (FilesystemAttachmentVersioningStore) avs; |
454 |
0 |
favs.getArchiveDeleteRunnable(attachment.loadArchive(context)).runIn(this); |
455 |
|
} else { |
456 |
2 |
new TransactionRunnable<HibernateTransaction>() |
457 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
458 |
2 |
protected void onRun() throws XWikiException... |
459 |
|
{ |
460 |
2 |
avs.deleteArchive(attachment, context, false); |
461 |
|
} |
462 |
|
} .runIn(this); |
463 |
|
} |
464 |
|
|
465 |
2 |
this.context = context; |
466 |
2 |
this.attachment = attachment; |
467 |
2 |
this.updateDocument = updateDocument; |
468 |
|
} |
469 |
|
|
|
|
| 88.2% |
Uncovered Elements: 2 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
470 |
2 |
@Override... |
471 |
|
protected void onRun() throws Exception |
472 |
|
{ |
473 |
|
|
474 |
|
|
475 |
|
|
476 |
2 |
final Session session = this.context.getWiki().getHibernateStore().getSession(this.context); |
477 |
|
|
478 |
|
|
479 |
|
|
480 |
2 |
session.delete(new XWikiAttachmentContent(this.attachment)); |
481 |
|
|
482 |
|
|
483 |
2 |
if (this.updateDocument) { |
484 |
1 |
final String filename = this.attachment.getFilename(); |
485 |
1 |
final List<XWikiAttachment> list = attachment.getDoc().getAttachmentList(); |
486 |
1 |
for (int i = 0; i < list.size(); i++) { |
487 |
1 |
if (filename.equals(list.get(i).getFilename())) { |
488 |
1 |
list.remove(i); |
489 |
1 |
break; |
490 |
|
} |
491 |
|
} |
492 |
1 |
this.context.getWiki().getStore().saveXWikiDoc(this.attachment.getDoc(), |
493 |
|
this.context, |
494 |
|
false); |
495 |
|
} |
496 |
|
|
497 |
|
|
498 |
2 |
session.delete(this.attachment); |
499 |
|
} |
500 |
|
} |
501 |
|
} |