Class: URI::FILE
Overview
File URL. Keep in mind that file URLs take the form of file://host/path
, although the host is not used, so typically all you will see are three backslashes. This methods accept common variants, like file:/path
but always returns a valid URL.
Constant Summary collapse
- COMPONENT =
[ :host, :path ].freeze
Instance Method Summary collapse
-
#initialize(*args) ⇒ FILE
constructor
A new instance of FILE.
-
#read(options = nil, &block) ⇒ Object
See URI::Generic#read.
-
#real_path ⇒ Object
Returns the file system path based that corresponds to the URL path.
- #to_s ⇒ Object
- #upload(source, options = nil) ⇒ Object
Methods inherited from Generic
Constructor Details
#initialize(*args) ⇒ FILE
Returns a new instance of FILE.
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/buildr/core/transports.rb', line 500 def initialize(*args) super # file:something (opaque) becomes file:///something if path.nil? set_path "/#{opaque}" unless opaque.nil? set_opaque nil warn "#{caller[2]}: We'll accept this URL, but just so you know, it needs three slashes, as in: #{to_s}" end end # Sadly, file://something really means file://something/ (something being server) set_path '/' if path.empty? # On windows, file://c:/something is not a valid URL, but people do it anyway, so if we see a drive-as-host, # we'll just be nice enough to fix it. (URI actually strips the colon here) if host =~ /^[a-zA-Z]$/ set_path "/#{host}:#{path}" set_host nil end end |
Instance Method Details
#read(options = nil, &block) ⇒ Object
See URI::Generic#read
522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
# File 'lib/buildr/core/transports.rb', line 522 def read( = nil, &block) ||= {} raise ArgumentError, 'Either you\'re attempting to read a file from another host (which we don\'t support), or you used two slashes by mistake, where you should have file:///<path>.' if host path = real_path # TODO: complain about clunky URLs raise NotFoundError, "Looking for #{self} and can't find it." unless File.exists?(path) raise NotFoundError, "Looking for the file #{self}, and it happens to be a directory." if File.directory?(path) File.open path, 'rb' do |input| [:progress], path.split('/').last, input.stat.size do |progress| block ? block.call(input.read) : input.read end end end |
#real_path ⇒ Object
Returns the file system path based that corresponds to the URL path. On windows this method strips the leading slash off of the path. On all platforms this method unescapes the URL path.
544 545 546 547 |
# File 'lib/buildr/core/transports.rb', line 544 def real_path #:nodoc: real_path = Buildr::Util.win_os? && path =~ /^\/[a-zA-Z]:\// ? path[1..-1] : path URI.unescape(real_path) end |
#to_s ⇒ Object
537 538 539 |
# File 'lib/buildr/core/transports.rb', line 537 def to_s "file://#{host}#{path}" end |
#upload(source, options = nil) ⇒ Object
493 494 495 496 497 498 |
# File 'lib/buildr/core/transports.rb', line 493 def upload(source, = nil) super if File === source then File.chmod(source.stat.mode, real_path) end end |