Check if web file exists, without downloading it?
Is it somehow possible to check if a file exists, without actually downloading it?
I have this large (~ 40mb) file, for exampl开发者_运维百科e:
http://mirrors.sohu.com/mysql/MySQL-6.0/MySQL-6.0.11-0.glibc23.src.rpm
This is not strictly related to ruby, but it would be nice if the sender could set the content length.
RestClient.get "http://mirrors.sohu.com/mysql/MySQL-6.0/MySQL-6.0.11-0.glibc23.src.rpm",
headers: {"Content-Length" => 100}
Try RestClient.head
. (See https://www.rfc-editor.org/rfc/rfc9110.html#name-head)
The HTTP HEAD method is identical to GET except that the server MUST NOT send content in the response. HEAD is used to obtain metadata about the selected representation without transferring its representation data, often for the sake of testing hypertext links or finding recent modifications.
RFC 9110 HTTP Semantics
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
See section 9.4 regarding the HEAD request.
We can use wget:
def file_exists?(full_file_path)
resp = `wget --spider -v #{full_file_path} && echo 1 || echo 0`
resp.to_i.zero? ? false : true
end
精彩评论