|
17 | 17 | package com.reposilite.storage.api
|
18 | 18 |
|
19 | 19 | import com.reposilite.storage.getExtension
|
| 20 | +import java.nio.file.Path |
| 21 | +import java.nio.file.Paths |
20 | 22 | import panda.std.Result
|
21 | 23 | import panda.std.asSuccess
|
22 | 24 | import panda.std.letIf
|
23 |
| -import java.nio.file.Path |
24 |
| -import java.nio.file.Paths |
25 | 25 |
|
26 | 26 | /**
|
27 | 27 | * [Path] alternative, represents location of resource in [com.reposilite.storage.StorageProvider]
|
28 | 28 | */
|
29 |
| -@Suppress("DataClassPrivateConstructor") |
30 |
| -data class Location private constructor(private val uri: String) { |
| 29 | +class Location private constructor(private val uri: String) { |
31 | 30 |
|
32 | 31 | companion object {
|
33 | 32 |
|
| 33 | + private val empty = Location("") |
34 | 34 | private val multipleSlashes = Regex("/+")
|
| 35 | + private val multipleDirectoryOperators = Regex("\\.{2,}") |
35 | 36 |
|
36 | 37 | @JvmStatic
|
37 |
| - fun of(uri: String): Location = |
38 |
| - uri.replace("\\", "/") |
| 38 | + fun of(uri: String): Location { |
| 39 | + return uri |
| 40 | + .replaceBefore(":", "") |
| 41 | + .replace(":", "") |
| 42 | + .replace(multipleDirectoryOperators, ".") |
| 43 | + .replace("\\", "/") |
39 | 44 | .replace(multipleSlashes, "/")
|
40 | 45 | .letIf({ it.startsWith("/") }) { it.removePrefix("/") }
|
41 | 46 | .letIf({ it.endsWith("/") }) { it.removeSuffix("/") }
|
42 | 47 | .let { Location(it) }
|
| 48 | + } |
43 | 49 |
|
44 | 50 | @JvmStatic
|
45 | 51 | fun of(path: Path): Location =
|
46 |
| - path.toString().toLocation() |
| 52 | + of(path.normalize().toString()) |
47 | 53 |
|
48 | 54 | @JvmStatic
|
49 | 55 | fun of(root: Path, path: Path): Location =
|
50 |
| - of(root.relativize(path)) |
| 56 | + of(root.relativize(path.normalize())) |
51 | 57 |
|
52 | 58 | @JvmStatic
|
53 | 59 | fun empty(): Location =
|
54 |
| - "".toLocation() |
| 60 | + empty |
55 | 61 |
|
56 | 62 | }
|
57 | 63 |
|
58 | 64 | fun toPath(): Result<Path, String> {
|
59 |
| - if (uri.contains("..") || uri.contains(":") || uri.contains("\\")) { |
| 65 | + if (uri.contains(":") || uri.contains("\\") || uri.contains(multipleDirectoryOperators)) { |
60 | 66 | return Result.error("Illegal path operator in URI")
|
61 | 67 | }
|
62 |
| - |
63 | 68 | return Paths.get(uri).normalize().asSuccess()
|
64 | 69 | }
|
65 | 70 |
|
@@ -99,13 +104,23 @@ data class Location private constructor(private val uri: String) {
|
99 | 104 | fun getSimpleName(): String =
|
100 | 105 | uri.substringAfterLast("/")
|
101 | 106 |
|
| 107 | + override fun equals(other: Any?): Boolean = |
| 108 | + when { |
| 109 | + this === other -> true |
| 110 | + javaClass != other?.javaClass -> false |
| 111 | + else -> uri == (other as Location).uri |
| 112 | + } |
| 113 | + |
| 114 | + override fun hashCode(): Int = |
| 115 | + uri.hashCode() |
| 116 | + |
102 | 117 | override fun toString(): String =
|
103 | 118 | uri
|
104 | 119 |
|
105 | 120 | }
|
106 | 121 |
|
107 | 122 | fun String?.toLocation(): Location =
|
108 |
| - if (this != null) |
109 |
| - Location.of(this) |
110 |
| - else |
111 |
| - Location.empty() |
| 123 | + when { |
| 124 | + this != null -> Location.of(this) |
| 125 | + else -> Location.empty() |
| 126 | + } |
0 commit comments