Module: Blobsterix::S3UrlHelper

Included in:
S3Api
Defined in:
lib/blobsterix/s3/s3_url_helper.rb

Constant Summary collapse

HOST_PATH =
/(\w+)(\.s3)?\.\w+\.\w+/

Instance Method Summary collapse

Instance Method Details

#bucketObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/blobsterix/s3/s3_url_helper.rb', line 49

def bucket
  host = bucket_matcher(env['HTTP_HOST'])
  if host
    host[1]
  elsif  (env[nil] && env[nil][:bucket])
    env[nil][:bucket]
  elsif  (env[nil] && env[nil][:bucket_or_file])
    if env[nil][:bucket_or_file].include?("/")
      env[nil][:bucket_or_file].split("/")[0]
    else
      env[nil][:bucket_or_file]
    end
  else
    "root"
  end
end

#bucket?Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/blobsterix/s3/s3_url_helper.rb', line 66

def bucket?
  host = bucket_matcher(env['HTTP_HOST'])
  host || env[nil][:bucket] || included_bucket
end

#bucket_matcher(str) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/blobsterix/s3/s3_url_helper.rb', line 4

def bucket_matcher(str)
  if str.include?("s3")
    str.match(/(\w+)\.s3\.\w+\.\w+/)
  else
    str.match(/(\w+)\.\w+\.\w+/)
  end
end

#cache_uploadObject



16
17
18
# File 'lib/blobsterix/s3/s3_url_helper.rb', line 16

def cache_upload
  cache.put(cache_upload_key, env['rack.input'].read)
end

#cache_upload_keyObject



29
30
31
32
# File 'lib/blobsterix/s3/s3_url_helper.rb', line 29

def cache_upload_key
  #@cache_upload_key ||= "upload/"+bucket.gsub("/", "_")+"_"+file.gsub("/", "_")
  @cache_upload_key ||= Blobsterix::BlobAccess.new(:bucket => bucket, :id => "upload_#{file.gsub("/", "_")}")
end

#cached_uploadObject



20
21
22
23
# File 'lib/blobsterix/s3/s3_url_helper.rb', line 20

def cached_upload
  cache_upload if not cache.exists?(cache_upload_key)
  cache.get(cache_upload_key)
end

#cached_upload_clearObject



25
26
27
# File 'lib/blobsterix/s3/s3_url_helper.rb', line 25

def cached_upload_clear
  cache.delete(cache_upload_key)
end

#faviconObject



12
13
14
# File 'lib/blobsterix/s3/s3_url_helper.rb', line 12

def favicon
  @favicon ||= file.match /favicon/
end

#fileObject



85
86
87
88
89
90
91
# File 'lib/blobsterix/s3/s3_url_helper.rb', line 85

def file
  if format
    [env[nil][:file] || env[nil][:bucket_or_file] || "", format].join(".")
  else
    env[nil][:file] || env[nil][:bucket_or_file] || ""
  end
end

#formatObject



71
72
73
# File 'lib/blobsterix/s3/s3_url_helper.rb', line 71

def format
  @format ||= env[nil][:format]
end

#included_bucketObject



75
76
77
78
79
80
81
82
83
# File 'lib/blobsterix/s3/s3_url_helper.rb', line 75

def included_bucket
  if env[nil][:bucket_or_file] && env[nil][:bucket_or_file].include?("/")
    env[nil][:bucket] = env[nil][:bucket_or_file].split("/")[0]
    env[nil][:bucket_or_file] = env[nil][:bucket_or_file].gsub("#{env[nil][:bucket]}/", "")
    true
  else
    false
  end
end

#trafo(trafo_s = '') ⇒ Object

TransformationCommand



39
40
41
42
43
44
45
46
47
# File 'lib/blobsterix/s3/s3_url_helper.rb', line 39

def trafo(trafo_s='')
  trafo_a = []
  trafo_s.split(",").each{|command|
    parts = command.split("_")
    key = parts.delete_at(0)
    trafo_a << [key, parts.join("_")]
  }
  trafo_a
end

#trafo_stringObject



34
35
36
# File 'lib/blobsterix/s3/s3_url_helper.rb', line 34

def trafo_string
  @trafo ||= env["HTTP_X_AMZ_META_TRAFO"] || ""
end