Class: Jaxx::Upload

Inherits:
Object
  • Object
show all
Defined in:
lib/jaxx/upload.rb

Constant Summary collapse

DEFAULT_RETRIES =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Upload

Returns a new instance of Upload.



11
12
13
14
15
# File 'lib/jaxx/upload.rb', line 11

def initialize args = {}
  @process  = Process.new(args.merge('validations' => [:privacy, :file_exists, :file_presence]))
  @filename = args['filename']
  @retries  = args['retries'] || DEFAULT_RETRIES
end

Instance Attribute Details

#processObject (readonly)

Returns the value of attribute process.



9
10
11
# File 'lib/jaxx/upload.rb', line 9

def process
  @process
end

#retriesObject (readonly)

Returns the value of attribute retries.



9
10
11
# File 'lib/jaxx/upload.rb', line 9

def retries
  @retries
end

Instance Method Details

#executeObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jaxx/upload.rb', line 29

def execute
  process.start do |storage|
    dir, attempts = remote_directory(storage), 0

    files.each do |file, name|
      stat, exc = false, nil
      attempts += 1 

      begin
        stat = create_file dir, file, name
      rescue => ex
        exc = ex
      end

      if !stat and attempts >= self.retries
        raise("File process failed: #{file}:#{name} : #{exc.message rescue nil}") 
      elsif !stat
        redo
      end

      attempts = 0
    end
  end

end

#filenameObject



17
18
19
# File 'lib/jaxx/upload.rb', line 17

def filename
  @filename || File.basename(process.file)
end

#filesObject



21
22
23
24
25
26
27
# File 'lib/jaxx/upload.rb', line 21

def files
  if File.directory?(process.file)
    Dir[File.join(process.file, "**", "*")].reject{|fp| File.directory?(fp) }.inject({}) {|hsh, fp| hsh[fp] = fp.gsub(process.file, ''); hsh }
  else
    { process.file => filename }
  end
end