Class: Uploader::FileSystemService

Inherits:
Service show all
Defined in:
lib/uploader/filesystem_service.rb

Instance Method Summary collapse

Methods inherited from Service

#close!, #initialize, #initialized?, is_registered?, register, services

Constructor Details

This class inherits a constructor from Uploader::Service

Instance Method Details

#connect!Object



11
12
# File 'lib/uploader/filesystem_service.rb', line 11

def connect!
end

#copy_contents_to_file_system(remote_path, contents, binary = true) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/uploader/filesystem_service.rb', line 38

def copy_contents_to_file_system(remote_path, contents, binary=true)
  destination = Path.new(@config.destination) + remote_path.relative

  if @config.verbose?
    $stderr.puts %Q{Copying #{GREEN}#{remote_path}#{ENDC} to the file system
  --> #{destination}} 
  else
    $stderr.print "."
  end

  begin
    f = File.open(destination.absolute, "w+")

  rescue => detail
    $stderr.puts detail.message if @config.verbose?
    $stderr.puts "Attempting to create the path." if @config.verbose?

    # Assume the directories leading to the file don't exist. Create them.
    FileUtils::mkdir_p(destination.dirname)

    f = File.open(destination.absolute, "w+")
  end

  unless f.nil?
    f.syswrite(contents)
    f.close
    $stderr.puts "Done with #{destination.relative}." if @config.verbose?
  end
end

#get_upload_contents(contents) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/uploader/filesystem_service.rb', line 22

def get_upload_contents(contents)
  binmode = false
  if contents.is_a?(File)
    contents_to_upload = contents.read
    binmode = true

  elsif contents.is_a?(String)
    contents_to_upload = contents

  else
    raise UnrecognizedContentType
  end

  return contents_to_upload, binmode
end

#upload(remote_path, contents) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/uploader/filesystem_service.rb', line 14

def upload(remote_path, contents)
  raise UploadArgumentError if not remote_path.is_a?(Path)

  contents_to_upload, binmode = get_upload_contents(contents)

  copy_contents_to_file_system(remote_path, contents_to_upload, binmode)
end