This repository has been archived by the owner on Sep 25, 2022. It is now read-only.
/
FilesystemSourceUtils.java
332 lines (276 loc) · 13.2 KB
/
FilesystemSourceUtils.java
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Copyright (C) 2007 Peter Monks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of an unsupported extension to Alfresco.
*
*/
package org.alfresco.extension.bulkimport.source.fs;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.alfresco.repo.content.ContentStore;
import org.alfresco.repo.content.encoding.ContentCharsetFinder;
import org.alfresco.repo.content.filestore.FileContentStore;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.MimetypeService;
import org.alfresco.extension.bulkimport.source.BulkImportItemVersion;
/**
* This class is a miscellaneous grab bag of filesystem methods that are
* intended to be statically imported.
*
* @author Peter Monks (pmonks@gmail.com)
*
*/
public final class FilesystemSourceUtils
{
private final static String DEFAULT_TEXT_ENCODING = "UTF-8";
private final static int MAX_CONTENT_URL_LENGTH = 255;
// Regexes for matching version files
public final static String VERSION_LABEL_REGEX = "([\\d]+)(\\.([\\d]+))?"; // Group 0 = version label, Group 1 = major version #, group 3 (if not null) = minor version #
private final static String VERSION_SUFFIX_REGEX = "\\.v(" + VERSION_LABEL_REGEX + ")\\z"; // Note: group numbers are one greater than shown above
private final static String VERSION_FILENAME_REGEX = ".+" + VERSION_SUFFIX_REGEX;
private final static Pattern VERSION_FILENAME_PATTERN = Pattern.compile(VERSION_FILENAME_REGEX);
/**
* Returns true if the suspectedChild is within the given directory. The
* String version is preferred over this one, as it forces normalisation
* of the two paths first.
*
* @param directory The directory in which to check <i>(may be null, although doing so will always return false)</i>.
* @param suspectedChild The suspect child to check for <i>(may be null, although doing so will always return false)</i>.
* @return true if and only if suspectedChild is "within" directory. Note that this comparison is done solely at a "path string"
* level. It will attempt to remove relative path elements (".." especially) to avoid incorrect results, but YMMV.
*/
public final static boolean isInDirectory(final File directory, final File suspectedChild)
{
return(isInDirectory(directory.getAbsolutePath(), suspectedChild.getAbsolutePath()));
}
/**
* Returns true if the suspectedChild is within the given directory. This
* method is preferred over the File version, as it forces normalisation
* of the two paths
* (see <a href="http://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/FilenameUtils.html#normalize(java.lang.String, boolean)">this reference</a>)
* first.
*
* @param directoryPath The directory in which to check <i>(may be null, although doing so will always return false)</i>.
* @param suspectedChildPath The suspect child to check for <i>(may be null, although doing so will always return false)</i>.
* @return true if and only if suspectedChild is "within" directory. Note that this comparison is done solely at a "path string"
* level.
*/
public final static boolean isInDirectory(final String directoryPath, final String suspectedChildPath)
{
return(isInDirectoryImpl(new File(FilenameUtils.normalize(directoryPath, true)),
new File(FilenameUtils.normalize(suspectedChildPath, true))));
}
private final static boolean isInDirectoryImpl(final File directory, final File suspectedChild)
{
boolean result = false;
if (directory != null && suspectedChild != null)
{
if (suspectedChild.equals(directory))
{
result = true;
}
else
{
result = isInDirectoryImpl(directory, suspectedChild.getParentFile());
}
}
return(result);
}
/**
* Determines whether the given file is already located in an Alfresco managed content store. Used to determine
* whether to perform a streaming or in-place import.
*
* @param contentStore The content store Alfresco is configured to use <i>(must not be null)</i>.
* @param source The file to test. Typically this would be the source directory for the import <i>(must not be null)</i>.
* @return True if the given file is in an Alfresco managed content store, false otherwise.
*/
public final static boolean isInContentStore(final ContentStore contentStore, final File source)
{
boolean result = false;
final String contentStoreRoot = contentStore.getRootLocation();
if (contentStoreRoot != null && contentStoreRoot.trim().length() > 0)
{
final File contentStoreRootFile = new File(contentStoreRoot);
// If the content store root doesn't exist as a file, we're probably dealing with a non-filesystem content store
if (contentStoreRootFile.exists() && contentStoreRootFile.isDirectory())
{
result = isInDirectory(contentStoreRoot, source.getAbsolutePath());
}
}
return(result);
}
/**
* @param file The file to get the name of <i>(may be null)</i>.
* @return A human readable rendition of the file <i>(null when file is null)</i>.
*/
public final static String getFileName(final File file)
{
String result = null;
if (file != null)
{
result = file.getAbsolutePath();
}
return(result);
}
/**
* This method does the magic of constructing the content URL for
* "in-place" content.
*
* @param mimeTypeService The Alfresco MimetypeService <i>(must not be null)</i>.
* @param contentStore The content store Alfresco is configured to use <i>(must not be null)</i>.
* @param contentFile The content file to build a content URL for <i>(must not be null)</i>.
* @return The constructed <code>ContentData</code>, or null if the contentFile cannot be in-place imported for any reason.
*/
public final static ContentData buildContentProperty(final MimetypeService mimeTypeService, final ContentStore contentStore, final File contentFile)
{
ContentData result = null;
final String normalisedFilename = FilenameUtils.normalize(contentFile.getAbsolutePath(), true);
String normalisedContentStoreRoot = FilenameUtils.normalize(contentStore.getRootLocation(), true);
// Ensure content store root ends with a single / character
if (!normalisedContentStoreRoot.endsWith("/"))
{
normalisedContentStoreRoot = normalisedContentStoreRoot + "/";
}
// If, after normalisation, the filename doesn't start with the content store root, we can't in-place import
if (normalisedFilename.startsWith(normalisedContentStoreRoot))
{
final String contentStoreRelativeFilename = normalisedFilename.substring(normalisedContentStoreRoot.length());
final String contentUrl = FileContentStore.STORE_PROTOCOL + ContentStore.PROTOCOL_DELIMITER + contentStoreRelativeFilename;
// If the resulting content URL would be too long, we can't in-place import
if (contentUrl.length() <= MAX_CONTENT_URL_LENGTH)
{
final String mimeType = mimeTypeService.guessMimetype(contentFile.getName());
final String encoding = guessEncoding(mimeTypeService, contentFile, mimeType);
result = new ContentData(contentUrl, mimeType, contentFile.length(), encoding);
}
}
return(result);
}
/**
* Attempt to guess the encoding of a text file , falling back to {@link #DEFAULT_TEXT_ENCODING}.
*
* @param mimeTypeService The Alfresco MimetypeService <i>(must not be null)</i>.
* @param file The {@link java.io.File} to test <i>(must not be null)</i>.
* @param mimeType The file MIME type. Used to first distinguish between binary and text files <i>(must not be null)</i>.
* @return The text encoding as a {@link String}.
*/
public final static String guessEncoding(final MimetypeService mimeTypeService, final File file, final String mimeType)
{
String result = DEFAULT_TEXT_ENCODING;
InputStream is = null;
final ContentCharsetFinder charsetFinder = mimeTypeService.getContentCharsetFinder();
if (mimeTypeService.isText(mimeType))
{
try
{
is = new BufferedInputStream(new FileInputStream(file));
result = charsetFinder.getCharset(is, mimeType).name();
}
catch (final IOException ioe)
{
result = DEFAULT_TEXT_ENCODING;
}
finally
{
IOUtils.closeQuietly(is);
}
}
return(result);
}
/**
* Strips the version suffix (if any) from a filename.
*
* @param fileName The filename to strip the version suffix from <i>(must not be null, empty or blank)</i>.
* @return The filename with the version suffix (if any) stripped.
*/
public static String stripVersionSuffix(final String fileName)
{
String result = fileName;
if (isVersionFile(result))
{
result = result.replaceFirst(VERSION_SUFFIX_REGEX, "");
}
return(result);
}
/**
* Get the name of the parent file for this file.
*
* @param metadataLoader The configured <code>MetadataLoader</code> <i>(must not be null)</i>.
* @param fileName The filename to check <i>(must not be null, empty or blank)</i>.
* @return The name of the parent file this file.
*/
public static String getParentName(final MetadataLoader metadataLoader, final String fileName)
{
String result = stripVersionSuffix(fileName);
if (isMetadataFile(metadataLoader, result))
{
result = result.substring(0, result.length() - (MetadataLoader.METADATA_SUFFIX + metadataLoader.getMetadataFileExtension()).length());
}
return(result);
}
/**
* @param fileName The filename to check <i>(must not be null, empty or blank)</i>.
* @return True if the given filename represents a version file, false otherwise.
*/
public static boolean isVersionFile(final String fileName)
{
Matcher matcher = VERSION_FILENAME_PATTERN.matcher(fileName);
return(matcher.matches());
}
/**
* @param metadataLoader The configured <code>MetadataLoader</code> <i>(must not be null)</i>.
* @param fileName The filename to check <i>(must not be null, empty or blank)</i>.
* @return True if the given filename represents a metadata file, false otherwise.
*/
public static boolean isMetadataFile(final MetadataLoader metadataLoader, final String fileName)
{
boolean result = false;
if (metadataLoader != null)
{
final String tmpFileName = stripVersionSuffix(fileName);
result = tmpFileName.endsWith(MetadataLoader.METADATA_SUFFIX + metadataLoader.getMetadataFileExtension());
}
return(result);
}
/**
* @param fileName The filename to check <i>(must not be null, empty or blank)</i>.
* @return The version label for the given filename, or <code>Version.VERSION_HEAD</code> if it doesn't have one.
*/
public static BigDecimal getVersionNumber(final String fileName)
{
BigDecimal result = null;
if (fileName != null)
{
Matcher m = VERSION_FILENAME_PATTERN.matcher(fileName);
if (m.matches())
{
result = new BigDecimal(m.group(1)); // Group 1 = version label, including full stop separator for decimal version numbers
}
else
{
result = BulkImportItemVersion.VERSION_HEAD; // Filename doesn't include a version label, so its version is HEAD
}
}
return(result);
}
}