-
Notifications
You must be signed in to change notification settings - Fork 1
/
RegexDiskScanning.java
156 lines (143 loc) · 5.79 KB
/
RegexDiskScanning.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
/*
* Copyright 2017-2022 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
*
* This file is part of REGARDS.
*
* REGARDS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* REGARDS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with REGARDS. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.cnes.regards.modules.acquisition.service.plugins;
import fr.cnes.regards.framework.modules.plugins.annotations.Plugin;
import fr.cnes.regards.framework.modules.plugins.annotations.PluginParameter;
import fr.cnes.regards.framework.utils.plugins.PluginUtilsRuntimeException;
import fr.cnes.regards.modules.acquisition.plugins.IScanPlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
/**
* Scan directories and return detected files according to last modification date filter and a regular expression
* pattern.
*
* @author Marc Sordi
*/
@Plugin(id = "RegexDiskScanning",
version = "1.0.0-SNAPSHOT",
description = "Scan directories to detect files filtering with a regular expression pattern",
author = "REGARDS Team",
contact = "regards@c-s.fr",
license = "GPLv3",
owner = "CSSI",
url = "https://github.com/RegardsOss")
public class RegexDiskScanning implements IScanPlugin {
private static final Logger LOGGER = LoggerFactory.getLogger(RegexDiskScanning.class);
public static final String FIELD_REGEX = "pattern";
@PluginParameter(name = FIELD_REGEX, label = "Regular expression", defaultValue = ".*", optional = true)
private String regex;
private DirectoryStream.Filter<Path> filter;
@Override
public List<Path> scan(Path dirPath, Optional<OffsetDateTime> scanningDate) {
// Init filter
filter = file -> Pattern.compile(regex).matcher(file.getFileName().toString()).matches();
List<Path> scannedFiles = new ArrayList<>();
if (Files.isDirectory(dirPath)) {
scannedFiles.addAll(scanDirectory(dirPath, scanningDate));
} else {
LOGGER.error("Invalid directory path : {}", dirPath.toString());
}
return scannedFiles;
}
private List<Path> scanDirectory(Path dirPath, Optional<OffsetDateTime> scanningDate) {
long startTime = System.currentTimeMillis();
List<Path> scannedFiles = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dirPath, filter)) {
for (Path entry : stream) {
if (Files.isRegularFile(entry)) {
if (scanningDate.isPresent()) {
OffsetDateTime lmd = OffsetDateTime.ofInstant(Files.getLastModifiedTime(entry).toInstant(),
ZoneOffset.UTC);
if (lmd.isAfter(scanningDate.get()) || lmd.isEqual(scanningDate.get())) {
scannedFiles.add(entry);
}
} else {
scannedFiles.add(entry);
}
}
}
} catch (IOException x) {
throw new PluginUtilsRuntimeException("Scanning failure", x);
}
LOGGER.info("{} new file(s) scanned inside the directory {} in {} milliseconds",
scannedFiles.size(),
dirPath.toString(),
System.currentTimeMillis() - startTime);
return scannedFiles;
}
// FIXME test before regex adapting
// /**
// * Converts a a pattern to a Java pattern.<br>
// * The table below shows the conversions that are applied.<br>
// * The order of this 2 conversions is important, it should be not modified.<br>
// * <table border=1 cellpadding=2>
// * <tr>
// * <th>Order</th>
// * <th>Original pattern</th>
// * <th>Java pattern</th>
// * </tr>
// * <tr>
// * <td>1</td>
// * <td>.</td>
// * <td>\.</td>
// * </tr>
// * <tr>
// * <td>2</td>
// * <td>*</td>
// * <td>.*</td>
// * </tr>
// * </table>
// *
// * @param originalPattern a pattern to converts to a Java pattern
// * @return the Java pattern
// */
// protected String getAdaptedPattern(String originalPattern) {
//
// String adaptedPattern = originalPattern;
// // "." => "\."
// adaptedPattern = replacePattern("\\.", "\\\\.", adaptedPattern);
// // "*" => ".*"
// adaptedPattern = replacePattern("\\*", "\\.\\*", adaptedPattern);
// return adaptedPattern;
// }
//
// /**
// * Replace a pattern by a replacement value in a {@link String}
// *
// * @param patternToReplace the {@link String} to replace
// * @param replacement the replacement value
// * @param target the {@link String} in that apply the replacement
// * @return a new {@link String} where a the pattern is replaced by a replacement value
// */
// protected String replacePattern(String patternToReplace, String replacement, String target) {
// Pattern pattern = Pattern.compile(patternToReplace);
// Matcher matcher = pattern.matcher(target);
// return matcher.replaceAll(replacement);
// }
}