Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
DefaultAttachmentFileProvider | 30 | 7 | 0% | 6 | 4 |
1 | /* | |
2 | * See the NOTICE file distributed with this work for additional | |
3 | * information regarding copyright ownership. | |
4 | * | |
5 | * This is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License as | |
7 | * published by the Free Software Foundation; either version 2.1 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This software is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this software; if not, write to the Free | |
17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
19 | */ | |
20 | package org.xwiki.store.filesystem.internal; | |
21 | ||
22 | import java.io.File; | |
23 | ||
24 | /** | |
25 | * A means of getting files for storing information about a given attachment. | |
26 | * | |
27 | * @version $Id: 38ae7f77d6068cadd1dee44273c4057c6d6814e2 $ | |
28 | * @since 3.0M2 | |
29 | */ | |
30 | public class DefaultAttachmentFileProvider implements AttachmentFileProvider | |
31 | { | |
32 | /** | |
33 | * This stores the attachment metadata for each revision of the attachment in XML format. | |
34 | * | |
35 | * @see #getAttachmentVersioningMetaFile() | |
36 | */ | |
37 | private static final String ATTACH_ARCHIVE_META_FILENAME = "~METADATA.xml"; | |
38 | ||
39 | /** | |
40 | * The directory where all information about this attachment resides. | |
41 | */ | |
42 | private final File attachmentDir; | |
43 | ||
44 | /** | |
45 | * The name of the attached file. | |
46 | */ | |
47 | private final String attachmentFileName; | |
48 | ||
49 | /** | |
50 | * The Constructor. | |
51 | * | |
52 | * @param attachmentDir a directory where all information about this attachment is stored. | |
53 | * @param fileName the name of the attachment file. | |
54 | */ | |
55 | 24 | ![]() |
56 | { | |
57 | 24 | this.attachmentDir = attachmentDir; |
58 | 24 | this.attachmentFileName = fileName; |
59 | } | |
60 | ||
61 | /** | |
62 | * @return the directory where information about this attachment is stored. | |
63 | */ | |
64 | 0 | ![]() |
65 | { | |
66 | 0 | return this.attachmentDir; |
67 | } | |
68 | ||
69 | /** | |
70 | * @return the name of this attachment. | |
71 | */ | |
72 | 0 | ![]() |
73 | { | |
74 | 0 | return this.attachmentFileName; |
75 | } | |
76 | ||
77 | /** | |
78 | * {@inheritDoc} | |
79 | * <p> | |
80 | * Get a File for loading or storing this attachment's content. | |
81 | * This file is derived from the name of the document which the attachment resides in and the | |
82 | * attachment filename. The file will be placed in the storage area in a directory structure | |
83 | * called <storage dir>/<wiki>/<space>/<document name>/~this/attachments/<attachment file name>/ | |
84 | * So an attachment called file.txt in a document called Sandbox.Test in a the main wiki ("xwiki") | |
85 | * would go in the following file: | |
86 | * <storage dir>/xwiki/Sandbox/Test/~this/attachments/file.txt/file.txt | |
87 | * </p> | |
88 | * | |
89 | * @see AttachmentFileProvider#getAttachmentContentFile() | |
90 | */ | |
91 | 15 | ![]() |
92 | { | |
93 | // storage/xwiki/Main/WebHome/~this/attachments/some.file/some.file | |
94 | 15 | return new File(this.attachmentDir, GenericFileUtils.getURLEncoded(this.attachmentFileName)); |
95 | } | |
96 | ||
97 | /** | |
98 | * {@inheritDoc} | |
99 | * <p> | |
100 | * This will be a file named ~METADATA.xml which will reside in the attachment directory. | |
101 | * </p> | |
102 | * | |
103 | * @see AttachmentFileProvider#getAttachmentVersioningMetaFile() | |
104 | */ | |
105 | 11 | ![]() |
106 | { | |
107 | 11 | return new File(this.attachmentDir, ATTACH_ARCHIVE_META_FILENAME); |
108 | } | |
109 | ||
110 | /** | |
111 | * {@inheritDoc} | |
112 | * <p> | |
113 | * Get a file corrisponding to this version of this attachment. | |
114 | * If the file has one or more dots ('.') in it then the version number is inserted before | |
115 | * the last dot. Otherwise it is appended to the end. Version numbers always have "~v" prepended | |
116 | * to prevent collision. | |
117 | * version 1.1 of an attachment called file.txt will be stored as file~v1.1.txt | |
118 | * version 1.2 of an attachment called noExtension will be stored as noExtension~v1.2 | |
119 | * </p> | |
120 | * | |
121 | * @see AttachmentFileProvider#getAttachmentVersioningMetaFile() | |
122 | */ | |
123 | 30 | ![]() |
124 | { | |
125 | 30 | return new File(this.attachmentDir, |
126 | GenericFileUtils.getVersionedFilename(this.attachmentFileName, versionName)); | |
127 | } | |
128 | } |