kithe 2.0.0.pre.alpha2 → 2.0.0.pre.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/app/jobs/kithe/create_derivatives_job.rb +2 -2
  3. data/app/models/kithe/asset.rb +82 -154
  4. data/app/models/kithe/asset/derivative_creator.rb +32 -62
  5. data/app/models/kithe/asset/derivative_definition.rb +12 -13
  6. data/app/models/kithe/asset/set_shrine_uploader.rb +64 -0
  7. data/app/models/kithe/collection.rb +0 -6
  8. data/app/models/kithe/model.rb +0 -21
  9. data/app/models/kithe/work.rb +0 -5
  10. data/app/uploaders/kithe/asset_uploader.rb +15 -78
  11. data/lib/kithe/version.rb +4 -1
  12. data/lib/shrine/plugins/kithe_checksum_signatures.rb +41 -0
  13. data/lib/shrine/plugins/kithe_controllable_backgrounding.rb +53 -0
  14. data/lib/shrine/plugins/kithe_derivative_definitions.rb +101 -0
  15. data/lib/shrine/plugins/kithe_derivatives.rb +54 -0
  16. data/lib/shrine/plugins/kithe_determine_mime_type.rb +39 -0
  17. data/lib/shrine/plugins/kithe_persisted_derivatives.rb +161 -0
  18. data/lib/shrine/plugins/kithe_promotion_callbacks.rb +4 -0
  19. data/lib/shrine/plugins/kithe_promotion_directives.rb +33 -3
  20. data/lib/shrine/plugins/kithe_storage_location.rb +53 -4
  21. data/lib/tasks/kithe_tasks.rake +22 -15
  22. data/spec/dummy/log/development.log +867 -0
  23. data/spec/dummy/log/test.log +26005 -0
  24. data/spec/models/kithe/asset/asset_derivatives_spec.rb +137 -0
  25. data/spec/models/kithe/asset/asset_promotion_hooks_spec.rb +26 -5
  26. data/spec/models/kithe/asset/set_shrine_uploader_spec.rb +39 -0
  27. data/spec/models/kithe/asset_spec.rb +9 -59
  28. data/spec/models/kithe/model_spec.rb +0 -32
  29. data/spec/shrine/kithe_accept_remote_url_spec.rb +49 -0
  30. data/spec/shrine/kithe_checksum_signatures_spec.rb +63 -0
  31. data/spec/shrine/kithe_derivative_definitions_spec.rb +303 -0
  32. data/spec/shrine/kithe_persisted_derivatives_spec.rb +424 -0
  33. data/spec/shrine/kithe_storage_location_spec.rb +43 -15
  34. data/spec/spec_helper.rb +7 -6
  35. data/spec/test_support/images/3x3_pixel.jpg +0 -0
  36. data/spec/test_support/shrine_spec_support.rb +2 -1
  37. metadata +23 -23
  38. data/app/models/kithe/asset/derivative_updater.rb +0 -119
  39. data/app/models/kithe/derivative.rb +0 -15
  40. data/app/uploaders/kithe/derivative_uploader.rb +0 -48
  41. data/spec/models/kithe/asset/asset_create_derivatives_spec.rb +0 -320
  42. data/spec/models/kithe/derivative_spec.rb +0 -168
@@ -1,168 +0,0 @@
1
- require 'rails_helper'
2
-
3
- RSpec.describe Kithe::Derivative, type: :model, queue_adapter: :test do
4
- let(:key) { "some_thumb" }
5
- let(:asset) { FactoryBot.create(:kithe_asset, :with_faked_metadata, faked_metadata: { sha512: "fakesha512" }) }
6
- let(:derivative) { Kithe::Derivative.new }
7
-
8
- describe "for referential integrity" do
9
- it "needs an asset" do
10
- expect { derivative.save! }.to raise_error(ActiveRecord::RecordInvalid)
11
- end
12
- it "needs a key" do
13
- derivative.asset = asset
14
- expect { derivative.save! }.to raise_error(ActiveRecord::NotNullViolation)
15
- end
16
- it "can save with key and asset" do
17
- derivative.asset = asset
18
- derivative.key = key
19
- derivative.save!
20
- expect(derivative).to be_persisted
21
- end
22
- it "can't save duplicate key/asset" do
23
- existing_derivative = Kithe::Derivative.create!(asset: asset, key: key)
24
-
25
- derivative.asset = asset
26
- derivative.key = key
27
- expect { derivative.save! }.to raise_error(ActiveRecord::RecordNotUnique)
28
- end
29
- end
30
-
31
- describe "asset without a sha256" do
32
- let(:asset) { FactoryBot.create(:kithe_asset) }
33
- it "won't create derivative" do
34
- expect {
35
- asset.update_derivative(key, StringIO.new("something"))
36
- }.to raise_error(ArgumentError)
37
- end
38
- end
39
-
40
- describe "Asset#update_derivative", queue_adapter: :test do
41
- let(:key) { "some_derivative" }
42
- let(:dummy_content) { File.read(Kithe::Engine.root.join("spec/test_support/images/1x1_pixel.jpg"), encoding: "BINARY") }
43
- let(:dummy_io) { File.open(Kithe::Engine.root.join("spec/test_support/images/1x1_pixel.jpg"), encoding: "BINARY") }
44
- let(:asset) { FactoryBot.create(:kithe_asset, :with_faked_metadata, faked_metadata: { sha512: "fakesha512" })}
45
-
46
- it "can add a derivative" do
47
- derivative = asset.update_derivative(key, dummy_io)
48
-
49
- expect(derivative).to be_present
50
- derivative.reload
51
-
52
- # file is stored
53
- expect(derivative.key).to eq(key)
54
- expect(derivative.file).to be_present
55
- expect(derivative.file.storage_key).to eq(:kithe_derivatives)
56
- expect(derivative.file.read).to eq(dummy_content)
57
-
58
- # some metadata we got
59
- expect(derivative.size).to eq(dummy_content.length)
60
- expect(derivative.content_type).to eq("image/jpeg")
61
- expect(derivative.height).to eq(1)
62
- expect(derivative.width).to eq(1)
63
- expect(derivative.file.metadata["filename"]).to eq("#{asset.friendlier_id}_some_derivative.jpeg")
64
-
65
- # path on storage is nice and pretty
66
- expect(derivative.file.id).to match %r{\A#{asset.id}/#{key}/[a-f0-9]+\.jpeg\Z}
67
- end
68
-
69
- it "can add a derivative with custom storage location" do
70
- derivative = asset.update_derivative(key, dummy_io, storage_key: :store)
71
-
72
- expect(derivative).to be_present
73
- derivative.reload
74
- expect(derivative.file).to be_present
75
- expect(derivative.file.storage_key).to eq(:store)
76
- end
77
-
78
- it "can add a derivative with custom metadata" do
79
- derivative = asset.update_derivative(key, dummy_io, metadata: { "foo" => "bar" })
80
- expect(derivative).to be_present
81
- expect(derivative.file.metadata["size"]).to eq(dummy_content.length)
82
- expect(derivative.file.metadata["foo"]).to eq("bar")
83
- end
84
-
85
- it "deletes stored file when model is deleted" do
86
- derivative = asset.update_derivative(key, dummy_io, metadata: { foo: "bar" })
87
- stored_file = derivative.file
88
- expect(stored_file.exists?).to be(true)
89
-
90
- derivative.destroy
91
- expect(stored_file.exists?).to be(false)
92
- end
93
-
94
- describe "with an existing derivative" do
95
- let!(:existing) { asset.update_derivative(key, StringIO.new("something else")) }
96
-
97
- it "will replace an existing derivative" do
98
- expect(existing).to be_persisted
99
- original_shrine_file = existing.file
100
-
101
- replacement = asset.update_derivative(key, dummy_io)
102
-
103
- expect(original_shrine_file.exists?).to be(false)
104
- end
105
-
106
- it "can remove with #remove_derivative with derivatives assoc loaded" do
107
- expect(Kithe::Derivative.where(id: existing.id).count).to be(1)
108
- stored_file = existing.file
109
- expect(stored_file.exists?).to be(true)
110
-
111
- asset.derivatives.load
112
- asset.remove_derivative(key)
113
-
114
- expect(stored_file.exists?).to be(false)
115
- expect(Kithe::Derivative.where(id: existing.id).count).to be(0)
116
- end
117
-
118
- it "can remove with #remove_derivative without derivatives assoc loaded" do
119
- expect(Kithe::Derivative.where(id: existing.id).count).to be(1)
120
- stored_file = existing.file
121
- expect(stored_file.exists?).to be(true)
122
-
123
- asset.association(:derivatives).reset
124
- asset.remove_derivative(key)
125
-
126
- expect(stored_file.exists?).to be(false)
127
- expect(Kithe::Derivative.where(id: existing.id).count).to be(0)
128
- end
129
- end
130
-
131
- describe "with asset changed concurrently" do
132
- before do
133
- data_with_new_sha = asset.file.data.deep_dup.tap do |d|
134
- d["metadata"]["sha512"] = "new_fake_sha512"
135
- end
136
- Kithe::Asset.where(id: asset.id).update_all(file_data: data_with_new_sha)
137
- end
138
- it "does not add derivative" do
139
- expect {
140
- result = asset.update_derivative(key, StringIO.new("something else"))
141
- expect(result).to be_nil
142
- }.to_not change{ [Kithe::Derivative.count, Kithe::DerivativeUploader.storages[:kithe_derivatives].store.count]}
143
-
144
- expect(asset.derivatives.reload.count).to be(0)
145
- end
146
- end
147
-
148
- describe "existing asset" do
149
- let!(:existing_asset) do
150
- FactoryBot.create(:kithe_asset, :with_file).tap do |asset|
151
- asset.promote
152
- end
153
- end
154
- it "deletes existing derivatives on new file assignment" do
155
- deriv = existing_asset.update_derivative(key, StringIO.new("something"))
156
- deriv_uploaded_file = deriv.file
157
-
158
- existing_asset.file = File.open(Kithe::Engine.root.join("spec/test_support/images/2x2_pixel.jpg"))
159
- existing_asset.save!
160
-
161
- expect(Kithe::Derivative.where(id: deriv.id).exists?).to be(false)
162
- expect(deriv_uploaded_file.exists?).to be(false)
163
- expect(existing_asset.derivatives.reload.count).to be(0)
164
- end
165
- end
166
- end
167
- end
168
-