lib/star/file.rb in star-1.1.0 vs lib/star/file.rb in star-1.2.0
- old
+ new
@@ -36,10 +36,18 @@
def delete
Star.remote? ? delete_remote : delete_local
end
+ def copy_from(source)
+ Star.remote? ? copy_from_remote(source) : copy_from_local(source)
+ end
+
+ def remote_path
+ URI.escape path
+ end
+
private
def store_remote(tmp_file)
timestamp = Time.now.utc.strftime "%a, %d %b %Y %H:%M:%S UTC"
signature = sign "PUT\n\n#{@content_type}\n#{timestamp}"
@@ -73,14 +81,40 @@
request.add_field 'Authorization', "AWS #{key}:#{signature}"
request.add_field 'Date', timestamp
end
end
+ def copy_from_remote(source)
+ timestamp = Time.now.utc.strftime "%a, %d %b %Y %H:%M:%S UTC"
+ extra = "x-amz-copy-source:/#{bucket}#{source.remote_path}"
+ signature = sign "PUT\n\n#{@content_type}\n#{timestamp}\n#{extra}"
+ request = copy_file signature, timestamp, source
+ response = Net::HTTP.start(host, 443, use_ssl: true) do |http|
+ http.request request
+ end
+ response.error! unless response.is_a? Net::HTTPSuccess
+ sleep 3 # See https://forums.aws.amazon.com/message.jspa?messageID=370480
+ end
+
+ def copy_file(signature, timestamp, source)
+ Net::HTTP::Put.new("/#{bucket}#{remote_path}").tap do |request|
+ request.add_field 'Date', timestamp
+ request.add_field 'Content-Type', @content_type
+ request.add_field 'x-amz-copy-source', "/#{bucket}#{source.remote_path}"
+ request.add_field 'Authorization', "AWS #{key}:#{signature}"
+ end
+ end
+
def delete_local
FileUtils.rm_f path
end
+ def copy_from_local(source)
+ FileUtils.mkdir_p ::File.dirname(path)
+ FileUtils.cp source.path, path
+ end
+
def url_params
expires_at = Time.now.to_i + Star.configuration.duration
digest = OpenSSL::Digest.new 'sha1'
params = "response-content-disposition=attachment"
string = "GET\n\n\n#{expires_at}\n/#{bucket}#{remote_path}?#{params}"
@@ -113,13 +147,9 @@
URI.escape(string.strip).gsub(/./) {|c| s.fetch(c, c)}
end
def host
"s3.amazonaws.com"
- end
-
- def remote_path
- URI.escape path
end
def key
Star.configuration.access_key_id
end