Class: Crackup::Driver::FileDriver

Inherits:
Object
  • Object
show all
Includes:
Crackup::Driver
Defined in:
lib/crackup/drivers/file.rb

Overview

Filesystem storage driver for Crackup.

Author

Ryan Grove ([email protected])

Copyright

Copyright © 2006 Ryan Grove. All rights reserved.

License

New BSD License (opensource.org/licenses/bsd-license.php)

Instance Attribute Summary

Attributes included from Crackup::Driver

#url

Instance Method Summary collapse

Methods included from Crackup::Driver

get_driver, #initialize

Instance Method Details

#delete(url) ⇒ Object

Deletes the file at the specified url.



17
18
19
20
21
22
23
# File 'lib/crackup/drivers/file.rb', line 17

def delete(url)
  File.delete(get_path(url))
  return true
  
rescue => e
  raise Crackup::StorageError, "Unable to delete #{url}: #{e}"
end

#get(url, local_filename) ⇒ Object

Downloads the file at url to local_filename.



26
27
28
29
30
31
32
# File 'lib/crackup/drivers/file.rb', line 26

def get(url, local_filename)
  FileUtils::copy(get_path(url), local_filename)
  return true

rescue => e
  raise Crackup::StorageError, "Unable to get #{url}: #{e}"
end

#get_path(url) ⇒ Object

Gets the filesystem path represented by url. This method is capable of parsing URLs in any of the following formats:

  • file:///foo/bar

  • file://c:/foo/bar

  • c:/foo/bar

  • /foo/bar

  • //smbhost/foo/bar



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/crackup/drivers/file.rb', line 42

def get_path(url)
  uri  = URI::parse(url)
  path = ''
  
  if uri.scheme =~ /^[a-z]$/i
    # Windows drive letter.
    path = uri.scheme + ':'
  elsif uri.host =~ /^[a-z]$/i
    # Windows drive letter.
    path = uri.host + ':'
  elsif uri.scheme.nil? && !uri.host.nil?
    # SMB share.
    path = '//' + uri.host
  end
  
  return path += uri.path
  
rescue => e
  raise Crackup::StorageError, "Invalid URL: #{url}"
end

#put(url, local_filename) ⇒ Object

Uploads the file at local_filename to url.



64
65
66
67
68
69
70
# File 'lib/crackup/drivers/file.rb', line 64

def put(url, local_filename)
  FileUtils::copy(local_filename, get_path(url))
  return true
  
rescue => e
  raise Crackup::StorageError, "Unable to put #{url}: #{e}"
end