Class: Crackup::FileObject
- Inherits:
-
Object
- Object
- Crackup::FileObject
- Includes:
- FileSystemObject
- Defined in:
- lib/crackup/file_object.rb
Overview
Represents a file on the local filesystem.
Instance Attribute Summary collapse
-
#file_hash ⇒ Object
readonly
Returns the value of attribute file_hash.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Attributes included from FileSystemObject
Instance Method Summary collapse
-
#==(file) ⇒ Object
Compares the specified Crackup::FileObject to this one.
-
#initialize(filename) ⇒ FileObject
constructor
A new instance of FileObject.
-
#remove ⇒ Object
Removes this file from the remote location.
-
#restore(path) ⇒ Object
Restores the remote copy of this file to the local path specified by path.
-
#update ⇒ Object
Uploads this file to the remote location.
Methods included from FileSystemObject
Constructor Details
#initialize(filename) ⇒ FileObject
Returns a new instance of FileObject.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/crackup/file_object.rb', line 13 def initialize(filename) unless File.file?(filename) raise ArgumentError, "#{filename} is not a file" end super(filename) # Get the file's SHA256 hash. digest = Digest::SHA256.new File.open(filename, 'rb') do |file| while buffer = file.read(1048576) do digest << buffer end end @file_hash = digest.hexdigest() @url = "#{Crackup.driver.url}/crackup_#{@name_hash}" end |
Instance Attribute Details
#file_hash ⇒ Object (readonly)
Returns the value of attribute file_hash.
11 12 13 |
# File 'lib/crackup/file_object.rb', line 11 def file_hash @file_hash end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
11 12 13 |
# File 'lib/crackup/file_object.rb', line 11 def url @url end |
Instance Method Details
#==(file) ⇒ Object
Compares the specified Crackup::FileObject to this one. Returns false if file is different, true if file is the same. The comparison is performed using an SHA256 hash of the file contents.
36 37 38 |
# File 'lib/crackup/file_object.rb', line 36 def ==(file) return file.name == @name && file.file_hash == @file_hash end |
#remove ⇒ Object
Removes this file from the remote location.
41 42 43 44 |
# File 'lib/crackup/file_object.rb', line 41 def remove Crackup.debug "--> #{@name}" Crackup.driver.delete(@url) end |
#restore(path) ⇒ Object
Restores the remote copy of this file to the local path specified by path.
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 |
# File 'lib/crackup/file_object.rb', line 48 def restore(path) path = path.chomp('/') + '/' + File.dirname(@name).delete(':') filename = path + '/' + File.basename(@name) Crackup.debug "--> #{filename}" # Create the path if it doesn't exist. unless File.directory?(path) begin FileUtils.mkdir_p(path) rescue => e raise Crackup::Error, "Unable to create local directory: #{path}" end end # Download the remote file. tempfile = Crackup.get_tempfile() Crackup.driver.get(@url, tempfile) # Decompress/decrypt the file. if Crackup.[:passphrase].nil? Crackup.decompress_file(tempfile, filename) else Crackup.decrypt_file(tempfile, filename) end end |
#update ⇒ Object
Uploads this file to the remote location.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/crackup/file_object.rb', line 76 def update Crackup.debug "--> #{@name}" # Compress/encrypt the file. tempfile = Crackup.get_tempfile() if Crackup.[:passphrase].nil? Crackup.compress_file(@name, tempfile) else Crackup.encrypt_file(@name, tempfile) end # Upload the file. Crackup.driver.put(@url, tempfile) end |