Class: ErpTechSvcs::FileSupport::FileSystemManager

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

Constant Summary collapse

REMOVE_FILES_REGEX =
/^\./

Constants inherited from Manager

Manager::FILE_DOES_NOT_EXIST, Manager::FILE_FOLDER_DOES_NOT_EXIST, Manager::FOLDER_IS_NOT_EMPTY

Instance Method Summary collapse

Methods inherited from Manager

#build_file_assets_tree_for_model, find_parent, #insert_folders, #sync

Instance Method Details

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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 153

def build_node(path, options={})
  if File.directory?(path)
    if options[:preload]
      build_tree_for_directory(path, options) if options[:preload]
    else
      path.gsub!(root, '') unless options[:keep_full_path]

      {:text => path.split('/').last, :id => path, :iconCls => 'icon-content'}
    end
  else
    path.gsub!(root, '') unless options[:keep_full_path]

    parts = path.split('/')
    parts.pop
    download_path = parts.join('/')

    if !options[:included_file_extensions_regex].nil? && entry =~ options[:included_file_extensions_regex]
      {:text => path.split('/').last, :leaf => true, :iconCls => 'icon-document', :downloadPath => download_path, :id => path}
    elsif options[:included_file_extensions_regex].nil?
      {:text => path.split('/').last, :leaf => true, :iconCls => 'icon-document', :downloadPath => download_path, :id => path}
    end

  end
end

#build_tree(starting_path, options = {}) ⇒ Object



118
119
120
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 118

def build_tree(starting_path, options={})
  find_node(starting_path, options)
end

#copy_file(origination_file, path, name) ⇒ Object

copy a file



20
21
22
23
24
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 20

def copy_file(origination_file, path, name)
  contents = get_contents(origination_file)

  create_file(path, name, contents)
end

#create_file(path, name, contents) ⇒ Object



14
15
16
17
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 14

def create_file(path, name, contents)
  FileUtils.mkdir_p path unless File.exists? path
  File.open(File.join(path, name), 'wb+') { |f| f.write(contents) }
end

#create_folder(path, name) ⇒ Object



26
27
28
29
30
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 26

def create_folder(path, name)
  unless File.directory? File.join(path, name)
    FileUtils.mkdir_p File.join(path, name)
  end
end

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



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
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 72

def delete_file(path, options={})
  result = false
  name = File.basename(path)
  is_directory = false
  if !File.exists? path and !File.directory? path
    message = FILE_FOLDER_DOES_NOT_EXIST
  else
    if File.directory? path
      is_directory = true
      entries = Dir.entries(path)
      entries.delete_if { |entry| entry =~ REMOVE_FILES_REGEX }
      if entries.count > 0 && !options[:force]
        message = FOLDER_IS_NOT_EMPTY
        result = false
      else
        FileUtils.rm_rf(path)

        child_files = FileAsset.where(FileAsset.arel_table[:directory].matches("#{path.gsub(root, '')}%"))
        child_files.each do |file|
          file.destroy
        end

        message = "Folder #{name} was deleted #{name} successfully"
        result = true
      end
    else
      FileUtils.rm_rf(path)
      message = "File #{name} was deleted #{name} successfully"
      result = true
    end
  end

  return result, message, is_directory
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 50

def exists?(path)
  File.exists? path
end

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 122

def find_node(path, options={})
  if options[:file_asset_holder]
    super
  else
    if File.exists? path
      if File.directory? path
        path_pieces = path.split('/')
        parent = build_tree_for_directory(path, options)
        unless parent[:id] == path
          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
        end

        parent = nil if parent[:id] != path
        parent
      else
        build_node(path, options)
      end
    else
      nil
    end
  end
end

#get_contents(path) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 107

def get_contents(path)
  contents = nil
  message = nil
  unless File.exists? path
    message = FILE_DOES_NOT_EXIST
  else
    contents = File.open(path, 'rb') { |file| file.read }
  end
  return contents, message
end

#rename_file(path, name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 54

def rename_file(path, name)
  result = false
  unless File.exists? path
    message = FILE_DOES_NOT_EXIST
  else
    old_name = File.basename(path)
    path_pieces = path.split('/')
    path_pieces.delete(path_pieces.last)
    path_pieces.push(name)
    new_path = path_pieces.join('/')
    File.rename(path, new_path)
    message = "#{old_name} was renamed to #{name} successfully"
    result = true
  end

  return result, message
end

#rootObject



6
7
8
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 6

def root
  Rails.root.to_s
end

#save_move(path, new_parent_path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 32

def save_move(path, new_parent_path)
  old_path = File.join(path)
  new_path = File.join(Rails.root, new_parent_path)
  result = false
  unless File.exists? old_path
    message = FILE_DOES_NOT_EXIST
  else
    name = File.basename(path)
    #make sure path is there.
    FileUtils.mkdir_p new_path unless File.directory? new_path
    FileUtils.mv(old_path, File.join(new_path, name))
    message = "#{name} was moved to #{new_parent_path} successfully"
    result = true
  end

  return result, message
end

#update_file(path, content) ⇒ Object



10
11
12
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 10

def update_file(path, content)
  File.open(path, 'wb+') { |f| f.write(content) }
end