diff --git a/src/awscr-s3/client.cr b/src/awscr-s3/client.cr index f2bdd69..b790bd8 100644 --- a/src/awscr-s3/client.cr +++ b/src/awscr-s3/client.cr @@ -268,6 +268,8 @@ module Awscr::S3 # p resp.size # => 123 # p resp.status # => HTTP::Status::OK # p resp.last_modified # => "Wed, 19 Jun 2019 11:55:33 GMT" + # p resp.etag # => "" + # p resp.meta # => {"my_tag" => "my_value"} # ``` def head_object(bucket, object : String, headers : Hash(String, String) = Hash(String, String).new) resp = http.head("/#{bucket}/#{Util.encode(object)}", headers: headers) diff --git a/src/awscr-s3/head_object_output.cr b/src/awscr-s3/head_object_output.cr deleted file mode 100644 index 99b02c4..0000000 --- a/src/awscr-s3/head_object_output.cr +++ /dev/null @@ -1,33 +0,0 @@ -module Awscr::S3::Response - class HeadObjectOutput - DATE_FORMAT = "%a, %d %b %Y %H:%M:%S %Z" - - # The body of the request object - getter status : HTTP::Status - getter status_message : String | Nil - getter headers : HTTP::Headers - getter content_type : String - getter last_modified : Time - getter size : UInt64 - - # Create a `GetObjectOutput` response from an - # `HTTP::Client::Response` object - def self.from_response(response) - new( - status: response.status, - status_message: response.status_message, - headers: response.headers, - content_type: response.headers["Content-Type"], - last_modified: self.parse_date(response.headers["Last-Modified"]), - size: response.headers["Content-Length"].to_u64, - ) - end - - def initialize(@status, @status_message, @headers, @content_type, @last_modified, @size) - end - - private def self.parse_date(date : String) - Time.parse!(date.gsub(/\s{2,}/, ' '), DATE_FORMAT) - end - end -end diff --git a/src/awscr-s3/responses/head_object_output.cr b/src/awscr-s3/responses/head_object_output.cr new file mode 100644 index 0000000..204b55e --- /dev/null +++ b/src/awscr-s3/responses/head_object_output.cr @@ -0,0 +1,54 @@ +module Awscr::S3::Response + class HeadObjectOutput + DATE_FORMAT = "%a, %d %b %Y %H:%M:%S %Z" + + # The body of the request object + getter status : HTTP::Status + getter status_message : String | Nil + getter headers : HTTP::Headers + + # Create a `GetObjectOutput` response from an + # `HTTP::Client::Response` object + def self.from_response(response : HTTP::Client::Response) + new( + status: response.status, + status_message: response.status_message, + headers: response.headers + ) + end + + def initialize(@status, @status_message, @headers) + end + + {% for f in ["Cache-Control", "Content-Disposition", "Content-Encoding", "Content-Language", "Content-Type",] %} + def {{ f.id.stringify.underscore.gsub(/-/, "_").id }} : String? + headers["{{ f.id }}"]? + end + {% end %} + + def last_modified : Time + parse_date(headers["Last-Modified"]) + end + + def size : UInt64 + headers["Content-Length"].to_u64 + end + + def etag : String? + headers["ETag"].try{|v| v.strip('"')} + end + + def meta : Hash(String, String) + meta = {} of String => String + response.headers.each do |k, v| + next unless k.starts_with?("x-amz-meta-") + meta[k.lchop("x-amz-meta-")] = v.first + end + meta + end + + private def parse_date(date : String) + Time.parse!(date.gsub(/\s{2,}/, ' '), DATE_FORMAT) + end + end +end