Class: Knitkit::ErpApp::Desktop::FileAssetsController

Inherits:
ErpApp::Desktop::FileManager::BaseController
  • Object
show all
Defined in:
app/controllers/knitkit/erp_app/desktop/file_assets_controller.rb

Direct Known Subclasses

ImageAssetsController

Instance Method Summary collapse

Instance Method Details

#base_pathObject



10
11
12
13
14
15
16
17
18
# File 'app/controllers/knitkit/erp_app/desktop/file_assets_controller.rb', line 10

def base_path          
  if @root_node.nil?
    @base_path = nil
  else
    @base_path = File.join(@file_support.root, @root_node) 
  end

  @base_path
end

#create_fileObject



29
30
31
32
33
34
35
36
# File 'app/controllers/knitkit/erp_app/desktop/file_assets_controller.rb', line 29

def create_file
  path = params[:path] == 'root_node' ? base_path : params[:path]
  name = params[:name]

  @assets_model.add_file('#Empty File', File.join(path, name))

  render :json => {:success => true}
end

#create_folderObject



38
39
40
41
42
43
44
45
46
# File 'app/controllers/knitkit/erp_app/desktop/file_assets_controller.rb', line 38

def create_folder
  path = (params[:path] == 'root_node') ? base_path : params[:path]
  name = params[:name]

  path = File.join(@file_support.root,path) if path.index(@file_support.root).nil?

  @file_support.create_folder(path, name)
  render :json => {:success => true}
end

#delete_fileObject



107
108
109
110
111
112
113
114
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
146
147
148
# File 'app/controllers/knitkit/erp_app/desktop/file_assets_controller.rb', line 107

def delete_file
  messages = []

  if @context == Website
    capability_type = "view"
    capability_resource = "SiteFileAsset"
  else
    capability_type = "delete"
    capability_resource = "GlobalFileAsset"
  end

  nodes_to_delete = (params[:selected_nodes] ? JSON(params[:selected_nodes]) : [params[:node]])

  begin
    result = false
    nodes_to_delete.each do |path|
      current_user.with_capability(capability_type, capability_resource) do
        path = "#{path}/" if params[:leaf] == 'false' and path.match(/\/$/).nil?                
        begin
          name = File.basename(path)
          result, message, is_folder = @file_support.delete_file(File.join(@file_support.root,path))
          if result and !is_folder
            file = @assets_model.files.find(:first, :conditions => ['name = ? and directory = ?', ::File.basename(path), ::File.dirname(path)])
            file.destroy
          end
          messages << message
        rescue Exception=>ex
          Rails.logger.error ex.message
          Rails.logger.error ex.backtrace.join("\n")
          render :json => {:success => false, :error => "Error deleting #{name}"} and return
        end
      end # end current_user.with_capability
    end # end nodes_to_delete.each
    if result
      render :json => {:success => true, :message => messages.join(',')}
    else
      render :json => {:success => false, :error => messages.join(',')}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex
    render :json => {:success => false, :message => ex.message}
  end
end

#expand_directoryObject



20
21
22
23
24
25
26
27
# File 'app/controllers/knitkit/erp_app/desktop/file_assets_controller.rb', line 20

def expand_directory
  if @assets_model.nil?
    render :json => []
  else
    path = (params[:node] == ROOT_NODE) ? base_path : params[:node]
    render :json => @file_support.build_tree(path, :file_asset_holder => @assets_model, :preload => true)
  end
end

#rename_fileObject



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/controllers/knitkit/erp_app/desktop/file_assets_controller.rb', line 150

def rename_file
  path = params[:node]
  name = params[:file_name]

  result, message = @file_support.rename_file(File.join(@file_support.root,path), name)
  if result
    file = @assets_model.files.find(:first, :conditions => ['name = ? and directory = ?', ::File.basename(path), ::File.dirname(path)])
    file.name = name
    file.save
  end

  render :json =>  {:success => true, :message => message}
end

#save_moveObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/knitkit/erp_app/desktop/file_assets_controller.rb', line 83

def save_move
  messages = []
  result = {}
  path = params[:node]
  new_parent_path = params[:parent_node]
  new_parent_path = @root_node if new_parent_path == ROOT_NODE
  
  nodes_to_move = (params[:selected_nodes] ? JSON(params[:selected_nodes]) : [params[:node]])
  begin
    nodes_to_move.each do |path|
      if ErpTechSvcs::Config.file_storage == :filesystem and !File.exists?(File.join(@file_support.root, path))
        result = {:success => false, :msg => 'File does not exist.'}
      else
        file = @assets_model.files.find(:first, :conditions => ['name = ? and directory = ?', ::File.basename(path), ::File.dirname(path)])
        result, message = file.move(new_parent_path)
      end
      messages << message
    end
    render :json => {:success => true, :msg => messages.join(',')}
  rescue Exception => e
   render :json => {:success => false, :msg => e.message}
  end
end

#update_securityObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/controllers/knitkit/erp_app/desktop/file_assets_controller.rb', line 164

def update_security
  path   = params[:path]
  secure = params[:secure]
  roles = []

  #get roles
  params.each do |k, v|
    if v == 'on'
      roles.push(k)
    end
  end

  file = @assets_model.files.where(['name = ? and directory = ?', ::File.basename(path), ::File.dirname(path)]).first

  if roles.empty?
    file.remove_capability(:download)
  else
    capability = file.add_capability(:download)
    capability.remove_all_roles
    roles.each do |r|
      role = SecurityRole.find_by_internal_identifier(r)
      role.add_capability(capability)
    end
  end
  
  # if we're using S3, set file permissions to private or public_read   
  @file_support.set_permissions(path, (file.is_secured? ? :private : :public_read)) if ErpTechSvcs::Config.file_storage == :s3
  
  render :json => {:success => true, :secured => file.is_secured?, :roles => file.roles.uniq.collect{|item| item.internal_identifier}}
end

#upload_fileObject



48
49
50
51
52
53
54
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
# File 'app/controllers/knitkit/erp_app/desktop/file_assets_controller.rb', line 48

def upload_file
  #Website level assets if allowed to be viewed can also be uploaded and deleted so this is only checking for the view capability
  if @context == Website
    capability_type = "view"
    capability_resource = "SiteFileAsset"
  else
    capability_type = "upload"
    capability_resource = "GlobalFileAsset"
  end

  model = DesktopApplication.find_by_internal_identifier('knitkit')
  begin
    current_user.with_capability(capability_type, capability_resource) do
      result = {}
      upload_path = params[:directory]
      name = params[:name]
      data = request.raw_post

      begin
        upload_path == 'root_node' ? @assets_model.add_file(data, File.join(base_path,name)) : @assets_model.add_file(data, File.join(@file_support.root,upload_path,name))
        result = {:success => true}
      rescue Exception=>ex
        logger.error ex.message
        logger.error ex.backtrace.join("\n")
        result = {:success => false, :error => "Error uploading file."}
      end

      #the awesome uploader widget whats this to mime type text, leave it render :inline
      render :inline => result.to_json
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex
    render :json => {:success => false, :message => ex.message}
  end
end