Class: MysqlInterface::FilesMgr

Inherits:
Object
  • Object
show all
Defined in:
lib/glue_envs/mysql/mysql_files_mgr.rb

Constant Summary collapse

MySqlPrimaryKey =

Table Structure

'__pkid-file'
NodeName =
'node_name'
Basename =
'basename'
ContentType =
'content_type'
ModifiedAt =
'modified_at'
RawContent =
'raw_content'
FileTableKeys =
[MySqlPrimaryKey, NodeName, Basename, ContentType, ModifiedAt, RawContent]
TablePostFix =

TODO: See if you can get away from that

"_files"
@@home_dir =
ENV["HOME"]
@@my_pw =
File.open("#{@@home_dir}/.locker/tinkit_mysql"){|f| f.read}.strip

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(glue_env, node_key_value) ⇒ FilesMgr

Returns a new instance of FilesMgr.



31
32
33
34
# File 'lib/glue_envs/mysql/mysql_files_mgr.rb', line 31

def initialize(glue_env, node_key_value)
  @dbh = self.class.dbh
  @file_table_name = glue_env.file_mgr_table
end

Class Attribute Details

.dbhObject

Returns the value of attribute dbh.



10
11
12
# File 'lib/glue_envs/mysql/mysql_files_mgr.rb', line 10

def dbh
  @dbh
end

Instance Attribute Details

#file_table_nameObject

options: include in model_save_params, or let base node pass on methods to underlying glue env (probably this)



29
30
31
# File 'lib/glue_envs/mysql/mysql_files_mgr.rb', line 29

def file_table_name
  @file_table_name
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
# File 'lib/glue_envs/mysql/mysql_files_mgr.rb', line 36

def add(node, file_datas)
  filenames = []
  file_datas.each do |file_data|
    filenames << file_data[:src_filename]
  end
  
  filenames.each do |filename|
    #p File.open(filename, 'rb'){|f| f.read}
    basename = File.basename(filename)
    #derive content_type
    content_type = MimeNew.for_ofc_x(basename)
    #derive modified time from file
    modified_at = File.mtime(filename).to_s
    rb = 'rb' #lazily avoiding escape issues
    node_name = node.__send__(node.my_GlueEnv.model_key.to_sym)
    fields_str =   "`#{NodeName}`, `#{Basename}`, `#{ContentType}`, `#{ModifiedAt}`, `#{RawContent}`"
    prep_sql = "REPLACE INTO `#{@file_table_name}` (#{fields_str})
    VALUES ( ?, ?, ?, ?, ?)"
    sth = @dbh.prepare(prep_sql)
    values_input = [node_name, basename, content_type, modified_at, File.open(filename, rb){|f| f.read}]
    sth.execute(*values_input)
  end
  filenames.map {|f| TkEscape.escape(File.basename(f))} #return basenames
end

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/glue_envs/mysql/mysql_files_mgr.rb', line 61

def add_raw_data(node, attach_name, content_type, raw_data, file_modified_at = nil)
  raise "No Data provided for file" unless raw_data
  if file_modified_at
    modified_at = file_modified_at
  else
    modified_at = Time.now.to_s
  end
  
  attachment_package = {}
  node_name = node.__send__(node.my_GlueEnv.model_key.to_sym)
  fields_str =   "`#{NodeName}`, `#{Basename}`, `#{ContentType}`, `#{ModifiedAt}`, `#{RawContent}`"
  prep_sql = "REPLACE INTO `#{@file_table_name}` (#{fields_str})
  VALUES ( ?, ?, ?, ?, ?)"
  sth = @dbh.prepare(prep_sql)
  values_input = [node_name, attach_name, content_type, modified_at, raw_data]
  sth.execute(*values_input) 
  return [attach_name]
end

#get_attachments_metadata(node) ⇒ Object

todo change name to get_files_metadata



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/glue_envs/mysql/mysql_files_mgr.rb', line 115

def (node)
  files_md = {}
  md_list = FileTableKeys
  md_list.delete(RawContent)
  md_fields = md_list.join("`, `")
    
  model_key = node.my_GlueEnv.model_key
  sql = "SELECT `#{md_fields}` FROM `#{@file_table_name}`
   WHERE `#{NodeName}` = '#{node.__send__(model_key.to_sym)}'"
  sth = @dbh.prepare(sql)
  rtn = []
  sth.execute
  while row=sth.fetch do
    rtn << row.to_h
  end
  #rtn
  sth.finish
  objects = rtn
  objects.each do |object|
    obj_md = object 
    #speputs "Obj It: #{obj_md.inspect}"
    obj_md_file_modified = obj_md["modified_at"]
    obj_md_content_type = obj_md["content_type"]
    new_md = {:content_type => obj_md_content_type, :file_modified => obj_md_file_modified}
    new_md.merge(obj_md)  #where does the original metadata go?
    #p new_md.keys
    files_md[obj_md["basename"]] = new_md
    #puts "Obj METADATA: #{new_md.inspect}"
  end
  files_md
end

#get_raw_data(node, file_basename) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/glue_envs/mysql/mysql_files_mgr.rb', line 96

def get_raw_data(node, file_basename)
  model_key = node.my_GlueEnv.model_key
  sql = "SELECT `#{RawContent}` FROM `#{@file_table_name}`
   WHERE `#{NodeName}` = '#{node.__send__(model_key.to_sym)}'
   AND `#{Basename}` = '#{file_basename}'"
  #puts "Raw Data SQL: #{sql}"
  sth = @dbh.prepare(sql)
  rtn = []
  sth.execute
  while row=sth.fetch do
    rtn << row.to_h
  end
  #rtn
  sth.finish
  rtn_val = rtn.first || {} #remember in production to sort on internal primary id (once delete revisions works)
  rtn_val['raw_content'] 
end

#list(node) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/glue_envs/mysql/mysql_files_mgr.rb', line 80

def list(node)
  model_key = node.my_GlueEnv.model_key
  
  sql = "SELECT `#{Basename}` FROM `#{@file_table_name}`
   WHERE `#{NodeName}` = '#{node.__send__(model_key.to_sym)}'"
  sth = @dbh.prepare(sql)
  rtn = []
  sth.execute
  while row=sth.fetch do
    rtn << row.to_h
  end
  #rtn
  sth.finish
  basenames =  rtn.map{|basename_hash| basename_hash.values}.flatten
end

#subtract(node, file_basenames) ⇒ Object

def



147
148
149
150
151
152
153
# File 'lib/glue_envs/mysql/mysql_files_mgr.rb', line 147

def subtract(node, file_basenames)
  if file_basenames == :all
    subtract_all(node)
  else
    subtract_some(node, file_basenames)
  end
end

#subtract_all(node) ⇒ Object



155
156
157
158
159
160
# File 'lib/glue_envs/mysql/mysql_files_mgr.rb', line 155

def subtract_all(node)
  model_key = node.my_GlueEnv.model_key
  sql = "DELETE FROM `#{@file_table_name}`
        WHERE `#{NodeName}` = '#{node.__send__(model_key.to_sym)}'"
  @dbh.do(sql)
end

#subtract_some(node, file_basenames) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/glue_envs/mysql/mysql_files_mgr.rb', line 162

def subtract_some(node, file_basenames)
  file_basenames = [file_basenames].flatten
  model_key = node.my_GlueEnv.model_key
  #probalby get better performance by changing the sql match query
  #rather than iterating
  file_basenames.each do |file_basename|
    sql = "DELETE FROM `#{@file_table_name}`
        WHERE `#{NodeName}` = '#{node.__send__(model_key.to_sym)}'
        AND `#{Basename}` = '#{file_basename}'"
   
    @dbh.do(sql)
  end
end