Skip to content

Commit

Permalink
Merge pull request #6 from wout/remove-json-mapping
Browse files Browse the repository at this point in the history
Remove JSON.mapping in favor of JSON::Serializable.
  • Loading branch information
igor-alexandrov authored May 14, 2020
2 parents 8f169f1 + 2233823 commit 685ab36
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: |
authors:
- Igor Alexandrov <[email protected]>

crystal: 0.34
crystal: 0.34.0

license: MIT

Expand Down
18 changes: 9 additions & 9 deletions spec/lib/storage/s3_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Spectator.describe Shrine::Storage::S3 do
describe "#upload" do
context "without `prefix`" do
it "creates subdirectories" do
WebMock.stub(:put, "http://s3-us-east-2.amazonaws.com/test/object?")
WebMock.stub(:put, "https://s3-us-east-2.amazonaws.com/test/object?")
.with(body: "", headers: {"Content-Type" => "binary/octet-stream"})
.to_return(status: 200, body: "", headers: {"ETag" => "etag"})

Expand All @@ -61,7 +61,7 @@ Spectator.describe Shrine::Storage::S3 do
let(prefix) { "prefix" }

it "creates subdirectories" do
WebMock.stub(:put, "http://s3-us-east-2.amazonaws.com/test/#{prefix}/a/a/a.jpg?")
WebMock.stub(:put, "https://s3-us-east-2.amazonaws.com/test/#{prefix}/a/a/a.jpg?")
.with(body: "", headers: {"Content-Type" => "binary/octet-stream"})
.to_return(status: 200, body: "", headers: {"ETag" => "etag"})

Expand All @@ -73,7 +73,7 @@ Spectator.describe Shrine::Storage::S3 do

context "with metadata" do
it "file uploads" do
WebMock.stub(:put, "http://s3-us-east-2.amazonaws.com/test/a/a/a.jpg?")
WebMock.stub(:put, "https://s3-us-east-2.amazonaws.com/test/a/a/a.jpg?")
.with(body: "", headers: {"Content-Type" => "binary/octet-stream", "Content-Disposition" => "inline; filename=\"ex\"; filename*=UTF-8''ex"})
.to_return(status: 200, body: "", headers: {"ETag" => "etag"})
response = subject.upload(FakeIO.new, "a/a/a.jpg", metadata)
Expand All @@ -87,15 +87,15 @@ Spectator.describe Shrine::Storage::S3 do

describe "#exists?" do
it "file exists" do
WebMock.stub(:head, "http://s3-us-east-2.amazonaws.com/test/a/a/a.jpg?")
WebMock.stub(:head, "https://s3-us-east-2.amazonaws.com/test/a/a/a.jpg?")
.to_return(status: 200, headers: {"Content-Type" => "binary/octet-stream", "Last-Modified" => "Sun, 10 Jan 2020 4:47:46 UTC"})
expect(
subject.exists?("a/a/a.jpg")
).to be_true
end

it "file does not exist" do
WebMock.stub(:head, "http://s3-us-east-2.amazonaws.com/test/ex.jpg?")
WebMock.stub(:head, "https://s3-us-east-2.amazonaws.com/test/ex.jpg?")
.to_return(status: 404)
expect(
subject.exists?("ex.jpg")
Expand Down Expand Up @@ -125,7 +125,7 @@ Spectator.describe Shrine::Storage::S3 do
describe "#open" do
context "without `prefix`" do
it "returns a IO-like object" do
WebMock.stub(:get, "http://s3-us-east-2.amazonaws.com/test/foo.jpg?")
WebMock.stub(:get, "https://s3-us-east-2.amazonaws.com/test/foo.jpg?")
.to_return(body_io: FakeIO.new)
expect(
subject.open("foo.jpg")
Expand All @@ -135,7 +135,7 @@ Spectator.describe Shrine::Storage::S3 do
context "with `prefix`" do
let(prefix) { "prefix" }
it "returns a IO-like object" do
WebMock.stub(:get, "http://s3-us-east-2.amazonaws.com/test/#{prefix}/foo.jpg?")
WebMock.stub(:get, "https://s3-us-east-2.amazonaws.com/test/#{prefix}/foo.jpg?")
.to_return(body_io: FakeIO.new)
expect(
subject.open("foo.jpg")
Expand All @@ -147,7 +147,7 @@ Spectator.describe Shrine::Storage::S3 do
describe "#delete" do
context "without `prefix`" do
it "deletes objects" do
WebMock.stub(:delete, "http://s3-us-east-2.amazonaws.com/test/foo.jpg?")
WebMock.stub(:delete, "https://s3-us-east-2.amazonaws.com/test/foo.jpg?")
.to_return(status: 204)
expect(
subject.delete("foo.jpg")
Expand All @@ -158,7 +158,7 @@ Spectator.describe Shrine::Storage::S3 do
context "with `prefix`" do
let(prefix) { "prefix" }
it "deletes objects" do
WebMock.stub(:delete, "http://s3-us-east-2.amazonaws.com/test/#{prefix}/foo.jpg?")
WebMock.stub(:delete, "https://s3-us-east-2.amazonaws.com/test/#{prefix}/foo.jpg?")
.to_return(status: 204)
expect(
subject.delete("foo.jpg")
Expand Down
17 changes: 14 additions & 3 deletions src/shrine/storage/s3.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@ class Shrine
# :public
# : Sets public access to all uploading files.
#
def initialize(@bucket : String, @client : Awscr::S3::Client?, @prefix : String? = nil,
@upload_options : Hash(String, String) = Hash(String, String).new, @public : Bool = false)
def initialize(
@bucket : String,
@client : Awscr::S3::Client?,
@prefix : String? = nil,
@upload_options : Hash(String, String) = Hash(String, String).new,
@public : Bool = false
)
end

# Copies the file into the given location.
def upload(io : IO, id : String, metadata : Shrine::UploadedFile::MetadataType? = nil, move = false, **upload_options)
def upload(
io : IO,
id : String,
metadata : Shrine::UploadedFile::MetadataType? = nil,
move = false,
**upload_options
)
options = Hash(String, String).new
options["Content-Disposition"] = ContentDisposition.inline(metadata["filename"].to_s) if metadata && metadata["filename"]
options["x-amz-acl"] = "public-read" if public?
Expand Down
21 changes: 5 additions & 16 deletions src/shrine/uploaded_file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@ require "json"

class Shrine
class UploadedFile
alias MetadataType = Hash(String, String | Int32 | UInt32 | Int64 | UInt64 | Nil)
include JSON::Serializable

# include JSON::Serializable
alias MetadataType = Hash(String, String | Int32 | UInt32 | Int64 | UInt64 | Nil)

@io : IO?

JSON.mapping(
id: String,
storage_key: String,
metadata: MetadataType
)

# @[JSON::Field(key: "id")]
# property id : String

# @[JSON::Field(key: "storage_key")]
# property storage_key : String

# @[JSON::Field(key: "metadata")]
# property metadata : MetadataType
property id : String
property storage_key : String
property metadata : MetadataType

def initialize(id : String, storage : String, metadata : MetadataType = MetadataType.new)
@storage_key = storage
Expand Down

0 comments on commit 685ab36

Please sign in to comment.