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 =
"1.2.0"
Class Method Summary collapse
- .cache_path(cn) ⇒ Object
- .configure {|options| ... } ⇒ Object
- .content_type_for(filename) ⇒ Object
- .convert_video_to_flv(file) ⇒ Object
- .convert_video_to_mp4(file) ⇒ Object
- .create_video_thumb(file) ⇒ 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
94 95 96 |
# File 'lib/quick_file.rb', line 94 def cache_path(cn) File.join(CACHE_DIR, cn) end |
.configure {|options| ... } ⇒ Object
11 12 13 |
# File 'lib/quick_file.rb', line 11 def configure yield if block_given? end |
.content_type_for(filename) ⇒ Object
27 28 29 30 31 |
# File 'lib/quick_file.rb', line 27 def content_type_for(filename) mime = MIME::Types.type_for(filename)[0] return mime.simplified if mime return "application/file" end |
.convert_video_to_flv(file) ⇒ Object
82 83 84 85 86 |
# File 'lib/quick_file.rb', line 82 def convert_video_to_flv(file) new_file = QuickFile.new_cache_file(".flv") `ffmpeg -i #{file} -f flv -ar 11025 -r 24 -s 480x320 #{new_file}` new_file end |
.convert_video_to_mp4(file) ⇒ Object
76 77 78 79 80 |
# File 'lib/quick_file.rb', line 76 def convert_video_to_mp4(file) new_file = QuickFile.new_cache_file(".mp4") `ffmpeg -i #{file} -vcodec libx264 -b 500k -bt 50k -acodec libfaac -ab 56k -ac 2 -s 480x320 #{new_file}` new_file end |
.create_video_thumb(file) ⇒ Object
88 89 90 91 92 |
# File 'lib/quick_file.rb', line 88 def create_video_thumb(file) new_file = QuickFile.new_cache_file(".jpg") `ffmpeg -i #{file} #{new_file}` new_file end |
.download(url, to) ⇒ Object
98 99 100 101 102 |
# File 'lib/quick_file.rb', line 98 def download(url, to) out = open(to, "wb") out.write(open(url).read) out.close end |
.fog_connection ⇒ Object
37 38 39 40 41 |
# File 'lib/quick_file.rb', line 37 def fog_connection @@fog_connection ||= begin Fog::Storage.new([:fog_credentials]) end end |
.fog_directory ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/quick_file.rb', line 43 def fog_directory @@fog_directory ||= begin fog_connection.directories.new( :key => [:fog_directory], :public => [:fog_public] ) end end |
.generate_cache_name(ext) ⇒ Object
19 20 21 |
# File 'lib/quick_file.rb', line 19 def generate_cache_name(ext) "#{SecureRandom.hex(5)}#{ext}" end |
.host_url ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/quick_file.rb', line 52 def host_url @@host_url ||= begin { :s3 => "s3.amazonaws.com/#{[:fog_directory]}/" } end end |
.image_from_url(url) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/quick_file.rb', line 104 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
33 34 35 |
# File 'lib/quick_file.rb', line 33 def is_video_file?(filename) filename.downcase.end_with?('.mov', '.3gp', '.wmv', '.m4v', '.mp4', '.flv') end |
.new_cache_file(ext) ⇒ Object
23 24 25 |
# File 'lib/quick_file.rb', line 23 def new_cache_file(ext) QuickFile.cache_path QuickFile.generate_cache_name(ext) end |
.options ⇒ Object
15 16 17 |
# File 'lib/quick_file.rb', line 15 def @@options ||= {} end |
.resize_to_fill(file, x, y) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/quick_file.rb', line 68 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
60 61 62 63 64 65 66 |
# File 'lib/quick_file.rb', line 60 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 |