-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAbstractFileSystemService.java
More file actions
143 lines (121 loc) · 4.42 KB
/
AbstractFileSystemService.java
File metadata and controls
143 lines (121 loc) · 4.42 KB
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
/*
* Copyright 2006-2018 University of Dundee. All rights reserved.
* Use is subject to license terms supplied in LICENSE.txt
*/
package ome.io.nio;
import java.io.File;
import java.nio.file.Paths;
import java.util.Formatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author callan
*
*/
public class AbstractFileSystemService {
/** The logger for this particular class */
private static Logger log = LoggerFactory.getLogger(AbstractFileSystemService.class);
public final static String ROOT_DEFAULT = File.separator + "OMERO"
+ File.separator;
public final static String PIXELS_PATH = "Pixels" + File.separator;
public final static String FILES_PATH = "Files" + File.separator;
public final static String THUMBNAILS_PATH = "Thumbnails" + File.separator;
private final String root;
@Deprecated
public AbstractFileSystemService(String path) {
this(path, false);
log.info("assuming read-write repository at " + path);
}
public AbstractFileSystemService(String path, boolean isReadOnlyRepo) {
File rootDirectory = new File(path);
if (!rootDirectory.isDirectory() || !rootDirectory.canRead()
|| !(isReadOnlyRepo || rootDirectory.canWrite())) {
throw new IllegalArgumentException(
"Invalid directory specified for file system service."
+ rootDirectory);
}
this.root = rootDirectory.getAbsolutePath();
if (log.isDebugEnabled()) {
log.debug("Using root path: '" + this.root + "'");
}
}
/**
* Makes sure that for a given path, its subpath exists. For example, given
* the path "/foo/bar/foobar.txt" the method will make sure the directory
* structure "/foo/bar" exists.
*
* @param path
* the path to check for subpath existence.
*/
protected void createSubpath(String path) {
File file = new File(path);
if (!file.exists()) {
File directory = new File(file.getParent());
if (!directory.exists()) {
directory.mkdirs();
}
}
}
/**
* Returns a numbered path relative to the root of this service, but is
* ignorant of FS and similar constructs. For example, this will return
* "ROOT/Pixels/"
*
* @return the path relative to the root
*/
public String getPixelsDirectory() {
return Paths.get(root, PIXELS_PATH).toString();
}
/**
* Returns a numbered path relative to the root of this service, but is
* ignorant of FS and similar constructs. For example, given an id of
* 12345 this will return "ROOT/Pixels/Dir-123/Dir-456/123456"
*
* @param id the Pixels identifier
* @return the path relative to the root
*/
public String getPixelsPath(Long id) {
return getPath(PIXELS_PATH, id);
}
/**
* Returns a numbered path relative to the root of this service, but is
* ignorant of FS and similar constructs. For example, given an id of
* 123456 this will return "ROOT/Files/Dir-123/Dir-456/123456"
*
* @param id the Files identifier
* @return the path relative to the root
*/
public String getFilesPath(Long id) {
return getPath(FILES_PATH, id);
}
/**
* Returns a numbered path relative to the root of this service, but is
* ignorant of FS and similar constructs. For example, given an id of
* 123456 this will return "ROOT/Thumbnails/Dir-123/Dir-456/123456"
*
* @param id the thumbnail identifier
* @return the path relative to the root
*/
public String getThumbnailPath(Long id) {
return getPath(THUMBNAILS_PATH, id);
}
private String getPath(String prefix, Long id) {
String suffix = "";
Long remaining = id;
Long dirno = 0L;
if (id == null) {
throw new NullPointerException("Expecting a not-null id.");
}
while (remaining > 999) {
remaining /= 1000;
if (remaining > 0) {
try (final Formatter formatter = new Formatter()) {
dirno = remaining % 1000;
suffix = formatter.format("Dir-%03d", dirno).out().toString()
+ File.separator + suffix;
}
}
}
return Paths.get(root, prefix + suffix + id).toString();
}
}