Class: Gitpic::Uploader
- Inherits:
-
Object
- Object
- Gitpic::Uploader
- Defined in:
- lib/gitpic/uploader.rb
Instance Attribute Summary collapse
-
#base_path ⇒ Object
Returns the value of attribute base_path.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #black_list ⇒ Object
- #check_file(checksum) ⇒ Object
-
#initialize(directory, token) ⇒ Uploader
constructor
A new instance of Uploader.
- #photo_extension?(file_name) ⇒ Boolean
- #post_file(path) ⇒ Object
- #upload!(this_path = nil) ⇒ Object
Constructor Details
#initialize(directory, token) ⇒ Uploader
Returns a new instance of Uploader.
6 7 8 9 |
# File 'lib/gitpic/uploader.rb', line 6 def initialize(directory, token) @base_path = File.(directory) @token = token end |
Instance Attribute Details
#base_path ⇒ Object
Returns the value of attribute base_path.
4 5 6 |
# File 'lib/gitpic/uploader.rb', line 4 def base_path @base_path end |
#token ⇒ Object
Returns the value of attribute token.
4 5 6 |
# File 'lib/gitpic/uploader.rb', line 4 def token @token end |
Instance Method Details
#black_list ⇒ Object
70 71 72 |
# File 'lib/gitpic/uploader.rb', line 70 def black_list [".", "..", ".DS_Store", "Thumbs.db"] end |
#check_file(checksum) ⇒ Object
33 34 35 36 |
# File 'lib/gitpic/uploader.rb', line 33 def check_file(checksum) response = RestClient.get "#{Gitpic::HOST}/api/checksums/#{checksum}?auth_token=#{token}" response.code == 204 end |
#photo_extension?(file_name) ⇒ Boolean
65 66 67 68 |
# File 'lib/gitpic/uploader.rb', line 65 def photo_extension?(file_name) photo_extensions = %w(jpg jpeg png gif) photo_extensions.include? file_name.split('.').last.downcase end |
#post_file(path) ⇒ 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 |
# File 'lib/gitpic/uploader.rb', line 38 def post_file(path) tries = 0 begin file = File.new(path, 'rb') RestClient.post( "#{Gitpic::HOST}/api/file_blobs?auth_token=#{token}", :file_blob => { :data => file, :path => path, :filesystem_created_at => file.ctime, :filesystem_modified_at => file.mtime }) rescue RestClient::PreconditionFailed => e puts "Failed to upload #{path}; #{e.response}" rescue RestClient::RequestTimeout, RestClient::ServiceUnavailable => e if tries < 10 tries += 1 puts "Server temporarily unavailable, trying again (#{tries} time)" sleep 30 retry else puts "Server is fully non-responsive, try re-running the uploader later" raise "Stop flogging the server, it's already dead!!!" end rescue Errno::EPIPE puts "Failed to upload #{path}; [\"File is too large\"]" end end |
#upload!(this_path = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/gitpic/uploader.rb', line 11 def upload!(this_path=nil) this_path ||= @base_path puts "Scanning #{this_path}" this_dir = Dir.new(this_path) this_dir.each do |file_name| next if black_list.include?(file_name) path = File.(file_name, this_path) if File.directory?(path) upload!(path) else next unless photo_extension? file_name checksum = Digest::SHA1.file(path).hexdigest if check_file(checksum) puts "Uploading #{path}" post_file(path) else puts "Already Uploaded #{path}" end end end end |