-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
WarcSideloadFactory.java
57 lines (43 loc) · 1.67 KB
/
WarcSideloadFactory.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
package nu.marginalia.converting.sideload.warc;
import com.google.inject.Inject;
import nu.marginalia.converting.sideload.SideloadSource;
import nu.marginalia.converting.sideload.SideloaderProcessing;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class WarcSideloadFactory {
private final SideloaderProcessing processing;
@Inject
public WarcSideloadFactory(SideloaderProcessing processing) {
this.processing = processing;
}
public Collection<? extends SideloadSource> createSideloaders(Path pathToWarcFiles) throws IOException {
if (Files.isRegularFile(pathToWarcFiles)) {
return List.of(new WarcSideloader(pathToWarcFiles, processing));
}
else if (Files.isDirectory(pathToWarcFiles)) {
final List<Path> files = new ArrayList<>();
try (var stream = Files.list(pathToWarcFiles)) {
stream
.filter(Files::isRegularFile)
.filter(this::isWarcFile)
.forEach(files::add);
}
List<WarcSideloader> sources = new ArrayList<>();
for (Path file : files) {
sources.add(new WarcSideloader(file, processing));
}
return sources;
}
else {
throw new IllegalArgumentException("Path " + pathToWarcFiles + " is neither a file nor a directory");
}
}
private boolean isWarcFile(Path path) {
return path.toString().endsWith(".warc")
|| path.toString().endsWith(".warc.gz");
}
}