Skip to content

Commit

Permalink
Changing the patching strategy for contentUrl, atLocation.location an…
Browse files Browse the repository at this point in the history
…d url (#5249)

* Patch url to the new location for unknown files

* Patch url to the new location for unknown files when matching the prefix

* Patch location and url to the stripped contentUrl when it matches the prefix

---------

Co-authored-by: Simon Dumas <[email protected]>
  • Loading branch information
imsdu and Simon Dumas authored Nov 26, 2024
1 parent 9fbf56f commit 2d6f506
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final case class FileProcessingConfig(
importBucket: String,
targetBucket: String,
prefix: Option[Path],
locationPrefixToStripOpt: Option[Uri],
locationPrefixToStrip: Option[Uri],
skipFileEvents: Boolean,
mediaTypeDetector: MediaTypeDetectorConfig
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,29 @@ final class DistributionPatcher(
}

private def stripLocationOnUnknownFile(json: Json): Json = {

def strip(original: String, locationPrefixToStrip: String) = original.replaceFirst(locationPrefixToStrip, "file://")

locationPrefixToStripOpt.fold(json) { locationPrefixToStrip =>
root.atLocation.location.string.modify { location =>
location.replaceFirst(locationPrefixToStrip.toString, "file://")
}(json)
val locationPrefixToStripString = locationPrefixToStrip.toString
val matchingContentUrl = root.contentUrl.string.getOption(json).filter(_.startsWith(locationPrefixToStripString))

matchingContentUrl match {
case Some(value) =>
// If there is a contentUrl matching the prefix it had the priority, we strip the path and apply it to contentUrl, location and url
val patchedValue = strip(value, locationPrefixToStripString)
setContentUrl(patchedValue).andThen(setLocation(patchedValue)).andThen(setUrl(patchedValue))(json)
case None =>
// We fallback to a location matching the prefix, we patch the location and the url
val matchedLocation =
root.atLocation.location.string.getOption(json).filter(_.startsWith(locationPrefixToStripString))
matchedLocation match {
case Some(value) =>
val patchedValue = strip(value, locationPrefixToStripString)
setLocation(patchedValue).andThen(setUrl(patchedValue))(json)
case None => json
}
}
}
}

Expand All @@ -95,6 +114,7 @@ final class DistributionPatcher(
private def setContentUrl(newContentUrl: String) = root.contentUrl.string.replace(newContentUrl)
private def setLocation(newLocation: String) = (json: Json) =>
json.deepMerge(Json.obj("atLocation" := Json.obj("location" := newLocation)))
private def setUrl(newUrl: String) = (json: Json) => json.deepMerge(Json.obj("url" := newUrl))
private def setContentSize(newSize: Long) = (json: Json) =>
json.deepMerge(Json.obj("contentSize" := Json.obj("unitCode" := "bytes", "value" := newSize)))
private def setEncodingFormat(contentType: Option[ContentType]) = (json: Json) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object SourcePatcher {
projectMapper,
iriPatcher,
targetBase,
config.files.locationPrefixToStripOpt,
config.files.locationPrefixToStrip,
fetchFileAttributes
)
new SourcePatcher(distributionPatcher, iriPatcher)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class DistributionPatcherSuite extends NexusSuite {
.map(distrubutionDigest)
.assertEquals(jobj"""{
"algorithm": "SHA-256",
"value": "${digest}"
"value": "$digest"
}""")
}

Expand All @@ -279,20 +279,72 @@ class DistributionPatcherSuite extends NexusSuite {
patcher.patchAll(input).assertEquals(expected)
}

test("Patch and strip the distribution location when it matches the given prefix") {
test("Patch and strip the distribution contentUrl when it matches the prefix and set the value to location and url") {
val input =
json"""{
"distribution": {
"contentUrl": "file:///location_to_strip/project/a/b/c/d/file.txt"
}
}"""

val expected = json"""{
"distribution": {
"atLocation": {
"location": "file:///project/a/b/c/d/file.txt"
},
"contentUrl": "file:///project/a/b/c/d/file.txt",
"url": "file:///project/a/b/c/d/file.txt"
}
}"""

patcher.patchAll(input).assertEquals(expected)
}

test("Do not patch the distribution contentUrl when it does not match the prefix") {
val input =
json"""{
"distribution": {
"contentUrl": "file:///some/other/location/project/a/b/c/d/file.txt"
}
}"""

patcher.patchAll(input).assertEquals(input)
}

test("Patch and strip the distribution location when it matches the prefix, setting the url to the same value") {
val input =
json"""{
"distribution": {
"url": "XXX",
"atLocation": {
"location": "file:///location_to_strip/project/a/b/c/d/file.txt"
}
}
}"""

patcher
.patchAll(input)
.map(distributionLocation)
.assertEquals("file:///project/a/b/c/d/file.txt")
val expected = json"""{
"distribution": {
"url": "file:///project/a/b/c/d/file.txt",
"atLocation": {
"location": "file:///project/a/b/c/d/file.txt"
}
}
}"""

patcher.patchAll(input).assertEquals(expected)
}

test("Do not patch the location or the url if the distribution location when it does not match the prefix") {
val input =
json"""{
"distribution": {
"atLocation": {
"location": "file:///some/other/location/project/a/b/c/d/file.txt"
}
}
}"""

patcher.patchAll(input).assertEquals(input)
}

private def distributionContentSize(json: Json): JsonObject = {
Expand Down

0 comments on commit 2d6f506

Please sign in to comment.