]> cat aescling's git repositories - mastodon.git/commitdiff
Improvements to image upload validation and creation (#10431)
authorslice <ryaneft@gmail.com>
Mon, 1 Apr 2019 05:30:46 +0000 (22:30 -0700)
committerEugen Rochko <eugen@zeonfederated.com>
Mon, 1 Apr 2019 05:30:46 +0000 (07:30 +0200)
* Check if image value is nil? before creating an image

Check if uploaded images aren't nil before creating SiteUpload models
for them.

* Validate presence of file in SiteUpload

* Fix file presence validation

* Fabricate SiteUpload#file

* Add link to Creative Commons license

app/models/form/admin_settings.rb
app/models/site_upload.rb
spec/fabricators/assets/TEAPOT [new file with mode: 0644]
spec/fabricators/assets/utah_teapot.png [new file with mode: 0644]
spec/fabricators/site_upload_fabricator.rb

index 85a6e6dffc60f898fbeff9b3e419167c837f33c9..d3af8c30f5283d224f46399ccc04bf0889bedf2c 100644 (file)
@@ -67,7 +67,7 @@ class Form::AdminSettings
     KEYS.each do |key|
       value = instance_variable_get("@#{key}")
 
-      if UPLOAD_KEYS.include?(key)
+      if UPLOAD_KEYS.include?(key) && !value.nil?
         upload = SiteUpload.where(var: key).first_or_initialize(var: key)
         upload.update(file: value)
       else
index 14d68376727619d8e448c53b851ce392f922283a..cf10b30fc98ee3d23d0d3433b38834584c702eb8 100644 (file)
@@ -18,6 +18,7 @@ class SiteUpload < ApplicationRecord
   has_attached_file :file
 
   validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/
+  validates :file, presence: true
   validates :var, presence: true, uniqueness: true
 
   before_save :set_meta
diff --git a/spec/fabricators/assets/TEAPOT b/spec/fabricators/assets/TEAPOT
new file mode 100644 (file)
index 0000000..e624ecb
--- /dev/null
@@ -0,0 +1,6 @@
+This "Utah teapot" photograph is licensed under the Creative Commons
+Attribution-Share Alike 3.0 Unported license:
+  https://creativecommons.org/licenses/by-sa/3.0/deed.en
+
+Original source of work:
+  https://commons.wikimedia.org/wiki/File:Utah_teapot_simple_2.png
diff --git a/spec/fabricators/assets/utah_teapot.png b/spec/fabricators/assets/utah_teapot.png
new file mode 100644 (file)
index 0000000..6708361
Binary files /dev/null and b/spec/fabricators/assets/utah_teapot.png differ
index 4a171486f67029d59e1503e63a3163118abfa713..2efc57e2803010f4f247d80dc41e4460794992e4 100644 (file)
@@ -1,2 +1,3 @@
 Fabricator(:site_upload) do
+  file { File.open(File.join(Rails.root, 'spec', 'fabricators', 'assets', 'utah_teapot.png')) }
 end