-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileChecker.scala
59 lines (52 loc) · 1.71 KB
/
FileChecker.scala
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
package com.exasol.common.file
import java.io.File
import java.io.IOException
import java.io.UncheckedIOException
import java.nio.file.Files
import java.nio.file.Paths
import com.exasol.errorreporting.ExaError
/**
* An abstract class for file checker implementations.
*/
abstract class FileChecker {
/**
* Returns an expected prefix that file paths starts with for this filesystem.
*
* @return a location prefix
*/
def getLocationPrefix(): String
/**
* Cheks if path is regular file.
*
* @param path a path to file location
* @return {@code true} if regular file, otherwise {@code false}
*/
final def isRegularFile(filePath: String): Boolean = {
val path = Paths.get(filePath)
checkStartsWithPath(path.toFile())
Files.isRegularFile(path)
}
protected[file] final def checkStartsWithPath(file: File): Unit =
try {
val absolutePath = file.getCanonicalPath()
if (!absolutePath.startsWith(getLocationPrefix())) {
throw new IllegalArgumentException(
ExaError
.messageBuilder("E-IEUCS-12")
.message("Provided path {{PATH}} does not start with expected location prefix.", absolutePath)
.mitigation("Please make sure that file path start with {{PREFIX}}.", getLocationPrefix())
.toString()
)
}
} catch {
case exception: IOException =>
throw new UncheckedIOException(
ExaError
.messageBuilder("E-IEUCS-13")
.message("Failed to open path {{PATH}}.", file.getAbsolutePath())
.mitigation("Please make sure that file exists and starts with location {{PREFIX}}", getLocationPrefix())
.toString(),
exception
)
}
}