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.0.4"

Class Method Summary collapse

Class Method Details

.cache_path(cn) ⇒ Object



76
77
78
# File 'lib/quick_file.rb', line 76

def cache_path(cn)
  File.join(CACHE_DIR, cn)
end

.configure {|options| ... } ⇒ Object

Yields:



11
12
13
# File 'lib/quick_file.rb', line 11

def configure
  yield options 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

.download(url, to) ⇒ Object



80
81
82
83
84
# File 'lib/quick_file.rb', line 80

def download(url, to)
  out = open(to, "wb")
  out.write(open(url).read)
  out.close
end

.fog_connectionObject



37
38
39
40
41
# File 'lib/quick_file.rb', line 37

def fog_connection
  @@fog_connection ||= begin
    Fog::Storage.new(options[:fog_credentials])
  end
end

.fog_directoryObject



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 => options[:fog_directory],
      :public => options[: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_urlObject



52
53
54
55
56
57
58
# File 'lib/quick_file.rb', line 52

def host_url
  @@host_url ||= begin
    {
      :s3 => "s3.amazonaws.com/#{options[:fog_directory]}/"
    }
  end
end

.image_from_url(url) ⇒ Object



86
87
88
89
90
91
# File 'lib/quick_file.rb', line 86

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

Returns:

  • (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')
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

.optionsObject



15
16
17
# File 'lib/quick_file.rb', line 15

def options
  @@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