Class: ErpTechSvcs::FileSupport::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/erp_tech_svcs/file_support/manager.rb

Direct Known Subclasses

FileSystemManager, S3Manager

Constant Summary collapse

FOLDER_IS_NOT_EMPTY =
'Folder is not empty'
FILE_DOES_NOT_EXIST =
'File does not exist'
FILE_FOLDER_DOES_NOT_EXIST =
'File / Folder does not exist'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_parent(item, parents) ⇒ Object



9
10
11
12
13
14
# File 'lib/erp_tech_svcs/file_support/manager.rb', line 9

def find_parent(item, parents)
  parents.find do |parent|
    path = item[:path].split('/')[0..-2].join('/')
    parent[:id] == path
  end
end

Instance Method Details

#build_file_assets_tree_for_model(model, starting_path) ⇒ Object



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
85
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
111
112
113
114
115
116
117
118
# File 'lib/erp_tech_svcs/file_support/manager.rb', line 55

def build_file_assets_tree_for_model(model, starting_path)
  files = model.files.where('directory like ?', starting_path + '%')

  node_tree = [{:text => root, :leaf => false, :id => root, :children => []}]
  
  paths = files.collect{|file| File.join(file.directory,file.name)}

  node_tree.first[:children] << {:id => starting_path,
                                 :text => starting_path.split('/').last,
                                 leaf: true,
                                 iconCls: 'icon-document',
                                 :children => []} if paths.select{|path| path.split('/')[1] == starting_path.split('/')[1]}.empty?
  
  nesting_depth = paths.collect{|item| item.split('/').count}.max
  unless nesting_depth.nil?
    levels = []
    (1..nesting_depth).each do |i|
      current_items = []
      objects_this_depth = paths.collect{|item|
        text = item.split('/')[i - 1]
        path = item.split('/')[0..(i-1)].join('/')
        if item.split('/').count >= i && current_items.select{|item| item[:text] == text and item[:path] == path}.empty?
          item_hash = {:text => text, :path => path}
          current_items << item_hash
          item_hash
        end
      }
      objects_this_depth.delete_if{|item| (item.nil? or item[:text].blank?)}
      levels << objects_this_depth unless objects_this_depth.empty?
    end

    old_parents = []
    new_parents = [node_tree[0]]
    levels.each do |level|
      old_parents = new_parents
      new_parents = []
      level.each do |item|
        parent = old_parents.count == 1 ? old_parents.first : self.class.find_parent(item, old_parents)
        path = File.join(parent[:id], item[:text]).gsub(root, '')
        icon_cls = File.extname(item[:text]).blank? ? 'icon-content' : 'icon-document'
        child_hash = {:text => item[:text], :downloadPath => parent[:id], :iconCls => icon_cls, :leaf => !File.extname(item[:text]).blank?, :id => path, :children => []}
        
        #attempt set security if this file is a file_asset model
        file = files.find{|file| file.directory == parent[:id].gsub(root,'') and file.name == item[:text]}
        unless file.nil?
          child_hash[:isSecured] = file.is_secured?
          child_hash[:roles] = file.roles.collect{|r| r.internal_identifier}
          child_hash[:iconCls] = 'icon-document_lock' if child_hash[:isSecured]
          child_hash[:size] = file.data_file_size
          child_hash[:width] = file.width
          child_hash[:height] = file.height
          child_hash[:url] = file.url
        end

        new_parents << child_hash
        parent[:children] << child_hash
      end
    end
  end

  insert_folders(node_tree.first[:children])

  node_tree
end

#find_node(path, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/erp_tech_svcs/file_support/manager.rb', line 33

def find_node(path, options={})
  node_tree = build_file_assets_tree_for_model(options[:file_asset_holder], path.gsub(root, ''))

  path_pieces = path.split('/')
  parent = node_tree.first
  path_pieces.each do |path_piece|
    next if path_piece.blank?
    parent[:children].each do |child_node|
      if child_node[:text] == path_piece
        parent = child_node
        break
      end
    end
  end

  path.sub!(self.root,'') if parent[:id].scan(self.root).empty?
  path = File.join(self.root, path) if !parent[:id].scan(self.root).empty? and path.scan(self.root).empty?

  parent = [] if parent[:id] != path
  parent
end

#insert_folders(file_asset_nodes) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/erp_tech_svcs/file_support/manager.rb', line 120

def insert_folders(file_asset_nodes)
  file_asset_nodes.each do |child_asset_node|
    node = find_node(File.join(self.root,child_asset_node[:id]))
    unless node.nil?
      if node[:leaf]
        folders = []
      else
        folders = node[:children].select{|item| !item[:leaf]}
      end

      child_asset_node[:children] = [] if child_asset_node[:children].nil? && !folders.empty?
      folders.each do |folder|
        folder[:id].gsub!(self.root,'')
        child_asset_node[:children] << folder unless child_asset_node[:children].collect{|child_node| child_node[:text] }.include?(folder[:text])
      end
      insert_folders(child_asset_node[:children])
    end
  end unless file_asset_nodes.nil?
end

#sync(path, model) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/erp_tech_svcs/file_support/manager.rb', line 17

def sync(path, model)
  result = nil
  message = nil

  node = find_node(path)
  if node.nil?
    message = "Nothing to sync"
  else
    sync_node(node, model)
    message = "Sync successful"
    result = true
  end

  return result, message
end