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

« 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


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