Class: Fiona7::Controllers::RestAPI::BlobController

Inherits:
Object
  • Object
show all
Defined in:
lib/fiona7/controllers/rest_api/blob_controller.rb

Instance Method Summary collapse

Instance Method Details

#copy(blob_id, params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fiona7/controllers/rest_api/blob_controller.rb', line 57

def copy(blob_id, params)
  input              = params.symbolize_keys
  blob_id            = blob_id
  destination_obj_id = input[:destination_obj_id]
  filename           = input[:filename]
  content_type       = input[:content_type]
  destination_obj    = Fiona7::WriteObj.find(destination_obj_id)

  destination_obj.reload_attributes

  binary_spec = Fiona7::Builder::LazyBlobCopier.new({
    destination_obj: destination_obj,
    source_blob_id: blob_id,
    filename: filename,
    content_type: content_type
  }).call.stringify_keys

  destination_obj.save!

  return binary_spec
end

#fetch(blob_id, transformation = false) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fiona7/controllers/rest_api/blob_controller.rb', line 7

def fetch(blob_id, transformation=false)
  public_get_url = BinaryHandling::UrlGenerator.new(blob_id, 'public_access', 'get', transformation).generate
  # all URLs are identical!
  # private_get_url = BinaryHandling::UrlGenerator.new(blob_id, 'private_access', 'get', false).generate
  # public_head_url = BinaryHandling::UrlGenerator.new(blob_id, 'public_access', 'head', false).generate
  # private_head_url = BinaryHandling::UrlGenerator.new(blob_id, 'private_access', 'head', false).generate
  private_head_url = public_head_url = private_get_url = public_get_url

  return {
    'public_access' => {
      'get' => {
        'url' => public_get_url
      },
      'head' => {
        'url' => public_head_url
      }
    },
    'private_access' => {
      'get' => {
        'url' => private_get_url
      },
      'head' => {
        'url' => private_head_url
      }
    }
  }
end

#metadata(blob_id) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fiona7/controllers/rest_api/blob_controller.rb', line 35

def (blob_id)
  meta_binary = BinaryHandling::MetaBinary.new(blob_id)
  content_type = meta_binary.mime_type
  content_length = meta_binary.length

  if meta_binary.image?
    width = meta_binary.width
    height = meta_binary.height
  else
    width = 0
    height = 0
  end
  {
    'meta_data' => {
      'content_type' => ['string', content_type],
      'content_length' => ['number', content_length],
      'width' => ['number', width],
      'height' => ['number', height]
    }
  }
end

#obj_id_from_blob_id(blob_id) ⇒ Object



85
86
87
88
89
# File 'lib/fiona7/controllers/rest_api/blob_controller.rb', line 85

def obj_id_from_blob_id(blob_id)
  return unless blob_id.present?
  binary = BinaryHandling::MetaBinary.new(blob_id)
  return binary.implementation.obj.id if binary.implementation.obj.present? 
end

#parse_blob_id_from_url(url) ⇒ Object



79
80
81
82
83
# File 'lib/fiona7/controllers/rest_api/blob_controller.rb', line 79

def parse_blob_id_from_url( url )
  return unless url.present?
  match_result = url.match(/\/_b\/([a-zA-Z0-9_-]+)(\Z|\/)/)
  return match_result[1] if match_result
end

#resolve_urls(params) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fiona7/controllers/rest_api/blob_controller.rb', line 91

def resolve_urls(params)
  if urls = params[:urls]
    if Array === urls
      {
        results: urls.map do |url| 
          obj_id = obj_id_from_blob_id( parse_blob_id_from_url( url ) )
          {obj_id: obj_id} if obj_id.present?
        end
      }
    else
      "The value for “urls” is not an array."
    end
  else
    "Missing value for the key “urls”."
  end
end