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_tree(starting_path, options = {}) ⇒ Object



103
104
105
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 103

def build_tree(starting_path, options={})
  find_node(starting_path, options)
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



19
20
21
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 19

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

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



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

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)
        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)


41
42
43
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 41

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

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 107

def find_node(path, options={})
  parent = if options[:file_asset_holder]
    super
  else
    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
  end

  parent
end

#get_contents(path) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 92

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 45

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/erp_tech_svcs/file_support/file_system_manager.rb', line 23

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