Add support for downloading caches in flat file format

Signed-off-by: Graham <gpe@openrs2.org>
Graham 4 years ago
parent 6aea0cfc8e
commit ba60fecb50
  1. 24
      archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt
  2. 14
      archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt
  3. 9
      archive/src/main/resources/org/openrs2/archive/templates/caches/index.html
  4. 14
      archive/src/main/resources/org/openrs2/archive/templates/caches/show.html

@ -15,6 +15,7 @@ import kotlinx.coroutines.sync.withPermit
import org.openrs2.archive.cache.CacheExporter import org.openrs2.archive.cache.CacheExporter
import org.openrs2.archive.map.MapRenderer import org.openrs2.archive.map.MapRenderer
import org.openrs2.cache.DiskStoreZipWriter import org.openrs2.cache.DiskStoreZipWriter
import org.openrs2.cache.FlatFileStoreZipWriter
import java.nio.file.attribute.FileTime import java.nio.file.attribute.FileTime
import java.time.Instant import java.time.Instant
import java.util.zip.Deflater import java.util.zip.Deflater
@ -53,7 +54,7 @@ public class CachesController @Inject constructor(
call.respond(ThymeleafContent("caches/show.html", mapOf("cache" to cache))) call.respond(ThymeleafContent("caches/show.html", mapOf("cache" to cache)))
} }
public suspend fun export(call: ApplicationCall) { public suspend fun exportDisk(call: ApplicationCall) {
val id = call.parameters["id"]?.toIntOrNull() val id = call.parameters["id"]?.toIntOrNull()
if (id == null) { if (id == null) {
call.respond(HttpStatusCode.NotFound) call.respond(HttpStatusCode.NotFound)
@ -74,6 +75,27 @@ public class CachesController @Inject constructor(
} }
} }
public suspend fun exportFlatFile(call: ApplicationCall) {
val id = call.parameters["id"]?.toIntOrNull()
if (id == null) {
call.respond(HttpStatusCode.NotFound)
return
}
call.response.header(
HttpHeaders.ContentDisposition,
ContentDisposition.Attachment
.withParameter(ContentDisposition.Parameters.FileName, "cache.zip")
.toString()
)
call.respondOutputStream(contentType = ContentType.Application.Zip) {
FlatFileStoreZipWriter(ZipOutputStream(this)).use { store ->
exporter.export(id, store)
}
}
}
public suspend fun exportKeysJson(call: ApplicationCall) { public suspend fun exportKeysJson(call: ApplicationCall) {
val id = call.parameters["id"]?.toIntOrNull() val id = call.parameters["id"]?.toIntOrNull()
if (id == null) { if (id == null) {

@ -55,7 +55,17 @@ public class WebServer @Inject constructor(
get("/") { call.respond(ThymeleafContent("index.html", emptyMap())) } get("/") { call.respond(ThymeleafContent("index.html", emptyMap())) }
get("/caches") { cachesController.index(call) } get("/caches") { cachesController.index(call) }
get("/caches/{id}") { cachesController.show(call) } get("/caches/{id}") { cachesController.show(call) }
get("/caches/{id}.zip") { cachesController.export(call) } get("/caches/{id}.zip") {
val id = call.parameters["id"]
if (id == null) {
call.respond(HttpStatusCode.NotFound)
return@get
}
call.respondRedirect(permanent = true) {
path("caches", id, "disk.zip")
}
}
get("/caches/{id}.json") { get("/caches/{id}.json") {
val id = call.parameters["id"] val id = call.parameters["id"]
if (id == null) { if (id == null) {
@ -67,6 +77,8 @@ public class WebServer @Inject constructor(
path("caches", id, "keys.json") path("caches", id, "keys.json")
} }
} }
get("/caches/{id}/disk.zip") { cachesController.exportDisk(call) }
get("/caches/{id}/flat-file.zip") { cachesController.exportFlatFile(call) }
get("/caches/{id}/keys.json") { cachesController.exportKeysJson(call) } get("/caches/{id}/keys.json") { cachesController.exportKeysJson(call) }
get("/caches/{id}/keys.zip") { cachesController.exportKeysZip(call) } get("/caches/{id}/keys.zip") { cachesController.exportKeysZip(call) }
get("/caches/{id}/map.png") { cachesController.renderMap(call) } get("/caches/{id}/map.png") { cachesController.renderMap(call) }

@ -79,8 +79,10 @@
Download Download
</button> </button>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li><a th:href="${'/caches/' + cache.id + '.zip'}" <li><a th:href="${'/caches/' + cache.id + '/disk.zip'}"
class="dropdown-item">Cache</a></li> class="dropdown-item">Cache (.dat2/.idx)</a></li>
<li><a th:href="${'/caches/' + cache.id + '/flat-file.zip'}"
class="dropdown-item">Cache (Flat file)</a></li>
<li> <li>
<hr class="dropdown-divider" /> <hr class="dropdown-divider" />
</li> </li>
@ -88,6 +90,9 @@
class="dropdown-item">Keys (JSON)</a></li> class="dropdown-item">Keys (JSON)</a></li>
<li><a th:href="${'/caches/' + cache.id + '/keys.zip'}" <li><a th:href="${'/caches/' + cache.id + '/keys.zip'}"
class="dropdown-item">Keys (Text)</a></li> class="dropdown-item">Keys (Text)</a></li>
<li>
<hr class="dropdown-divider" />
</li>
<li><a th:href="${'/caches/' + cache.id + '/map.png'}" <li><a th:href="${'/caches/' + cache.id + '/map.png'}"
class="dropdown-item">Map</a></li> class="dropdown-item">Map</a></li>
</ul> </ul>

@ -49,16 +49,24 @@
<tr class="thead-dark"> <tr class="thead-dark">
<th>Download</th> <th>Download</th>
<td> <td>
<div class="btn-group"> <div class="btn-toolbar">
<a th:href="${'/caches/' + cache.id + '.zip'}" <div class="btn-group mr-2">
class="btn btn-primary btn-sm">Cache</a> <a th:href="${'/caches/' + cache.id + '/disk.zip'}"
class="btn btn-primary btn-sm">Cache (.dat2/.idx)</a>
<a th:href="${'/caches/' + cache.id + '/flat-file.zip'}"
class="btn btn-primary btn-sm">Cache (Flat file)</a>
</div>
<div class="btn-group mr-2">
<a th:href="${'/caches/' + cache.id + '/keys.json'}" <a th:href="${'/caches/' + cache.id + '/keys.json'}"
class="btn btn-primary btn-sm">Keys (JSON)</a> class="btn btn-primary btn-sm">Keys (JSON)</a>
<a th:href="${'/caches/' + cache.id + '/keys.zip'}" <a th:href="${'/caches/' + cache.id + '/keys.zip'}"
class="btn btn-primary btn-sm">Keys (Text)</a> class="btn btn-primary btn-sm">Keys (Text)</a>
</div>
<div class="btn-group">
<a th:href="${'/caches/' + cache.id + '/map.png'}" <a th:href="${'/caches/' + cache.id + '/map.png'}"
class="btn btn-primary btn-sm">Map</a> class="btn btn-primary btn-sm">Map</a>
</div> </div>
</div>
</td> </td>
</tr> </tr>
</table> </table>

Loading…
Cancel
Save