-
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-1662 Replace in-memory cache with fs for processed frontend files (R…
…esolve #1662)
- Loading branch information
Showing
13 changed files
with
273 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
reposilite-backend/src/main/kotlin/com/reposilite/frontend/LazyPlaceholderResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.reposilite.frontend | ||
|
||
import java.io.InputStream | ||
import java.io.OutputStream | ||
|
||
internal class LazyPlaceholderResolver(private val placeholders: Map<String, String>) { | ||
|
||
private val theLongestPlaceholder = placeholders.keys | ||
.maxOfOrNull { it } | ||
?.length | ||
?: 0 | ||
|
||
init { | ||
verifyPlaceholders() | ||
} | ||
|
||
private fun verifyPlaceholders() { | ||
placeholders.keys.forEach { placeholder -> | ||
placeholder.forEach { | ||
if (it.code > Byte.MAX_VALUE) { | ||
throw UnsupportedOperationException("LazyPlaceholderResolve supports only basic placeholders from 1-byte long symbols") | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun process(input: InputStream, output: OutputStream) { | ||
val buffer = ByteArray(1024) | ||
|
||
while (true) { | ||
val length = input.read(buffer) | ||
.takeIf { it != -1 } | ||
?: break | ||
|
||
// convert data from buffer to text | ||
var content = buffer.decodeToString(endIndex = length) | ||
|
||
// check if current content may end with placeholder | ||
if (content.length > theLongestPlaceholder) { | ||
content = loadSlicedPlaceholders(input, content) | ||
} | ||
|
||
// resolve placeholders | ||
for ((name, value) in placeholders) { | ||
content = content.replace(name, value) | ||
} | ||
|
||
val resolvedContentAsBytes = content.toByteArray() | ||
output.write(resolvedContentAsBytes, 0, resolvedContentAsBytes.size) | ||
} | ||
} | ||
|
||
private fun loadSlicedPlaceholders(input: InputStream, content: String): String { | ||
val borderlineStartIndex = content.length - theLongestPlaceholder + 1 | ||
val positionCount = content.length - borderlineStartIndex | ||
|
||
for (index in 0 until positionCount) { | ||
val placeholderStartIndex = borderlineStartIndex + index | ||
val boundaryPlaceholder = content.substring(placeholderStartIndex, content.length) | ||
|
||
for (placeholder in placeholders.keys) { | ||
if (placeholder.startsWith(boundaryPlaceholder)) { | ||
val missingBuffer = ByteArray(theLongestPlaceholder) | ||
val missingLength = input.read(missingBuffer) | ||
|
||
if (missingLength == -1) { | ||
return content | ||
} | ||
|
||
val contentWithMissingPlaceholder = content + missingBuffer.decodeToString(endIndex = missingLength) | ||
return loadSlicedPlaceholders(input, contentWithMissingPlaceholder) | ||
} | ||
} | ||
} | ||
|
||
return content | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
reposilite-backend/src/main/kotlin/com/reposilite/frontend/NotFoundTemplate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.reposilite.frontend | ||
|
||
import org.intellij.lang.annotations.Language | ||
|
||
object NotFoundTemplate { | ||
|
||
private val uriFormatter = Regex("/+") // exclude common typos from URI | ||
private val regexAntiXss = Regex("[^A-Za-z0-9/.\\- ]") // exclude custom non-standard characters from template | ||
|
||
fun createNotFoundPage(basePath: String, originUri: String, details: String): String { | ||
val uri = originUri.replace(uriFormatter, "/") | ||
val dashboardUrl = basePath + (if (basePath.endsWith("/")) "" else "/") + "#" + uri | ||
|
||
@Language("html") | ||
val response = """ | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>Reposilite - 404 Not Found</title> | ||
</head> | ||
<style> | ||
body { | ||
height: calc(100vh - 170px); | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
font-family: Arial, Helvetica, sans-serif; | ||
} | ||
.error-view { | ||
text-align: center; | ||
width: 100vh; | ||
height: 100px; | ||
} | ||
.spooky p { | ||
margin-top: 0; | ||
margin-bottom: 0; | ||
font-size: 1.2rem; | ||
font-weight: lighter; | ||
} | ||
a:link, a:visited { | ||
color: rebeccapurple; | ||
} | ||
</style> | ||
<body> | ||
<div class='error-view'> | ||
<h1 style="font-size: 1.5rem"> | ||
<span style="color: gray;">404︱</span>Resource not found | ||
</h1> | ||
${if (details.isEmpty()) "" else "<p><i>${regexAntiXss.replace(details, "")}</i></p>" } | ||
<p>Looking for a dashboard?</p> | ||
<div class="spooky"> | ||
<p>{\__/}</p> | ||
<p>(●ᴗ●)</p> | ||
<p>( >🥕</p> | ||
</div> | ||
<p>Visit <a href="$dashboardUrl" style="color: rebeccapurple; text-decoration: none;">$dashboardUrl</a></p> | ||
</div> | ||
</body> | ||
</html> | ||
""" | ||
|
||
return response.trimIndent() | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
reposilite-backend/src/main/kotlin/com/reposilite/frontend/ResourceSupplier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.reposilite.frontend | ||
|
||
import panda.std.Result | ||
import java.io.IOException | ||
import java.io.InputStream | ||
|
||
fun interface ResourceSupplier { | ||
fun supply(): Result<InputStream, IOException> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.