Skip to content

Commit

Permalink
ref: now caching OSM images
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kruse committed Feb 14, 2016
1 parent ace4c54 commit ab1a5c6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ erl_crash.dump
/pictures/*
/send_mention.sh
/release.sh
/cache
1 change: 1 addition & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ config :logger, :console, format: "[$level] $message\n"
config :phoenix, :stacktrace_depth, 20

config :wwwtech, storage_path: "/home/ckruse/sites/wwwtech/pictures"
config :wwwtech, cache_path: "/home/ckruse/sites/wwwtech/cache"

# Configure your database
config :wwwtech, Wwwtech.Repo,
Expand Down
1 change: 1 addition & 0 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ config :logger, :out_log,


config :wwwtech, storage_path: "/home/ckruse/pictures_wwwtech"
config :wwwtech, cache_path: "/home/ckruse/cache_wwwtech"

# ## SSL Support
#
Expand Down
48 changes: 48 additions & 0 deletions web/controllers/cache_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
defmodule Wwwtech.CacheController do
use Wwwtech.Web, :controller
use Wwwtech.Web, :web_controller

alias Wwwtech.Note
alias Wwwtech.Picture
alias Wwwtech.Article

plug :set_mention_header
plug :set_caching_headers

@allowed_hosts [
~r/^https?:\/\/staticmap.openstreetmap.de/
]

def show(conn, %{"url" => url}) do
is_allowed = Enum.any?(@allowed_hosts, fn(x) -> Regex.match?(x, url) end)

if is_allowed do
send_cached_reply(conn, url)
else
conn |> put_status(403) |> text("Access forbidden")
end
end

defp send_cached_reply(conn, url) do
file_name = url2filename(url)

if not File.exists?(file_name) do
get_url(url, file_name)
end

send_file(conn, 200, file_name)
end

defp get_url(url, cache_file) do
rsp = HTTPotion.get(url)
if HTTPotion.Response.success?(rsp) do
File.write!(cache_file, rsp.body)
else
raise "Error getting URL"
end
end

defp url2filename(url) do
Application.get_env(:wwwtech, :cache_path) <> "/" <> Regex.replace(~r/[^a-z0-9A-Z]/, url, "_")
end
end
2 changes: 2 additions & 0 deletions web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ defmodule Wwwtech.Router do

resources "/pictures", PictureController
get "/pictures.atom", PictureController, :index_atom

get "/cache", CacheController, :show
end

scope "/", Wwwtech do
Expand Down
2 changes: 1 addition & 1 deletion web/templates/picture/picture.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ p_type = if assigns[:type], do: assigns[:type], else: :thumbnail
</div>

<%= if assigns[:index] == nil and assigns[:exif] != nil and @exif[:gps_latitude] != nil and @exif[:gps_longitude] != nil do %>
<a class="osm" href="http://www.openstreetmap.org/?lat=<%= to_degrees(@exif[:gps_latitude], @exif[:gps_latitude_ref]) %>&amp;lon=<%= to_degrees(@exif[:gps_longitude], @exif[:gps_longitude_ref]) %>&amp;mlat=<%= to_degrees(@exif[:gps_latitude], @exif[:gps_latitude_ref]) %>&amp;mlon=<%= to_degrees(@exif[:gps_longitude], @exif[:gps_longitude_ref]) %>&amp;zoom=16"><img src="http://staticmap.openstreetmap.de/staticmap.php?center=<%= to_degrees(@exif[:gps_latitude], @exif[:gps_latitude_ref]) %>,<%= to_degrees(@exif[:gps_longitude], @exif[:gps_longitude_ref]) %>&amp;markers=<%= to_degrees(@exif[:gps_latitude], @exif[:gps_latitude_ref]) %>,<%= to_degrees(@exif[:gps_longitude], @exif[:gps_longitude_ref]) %>&amp;zoom=16&amp;size=640x480"></a>
<a class="osm" href="http://www.openstreetmap.org/?lat=<%= to_degrees(@exif[:gps_latitude], @exif[:gps_latitude_ref]) %>&amp;lon=<%= to_degrees(@exif[:gps_longitude], @exif[:gps_longitude_ref]) %>&amp;mlat=<%= to_degrees(@exif[:gps_latitude], @exif[:gps_latitude_ref]) %>&amp;mlon=<%= to_degrees(@exif[:gps_longitude], @exif[:gps_longitude_ref]) %>&amp;zoom=16"><img src="<%= cache_path(@conn, :show, url: "http://staticmap.openstreetmap.de/staticmap.php?center=#{to_degrees(@exif[:gps_latitude], @exif[:gps_latitude_ref])},#{to_degrees(@exif[:gps_longitude], @exif[:gps_longitude_ref])}&markers=#{to_degrees(@exif[:gps_latitude], @exif[:gps_latitude_ref])},#{to_degrees(@exif[:gps_longitude], @exif[:gps_longitude_ref])}&zoom=16&size=640x480") %>"></a>
<% end %>

<%= if assigns[:index] == nil and @picture.posse do %>
Expand Down

0 comments on commit ab1a5c6

Please sign in to comment.