Ruby / Sinatra - serving up css, javascript, or image files
Sinatra and Rails use the path public
for static content - e.g., ./public/javascripts/
. All files in these paths would then be served by the web server (e.g. Thin, Passenger), but without the need for /public
in the URL (e.g. the file at #{my_app_root}/public/javascripts/application.js
would be available via the Web at the URL http://#{my_domain}/javascripts/application.js
).
Including local (JS and CSS) files in local Sinatra Development
Move your static files(css/js) into a folder named public
. Sinatra looks there with default settings.
If you want to change that behaviour have a look at this: Static Files
Serving static files with Sinatra
Without any additional configuration, Sinatra will serve assets in public
. For the empty route, you'll want to render the index document.
require 'rubygems'
require 'sinatra'
get '/' do
File.read(File.join('public', 'index.html'))
end
Routes should return a String
which become the HTTP response body. File.read
opens a file, reads the file, closes the file and returns a String
.
serve a single image with sinatra no matter whats asked for
This answer assumes you have a directory public/imgs
where you store your images:
With redirect():
require 'sinatra'
get "/imgs/*.*" do
img_name, ext = params[:splat]
if image_name == key
#do stuff
else
#do other stuff
end
redirect to('/imgs/alien1.png')
end
By default, Sinatra will first check if the requested file is in the ./public folder and return it if it exists. If the file isn't in ./public, then Sinatra will try to match a route. So the code in a route cannot prevent a user from seeing existing images in your public folder.
You can add disable :static
to the top of your routes file to stop Sinatra from looking in ./public for the requested file. Instead, Sinatra will go straight to route matching. In that case, the redirect() in the example WILL cause an infinite loop. So if you have images in the public folder that you don't want users to see, you can't use redirect().
With send_file():
It looks like tadman's send_file() solution requires an absolute(or relative) file system path, so you can do this:
require 'sinatra'
get "/imgs/*.*" do
img_name, ext = params[:splat]
if image_name == key
#do stuff
else
#do other stuff
end
send_file File.expand_path('imgs/alien1.png', settings.public_folder)
end
Note that with send_file(), the original url, with the originally requested image, will remain displayed in the browser, which may or may not be what you want.
Serving static files in Sinatra... with beautiful routes?
Got it!
get '/path_to_file/:number/:file' do
File.read(File.join('path_to_file', "#{params[:number]}", "#{params[:file]}"))
end
get '/path_to_file/:number' do
File.read(File.join('path_to_file', "#{params[:number]}", "index.html"))
end
Order is important, since if these two methods are reversed, get '/path_to_file/:number'
becomes a superset of get '/path_to_file/:number/:file'
.
Sinatra serving images in HTML
It was indeed a stupid mistake; I wasn't URI encoding the source. When I changed the name to just blah.png, it rendered correctly!
How to write portable image paths in CSS files for a mounted sinatra app?
You can just use a relative URL in your CSS.
Since both the CSS and the image will be under the public
dir the relative path between them will be the same wherever the app as a whole is mounted.
Serving an image from behind a firewall with Ruby/Sinatra
What if you have a route to get images from the API?
Example:
get '/image/:image_name' do
content_type 'image/png'
API::get_image(params[:image_name])
end
Then you would have to rewrite the urls in your image tags to use this route.
Hope this helps
PS: also make sure that the :image_name that can be passes will not be a security hole to the API. Make sure that it will only allow urls that 'make sense' or that the API has security measures that it only accepts calls to images and not sensible data.
Related Topics
Difference Between Plugins and Ruby Gems
Find() with Nil When There Are No Records
Using Helpers in Rails 3 to Output HTML
Ruby: Sum Corresponding Members of Two or More Arrays
Error While Installing Ruby Using Rvm
Sidekiq: Ensure All Jobs on the Queue Are Unique
How to Use Watir::Waiter::Wait_Until to Force Chrome to Wait
Ruby Gem Not Found Although It Is Installed
Replacing the 'Auto_Link' Method in Ruby on Rails 3.1
Hashes of Hashes Idiom in Ruby
Best Ruby Idiom for "Nil or Zero"
(Ruby,Rails) Context of Self in Modules and Libraries...
Ruby on Rails Error "Cannot Load Such File -- Less"
Repairing Postgresql After Upgrade to Osx Mavericks