Module: Filemanager::Controller

Defined in:
lib/filemanager/controller.rb

Instance Method Summary (collapse)

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method_id, *args)

methods for view



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/filemanager/controller.rb', line 162

def method_missing(method_id, *args)
  method_id_s = method_id.to_s
  if method_id_s[0, 3] == 'is_' && method_id_s[-1, 1] == '?'
    instance_eval %{
                      def #{method_id}(*args)
                        FM_#{method_id_s[3..-2].upcase}_TYPES.include?(@path_suffix)
                      end
                    }
    send(method_id, *args)
  else
    super
  end
end

Instance Method Details

- (Object) adjust_size

TODO



125
126
127
# File 'lib/filemanager/controller.rb', line 125

def adjust_size

end

- (Object) copy



77
78
79
80
81
# File 'lib/filemanager/controller.rb', line 77

def copy
  session[:source] = @source.map{|s| @current_path + File::SEPARATOR + s}
  session[:remove] = false
  success
end

- (Object) cut



83
84
85
86
87
# File 'lib/filemanager/controller.rb', line 83

def cut
  session[:source] = @source.map{|s| @current_path + File::SEPARATOR + s}
  session[:remove] = true
  success
end

- (Object) decode(target)



192
193
194
# File 'lib/filemanager/controller.rb', line 192

def decode(target)
  transfer(FM_ENCODING_TO, FM_ENCODING_FROM, target);
end

- (Object) download



102
103
104
105
106
107
108
109
110
# File 'lib/filemanager/controller.rb', line 102

def download
  now = Time.new
  now = "#{now.to_i}#{now.usec}"
  temp_file = FM_TEMP_DIR + File::SEPARATOR + now + '.zip'
  FileUtils.cd(@current_path) do |dir|
    system "zip -r #{temp_file} #{@source.map{|s| '"' + s + '"'}.join(' ')}"
  end
  send_file(temp_file)
end

- (Object) encode(target)



188
189
190
# File 'lib/filemanager/controller.rb', line 188

def encode(target)
  transfer(FM_ENCODING_FROM, FM_ENCODING_TO, target);
end

- (Object) error



151
152
153
# File 'lib/filemanager/controller.rb', line 151

def error()
  result("ERROR")
end

- (Object) file_content



44
45
46
# File 'lib/filemanager/controller.rb', line 44

def file_content
    File.size(@current_path) > 1000000 ? 'File too big' : File.read(@current_path)
end

- (Object) get_file_type(file)



206
207
208
209
210
211
212
213
214
# File 'lib/filemanager/controller.rb', line 206

def get_file_type(file)
  type = File.extname(file)

  unless type.blank?
    type = type.downcase[1..-1]
    return type if FM_SUPPORT_TYPES.include?(type)
  end
  FM_UNKNOWN_TYPE
end

- (Object) hsize(size)



196
197
198
199
200
201
202
203
204
# File 'lib/filemanager/controller.rb', line 196

def hsize(size)
  size = size/1024
  if size > 1024
    size = size/1024
    size = format('%0.2f',(size)) + ' mb'
  else
    size = format('%0.2f', size) + ' kb'
  end
end

- (Object) index



34
35
36
# File 'lib/filemanager/controller.rb', line 34

def index

end

- (Object) new_file



67
68
69
70
# File 'lib/filemanager/controller.rb', line 67

def new_file
  File.new(@current_path + File::SEPARATOR + decode(params[:new_name]), 'w')
  success
end

- (Object) new_folder



72
73
74
75
# File 'lib/filemanager/controller.rb', line 72

def new_folder
  Dir.mkdir(@current_path + File::SEPARATOR + decode(params[:new_name]))
  success
end

- (Object) paste



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/filemanager/controller.rb', line 89

def paste
  return error if session[:remove].nil? || session[:source].nil?
  begin
    session[:remove] == true ? FileUtils.mv(session[:source], @current_path) : FileUtils.cp_r(session[:source], @current_path)
    session[:remove] = nil
    session[:source] = nil
    success
  rescue => exception
    result(exception)
  end

end

- (Object) remove



62
63
64
65
# File 'lib/filemanager/controller.rb', line 62

def remove
  FileUtils.rm_rf(@source.map{|s| @current_path + File::SEPARATOR + s})
  success
end

- (Object) rename

def office

  render :action=>'excel' if is_excel?
  render :action=>'word' if is_word?
  render :action=>'ppt' if is_ppt?
  render :action=>'help' if is_help?
end


55
56
57
58
59
60
# File 'lib/filemanager/controller.rb', line 55

def rename
  old_name = @current_path + File::SEPARATOR + decode(params[:old_name])
  new_name = @current_path + File::SEPARATOR + decode(params[:new_name])
  File.rename(old_name, new_name)
  success
end

- (Object) result(message)



155
156
157
158
159
# File 'lib/filemanager/controller.rb', line 155

def result(message)
  respond_to do |wants|
    wants.js { render :text => message }
  end
end

- (Object) rotate

TODO



130
131
132
# File 'lib/filemanager/controller.rb', line 130

def rotate

end

- (Object) set_up



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/filemanager/controller.rb', line 10

def set_up
  @lock_path = FM_LOCK_PATH
  @source = params[:source]
  @source = decode(@source) unless @source.nil?
  @path = (params[:path].nil? || ! params[:path].index('..').nil?) ? '' : params[:path]
  @path = '' if @path == '/'
  @path = decode(@path)
  @resource_path = FM_RESOURCES_PATH
  @current_path = @resource_path + @path
  @current_file = (File.directory?(@current_path) ? Dir.new(@current_path) : File.new(@current_path))
  @parent_path = (!@path.blank? && !@path.rindex('/').nil?) ? (@path.rindex('/') == 0 ? '/' : @path[0..(@path.rindex('/')-1)]) : nil
  @path_suffix = @path.index('.').nil? || @path[-1] == '.' ? '' : @path[@path.index('.')+1..-1].downcase
  if File.directory?(@current_path)
    @all_files = Dir.entries(@current_path)
    @directories = @all_files.map{|f| File.directory?(@current_path + File::SEPARATOR + f) ? f : nil}.compact
    @files = @all_files.map{|f| File.directory?(@current_path + File::SEPARATOR + f) ? nil : f}.compact
    @file_total_size = @files.inject(0){|size, f| size + File.size(@current_path + File::SEPARATOR + f)}
  end
end

- (Object) success



147
148
149
# File 'lib/filemanager/controller.rb', line 147

def success
  result("SUCCESS")
end

- (Object) tear_off



30
31
32
# File 'lib/filemanager/controller.rb', line 30

def tear_off
  @current_file.close unless @current_file.nil?
end

- (Object) to_index



143
144
145
# File 'lib/filemanager/controller.rb', line 143

def to_index
  redirect_to :action => 'index', :path => encode(@path)
end

- (Object) transfer(from, to, target)



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/filemanager/controller.rb', line 176

def transfer(from, to, target)
  if FM_ENCODING_TO.nil?
    target
  else
    if target.is_a?(Array)
      target.map{|i| to.nil? ? i : Iconv.conv(to, from, i)}
    else
      Iconv.conv(to, from, target)
    end
  end
end

- (Object) unzip

TODO



135
136
137
138
139
140
141
# File 'lib/filemanager/controller.rb', line 135

def unzip
  filename = decode(params[:old_name])
  FileUtils.cd(@current_path) do |dir|
    system "unzip -o #{filename}"
  end
  to_index
end

- (Object) upload



113
114
115
116
117
118
119
120
# File 'lib/filemanager/controller.rb', line 113

def upload
  file = params[:upload]
  filename = decode(file.original_filename)
  File.open(@current_path + File::SEPARATOR + filename, "wb") do |f|
    f.write(file.read)
  end
  to_index
end

- (Object) view



38
39
40
41
42
# File 'lib/filemanager/controller.rb', line 38

def view
  #    respond_to do |wants|
  #      wants.js {  render :text => File.size(@current_path) > 1000000 ? 'File too big' : File.read(@current_path) }
  #    end
end