Module: QuickFile
- Defined in:
- lib/quick_file.rb,
lib/quick_file/upload.rb,
lib/quick_file/version.rb
Defined Under Namespace
Modules: Upload
Constant Summary collapse
- CACHE_DIR =
"/tmp"
- VERSION =
"0.0.1"
Class Method Summary collapse
- .cache_path(cn) ⇒ Object
- .configure {|options| ... } ⇒ Object
- .content_type_for(filename) ⇒ Object
- .download(url, to) ⇒ Object
- .fog_connection ⇒ Object
- .fog_directory ⇒ Object
- .generate_cache_name(ext) ⇒ Object
- .host_url ⇒ Object
- .image_from_url(url) ⇒ Object
- .is_video_file?(filename) ⇒ Boolean
- .new_cache_file(ext) ⇒ Object
- .options ⇒ Object
- .resize_to_fill(file, x, y) ⇒ Object
- .resize_to_fit(file, x, y) ⇒ Object
Class Method Details
.cache_path(cn) ⇒ Object
74 75 76 |
# File 'lib/quick_file.rb', line 74 def cache_path(cn) File.join(CACHE_DIR, cn) end |
.configure {|options| ... } ⇒ Object
9 10 11 |
# File 'lib/quick_file.rb', line 9 def configure yield if block_given? end |
.content_type_for(filename) ⇒ Object
25 26 27 28 29 |
# File 'lib/quick_file.rb', line 25 def content_type_for(filename) mime = MIME::Types.type_for(filename)[0] return mime.simplified if mime return "application/file" end |
.download(url, to) ⇒ Object
78 79 80 81 82 |
# File 'lib/quick_file.rb', line 78 def download(url, to) out = open(to, "wb") out.write(open(url).read) out.close end |
.fog_connection ⇒ Object
35 36 37 38 39 |
# File 'lib/quick_file.rb', line 35 def fog_connection @@fog_connection ||= begin Fog::Storage.new([:fog_credentials]) end end |
.fog_directory ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/quick_file.rb', line 41 def fog_directory @@fog_directory ||= begin fog_connection.directories.new( :key => [:fog_directory], :public => [:fog_public] ) end end |
.generate_cache_name(ext) ⇒ Object
17 18 19 |
# File 'lib/quick_file.rb', line 17 def generate_cache_name(ext) "#{SecureRandom.hex(5)}#{ext}" end |
.host_url ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/quick_file.rb', line 50 def host_url @@host_url ||= begin { :s3 => "s3.amazonaws.com/#{[:fog_directory]}/" } end end |
.image_from_url(url) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/quick_file.rb', line 84 def image_from_url(url) open(url, 'rb') do |f| image = Magick::Image.from_blob(f.read).first end image end |
.is_video_file?(filename) ⇒ Boolean
31 32 33 |
# File 'lib/quick_file.rb', line 31 def is_video_file?(filename) filename.downcase.end_with?('.mov', '.3gp', '.wmv', '.m4v', '.mp4') end |
.new_cache_file(ext) ⇒ Object
21 22 23 |
# File 'lib/quick_file.rb', line 21 def new_cache_file(ext) QuickFile.cache_path QuickFile.generate_cache_name(ext) end |
.options ⇒ Object
13 14 15 |
# File 'lib/quick_file.rb', line 13 def @@options ||= UPLOAD_OPTIONS end |
.resize_to_fill(file, x, y) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/quick_file.rb', line 66 def resize_to_fill(file, x, y) img = Magick::Image.read(file).first nim = img.resize_to_fill(x, y) outfile = cache_path(generate_cache_name(File.extname(file))) nim.write outfile outfile end |
.resize_to_fit(file, x, y) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/quick_file.rb', line 58 def resize_to_fit(file, x, y) img = Magick::Image.read(file).first nim = img.resize_to_fit x, y outfile = cache_path(generate_cache_name(File.extname(file))) nim.write outfile outfile end |