Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Fjölnir �sgeirsson http://ninjakitten.us

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

File icon giver/getter/whatever

This code uses acts_as_attachment (that's the attachment.filename) but you can of course replace it with anything.
It gets the file extension then looks up that extension in a dir full of files named extension.png
if none is found, unknown.png is used


<%
extension = File.extname(attachment.filename).gsub(/\./, "")
# get a file icon
icon_path = nil
Dir.entries("#{RAILS_ROOT}/public/images/filetypes").each do |entry|
	entry_extension = File.extname(entry)
	if entry.gsub(entry_extension, "") === extension
		icon_path = "/images/filetypes/" + entry
		break
	end
end
icon_path = "/images/filetypes/unknown.png" if icon_path.nil?
 %>
<%= image_tag icon_path %>
<span class="attachment_name"><%= attachment.filename %></span><br />
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS