Class: FilesystemInterface::FilesMgr

Inherits:
Object
  • Object
show all
Defined in:
lib/glue_envs/filesystem/filesystem_files_mgr.rb

Constant Summary collapse

@@log =

Set Logger

TinkitLog.set(self.name)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_env, node_key) ⇒ FilesMgr

Returns a new instance of FilesMgr.



50
51
52
53
54
# File 'lib/glue_envs/filesystem/filesystem_files_mgr.rb', line 50

def initialize(node_env, node_key)
  #for bufs node_key is the value of :my_category
  @node_key = node_key
  @attachment_location = File.join(node_env.user_datastore_location, node_key.to_s)
end

Instance Attribute Details

#attachment_locationObject

Returns the value of attribute attachment_location.



39
40
41
# File 'lib/glue_envs/filesystem/filesystem_files_mgr.rb', line 39

def attachment_location
  @attachment_location
end

#attachment_packagesObject

Returns the value of attribute attachment_packages.



39
40
41
# File 'lib/glue_envs/filesystem/filesystem_files_mgr.rb', line 39

def attachment_packages
  @attachment_packages
end

Class Method Details

.get_att_doc(node) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/glue_envs/filesystem/filesystem_files_mgr.rb', line 41

def self.get_att_doc(node)
  root_path = node.my_GlueEnv.user_datastore_location
  #my_cat dependency
  node_loc  = node._user_data[node.my_GlueEnv.node_key]
  node_path = File.join(root_path, node_loc)
  model_basenames = Dir.working_entries(node_path)
  filenames = model_basenames.map{|b| File.join(node_path, TkEscape.escape(b))}
end

Instance Method Details

#add(node, file_datas) ⇒ Object

TODO: Is passing node in methods duplicative now that the moab FileMgr is bound to an env at initialization?



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/glue_envs/filesystem/filesystem_files_mgr.rb', line 58

def add(node, file_datas)
  filenames = []
  file_datas.each do |file_data|
    #TODO Validate file data before saving
    filenames << file_data[:src_filename]
  end
  filenames.each do |filename|
    my_dest_basename = TkEscape.escape(File.basename(filename))
    node_dir = @attachment_location
     #File.join(node.my_GlueEnv.user_datastore_selector, node.my_category)  #TODO: this should be node id, not my cat
    my_dest = File.join(node_dir, my_dest_basename)
    #FIXME: obj.attached_files is broken, list_attached_files should work
    #@attached_files << my_dest
    same_file = filename if filename == my_dest
    @@log.debug {"File model attachments:"} if @@log.debug?
    @@log.debug { "Copy #{filename} to #{my_dest} if #{same_file.nil?}"} if @@log.debug?
    #was breaking if the dest path didn't exist
    FileUtils.mkdir_p(File.dirname(my_dest)) unless File.exist?(File.dirname(my_dest))
    FileUtils.cp(filename, my_dest, :preserve => true, :verbose => false ) unless same_file
    #self.file_metadata = {filename => {'file_modified' => File.mtime(filename).to_s}}
  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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/glue_envs/filesystem/filesystem_files_mgr.rb', line 82

def add_raw_data(node, attach_name, content_type, raw_data, file_modified_at = nil)
  raise "No Data provided for file" unless raw_data
  #bia_class = @model_actor[:attachment_actor_class]
   = {}
  if file_modified_at
    ['file_modified'] = file_modified_at
  else
    ['file_modified'] = Time.now.to_s
  end
  ['content_type'] = content_type #TODO: is unknown content handled gracefully?
  attachment_package = {}
  esc_attach_name = TkEscape.escape(attach_name)
  node_path = @attachment_location
  FileUtils.mkdir_p(node_path) unless File.exist?(node_path)
  raw_data_filename = File.join(node_path, esc_attach_name)
  File.open(raw_data_filename, 'wb'){|f| f.write(raw_data)}
  if file_modified_at
    File.utime(Time.parse(file_modified_at), Time.parse(file_modified_at), raw_data_filename)
  else
    file_modified_at = File.mtime(raw_data_filename).to_s     
  end
  [esc_attach_name]
end

#get_attachments_metadata(node) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/glue_envs/filesystem/filesystem_files_mgr.rb', line 139

def (node)
  att_md = {}
  node_dir = @attachment_location
  att_basenames = Dir.working_entries(node_dir)
  att_basenames.each do |att|
    file_md = {}

    filename = File.join(node_dir, att)
    file_md[:file_modified] = File.mtime(filename).to_s
    file_md[:content_type] = MimeNew.for_ofc_x(filename)
    att_md[att.to_sym] = file_md
  end
  att_md
end

#get_raw_data(node, model_basename) ⇒ Object



132
133
134
135
136
137
# File 'lib/glue_envs/filesystem/filesystem_files_mgr.rb', line 132

def get_raw_data(node, model_basename)
  node_dir = @attachment_location
  filename = File.join(node_dir, model_basename)
  return nil unless File.exist?(filename)
  File.open(filename, "r"){|f| f.read}
end

#list(node) ⇒ Object

Not used and I don’t think it will work anyway



155
156
157
# File 'lib/glue_envs/filesystem/filesystem_files_mgr.rb', line 155

def list(node)
  Dir.working_entries(@attachment_location)
end

#subtract(node, model_basenames) ⇒ Object

TODO Document the :all shortcut somewhere



107
108
109
110
111
112
113
# File 'lib/glue_envs/filesystem/filesystem_files_mgr.rb', line 107

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

#subtract_all(node) ⇒ Object

TODO: make private



125
126
127
128
129
130
# File 'lib/glue_envs/filesystem/filesystem_files_mgr.rb', line 125

def subtract_all(node)
  node_path = @attachment_location
  attached_entries = Dir.working_entries(node_path)
  attached_filenames = attached_entries.map{|e| File.join(node_path, e)}
  FileUtils.rm(attached_filenames)
end

#subtract_some(node, file_basenames) ⇒ Object

TODO: make private



116
117
118
119
120
121
122
123
# File 'lib/glue_envs/filesystem/filesystem_files_mgr.rb', line 116

def subtract_some(node, file_basenames)
  file_basenames = [file_basenames].flatten
  model_key = node.my_GlueEnv.model_key

  node_path = @attachment_location
  filenames = file_basenames.map{|b| File.join(node_path, TkEscape.escape(b))}
  FileUtils.rm_f(filenames)
end