Skip to content

Commit

Permalink
Limit markers to content type "image/*"
Browse files Browse the repository at this point in the history
  • Loading branch information
123FLO321 committed Aug 15, 2020
1 parent 69bb665 commit 821d4f7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ internal class StaticMapController {
scaleString = "@\(staticMap.scale)x"
}
let tileURL = "\(tileServerURL)/styles/\(staticMap.style)/static/\(staticMap.longitude),\(staticMap.latitude),\(staticMap.zoom)@\(staticMap.bearing ?? 0),\(staticMap.pitch ?? 0)/\(staticMap.width)x\(staticMap.height)\(scaleString).\(staticMap.format ?? "png")"
return APIUtils.downloadFile(request: request, from: tileURL, to: path).flatMapError { error in
return APIUtils.downloadFile(request: request, from: tileURL, to: path, type: "image").flatMapError { error in
return request.eventLoop.makeFailedFuture(Abort(.badRequest, reason: "Failed to load base static map: (\(error.localizedDescription))"))
}
}
Expand Down Expand Up @@ -241,7 +241,7 @@ internal class StaticMapController {
statsController.markerServed(new: false, path: path, domain: domain)
return request.eventLoop.future()
}
return APIUtils.downloadFile(request: request, from: url, to: path).always { _ in
return APIUtils.downloadFile(request: request, from: url, to: path, type: "image").always { _ in
self.statsController.markerServed(new: true, path: path, domain: domain)
}.flatMapError { error in
return request.eventLoop.makeFailedFuture(Abort(.badRequest, reason: "Failed to load marker: \(url) (\(error.localizedDescription))"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal class TileController {
} else {
tileURL = "\(tileServerURL)/styles/\(style)/\(z)/\(x)/\(y)\(scaleString).\(format)"
}
return APIUtils.downloadFile(request: request, from: tileURL, to: path).flatMapError { error in
return APIUtils.downloadFile(request: request, from: tileURL, to: path, type: "image").flatMapError { error in
return request.eventLoop.makeFailedFuture(Abort(.badRequest, reason: "Failed to load tile (\(error.localizedDescription))"))
}.always { _ in
request.application.logger.info("Served a generated tile")
Expand Down
6 changes: 4 additions & 2 deletions Sources/SwiftTileserverCache/Misc/APIUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public class APIUtils {

private init() {}

public static func downloadFile(request: Request, from: String, to: String) -> EventLoopFuture<Void> {
public static func downloadFile(request: Request, from: String, to: String, type: String?) -> EventLoopFuture<Void> {
let headers = HTTPHeaders([("User-Agent", "TileserverCache")])
return request.client.get(URI(string: from), headers: headers).flatMap { response in
let errorReason: String
if response.status.code >= 200 && response.status.code < 300 {
if let body = response.body, body.readableBytes != 0 {
if let type = type, response.content.contentType?.type != type {
errorReason = "Failed to load file. Got invalid type: \(request.content.contentType?.description ?? "non")"
} else if let body = response.body, body.readableBytes != 0 {
return request.application.fileio.openFile(
path: to,
mode: .write,
Expand Down

0 comments on commit 821d4f7

Please sign in to comment.