Class: CouchrestInterface::FilesMgr

Inherits:
Object
  • Object
show all
Defined in:
lib/glue_envs/couchrest/couchrest_files_mgr.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_env, node_key_value) ⇒ FilesMgr

Returns a new instance of FilesMgr.



25
26
27
28
29
30
31
32
33
34
# File 'lib/glue_envs/couchrest/couchrest_files_mgr.rb', line 25

def initialize(node_env, node_key_value)
  #for bufs node_key is the value of :my_category
  #although it is not used in this class, it is required to 
  #maintain consitency with tinkit_base_node
  #TODO: Actually the goal is for moab's to have no dependency on tinkit_base_node
  #so maybe the glue environment should have a files interface to tinkit_base_node??
  #@attachment_doc_class = node_env.attachClass  #old
  #TODO: just pass moab_data??
  @attachment_doc_class = node_env.moab_data[:attachClass]
end

Instance Attribute Details

#attachment_doc_classObject

Returns the value of attribute attachment_doc_class.



11
12
13
# File 'lib/glue_envs/couchrest/couchrest_files_mgr.rb', line 11

def attachment_doc_class
  @attachment_doc_class
end

Class Method Details

.get_att_doc(node) ⇒ Object

not tested in factory tests, tested in couchrest though



14
15
16
17
18
19
20
21
22
23
# File 'lib/glue_envs/couchrest/couchrest_files_mgr.rb', line 14

def self.get_att_doc(node)
  node_id = node.[:_id]
  attachment_doc_id = node.my_GlueEnv.moab_data[:attachClass].uniq_att_doc_id(node_id)
  att_doc = node.my_GlueEnv.moab_data[:db].get(attachment_doc_id)
  if att_doc
    return att_doc
  else
    return nil 
  end
end

Instance Method Details

#add(node, file_datas) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/glue_envs/couchrest/couchrest_files_mgr.rb', line 36

def add(node, file_datas)
  bia_class = @attachment_doc_class
  attachment_package = {}
  file_datas = [file_datas].flatten
  file_datas.each do |file_data|
    #get file data
    src_filename = file_data[:src_filename]
    src_basename = TkEscape.escape(File.basename(src_filename))
    raise "File data must include the source filename when adding a file to the model" unless src_filename
    model_basename = file_data[:model_basename] || src_basename
    model_basename.gsub!('+', ' ')  #plus signs are problematic
    #TODO: Consider creating TkEscape.unescape method
    model_basename = CGI.unescape(model_basename)
    content_type = file_data[:content_type] || MimeNew.for_ofc_x(model_basename)
    modified_time = file_data[:modified_time] || File.mtime(src_filename).to_s
    #create attachment class data structure
     = {}
    ['content_type'] = content_type
    ['file_modified'] = modified_time
    #read in file
    #TODO: reading the file in this way is memory intensive for large files, chunking it up would be better
    file_data = File.open(src_filename, "rb") {|f| f.read}
    attachment_package[model_basename] = {'data' => file_data, 'md' => }
  end
  #attachment package has now been created
  #create the attachment record
  #The attachment handler (bia_class) will deal with creating vs updating
  user_id = node.my_GlueEnv.user_id
  node_id = node.[:_id]
  #TODO: There is probably a cleaner way to do add attachments, but low on the priority list
  record = bia_class.add_attachment_package(node_id, bia_class, attachment_package)
  
  #get the basenames we just stored
  stored_basenames = record['_attachments'].keys
  if node.respond_to? :attachment_doc_id
    #make sure the objects attachment id matches the persistence layer's record id
    if node.attachment_doc_id && (node.attachment_doc_id != record['_id'] )
      raise "Attachment ID mismatch, current id: #{node.attachment_doc_id} new id: #{record['_id']}"
    #if the attachment id doesn't exist, create it
    elsif node.attachment_doc_id.nil?
      node.attachment_doc_id = record['_id']  #TODO is it nil after all attachs are deleted?
    else
      #we will reach here when everything is fine but we don't need to do anything
    end
  else #it's a new attachment and the attachment id has not been set, so we create and set it
    node.__set_userdata_key(:attachment_doc_id,  record['_id'] )
  end
  stored_basenames
end

#add_raw_data(node, attach_name, content_type, raw_data, file_modified_at = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/glue_envs/couchrest/couchrest_files_mgr.rb', line 86

def add_raw_data(node, attach_name, content_type, raw_data, file_modified_at = nil)
  bia_class = node.my_GlueEnv.moab_data[:attachClass]
   = {}
  if file_modified_at
    ['file_modified'] = file_modified_at
  else
    ['file_modified'] = Time.now.to_s
  end
  ['content_type'] = content_type 
  attachment_package = {}
  unesc_attach_name = TkEscape.unescape(attach_name)
  attachment_package[unesc_attach_name] = {'data' => raw_data, 'md' => }
  node_id = node.[:_id]
  record = bia_class.add_attachment_package(node_id, bia_class, attachment_package)
  if node.respond_to? :attachment_doc_id
    if node.attachment_doc_id && (node.attachment_doc_id != record['_id'] )
      raise "Attachment ID mismatch, current id: #{node.attachment_doc_id} new id: #{record['_id']}"
    elsif node.attachment_doc_id.nil?
      node.attachment_doc_id = record['_id']  #TODO How is it nil?
    end
  else
    node.__set_userdata_key(:attachment_doc_id,  record['_id'] )
  end
  [attach_name]
end

#get_attachments_metadata(node) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/glue_envs/couchrest/couchrest_files_mgr.rb', line 141

def (node)
  bia_class = node.my_GlueEnv.moab_data[:attachClass]
  node_id = node.[:_id]
  bia_doc_id = bia_class.uniq_att_doc_id(node_id)
  bia_doc = bia_class.get(bia_doc_id)
  atts = bia_doc.get_attachments
  md_symified = {}
  atts.each do |k,v|
    v_symified = HashKeys.str_to_sym(v)
    md_symified[k] = v_symified
  end
  md_symified
end

#get_raw_data(node, model_basename) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/glue_envs/couchrest/couchrest_files_mgr.rb', line 127

def get_raw_data(node, model_basename)
  bia_class = node.my_GlueEnv.moab_data[:attachClass]
  node_id = node.[:_id]
  bia_doc_id = bia_class.uniq_att_doc_id(node_id)
  bia_doc = bia_class.get(bia_doc_id)
  return nil unless bia_doc
  begin
    rtn = bia_doc.fetch_attachment(model_basename)
  rescue RestClient::ResourceNotFound
    return nil
  end
  rtn
end

#list(node) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/glue_envs/couchrest/couchrest_files_mgr.rb', line 155

def list(node)
  bia_class = node.my_GlueEnv.moab_data[:attachClass]
  node_id = node.[:_id]
  bia_doc_id = bia_class.uniq_att_doc_id(node_id)
  bia_doc = bia_class.get(bia_doc_id)
  return nil unless bia_doc
  bia_class.get_attachments(bia_doc).keys
end

#subtract(node, file_basenames) ⇒ Object

to conform with files_mgr



113
114
115
# File 'lib/glue_envs/couchrest/couchrest_files_mgr.rb', line 113

def subtract(node, file_basenames)
  subtract_files(node, file_basenames)
end

#subtract_all(node, bia_class) ⇒ Object

TODO: Validate that we want to remove the files container TODO: does attachment_doc_id exist any more?



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/glue_envs/couchrest/couchrest_files_mgr.rb', line 180

def subtract_all(node, bia_class)
  #delete the attachment record
  doc_db = node.my_GlueEnv.moab_data[:db]
  
  bia_class = node.my_GlueEnv.moab_data[:attachClass]
  node_id = node.[:_id]
  bia_doc_id = bia_class.uniq_att_doc_id(node_id)
  if bia_doc_id
    attach_doc = bia_class.get(bia_doc_id)
    doc_db.delete_doc(attach_doc)
    #node.__unset_userdata_key(:attachment_doc_id)
    #node.__save
  else
    puts "Warning: Attempted to delete attachments when none existed"
  end
  node
end

#subtract_files(node, model_basenames) ⇒ Object

TODO Document the :all shortcut somewhere



118
119
120
121
122
123
124
125
# File 'lib/glue_envs/couchrest/couchrest_files_mgr.rb', line 118

def subtract_files(node, model_basenames)
  bia_class = node.my_GlueEnv.moab_data[:attachClass]
  if model_basenames == :all
    subtract_all(node, bia_class)
  else
    subtract_some(node, model_basenames, bia_class)
  end
end

#subtract_some(node, model_basenames, bia_class) ⇒ Object

TODO: make private



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/glue_envs/couchrest/couchrest_files_mgr.rb', line 165

def subtract_some(node, model_basenames, bia_class)
  bia_class = node.my_GlueEnv.moab_data[:attachClass]
  node_id = node.[:_id]
  bia_doc_id = bia_class.uniq_att_doc_id(node_id)
  if bia_doc_id
    bia_doc = bia_class.get(bia_doc_id)
    raise "No attachment handler found for node: #{node.inspect}" unless bia_doc
    bia_doc.remove_attachment(model_basenames)
    rem_atts = bia_doc.get_attachments
    subtract_all(node, bia_class) if rem_atts.empty?
  end
end