Class: ShrinkUploadedImage
- Inherits:
-
Object
- Object
- ShrinkUploadedImage
- Defined in:
- lib/shrink_uploaded_image.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#upload ⇒ Object
readonly
Returns the value of attribute upload.
Instance Method Summary collapse
-
#initialize(upload:, path:, max_pixels:, verbose: false, interactive: false) ⇒ ShrinkUploadedImage
constructor
A new instance of ShrinkUploadedImage.
- #perform ⇒ Object
Constructor Details
#initialize(upload:, path:, max_pixels:, verbose: false, interactive: false) ⇒ ShrinkUploadedImage
Returns a new instance of ShrinkUploadedImage.
6 7 8 9 10 11 12 |
# File 'lib/shrink_uploaded_image.rb', line 6 def initialize(upload:, path:, max_pixels:, verbose: false, interactive: false) @upload = upload @path = path @max_pixels = max_pixels @verbose = verbose @interactive = interactive end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
4 5 6 |
# File 'lib/shrink_uploaded_image.rb', line 4 def path @path end |
#upload ⇒ Object (readonly)
Returns the value of attribute upload.
4 5 6 |
# File 'lib/shrink_uploaded_image.rb', line 4 def upload @upload end |
Instance Method Details
#perform ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/shrink_uploaded_image.rb', line 14 def perform # Neither #dup or #clone provide a complete copy original_upload = Upload.find_by(id: upload.id) unless original_upload log "Upload is missing" return false end posts = Post .unscoped .joins(:upload_references) .where(upload_references: { upload_id: original_upload.id }) .uniq .sort_by(&:created_at) if posts.empty? log "Upload not used in any posts" return false end OptimizedImage.downsize(path, path, "#{@max_pixels}@", filename: upload.original_filename) sha1 = Upload.generate_digest(path) if sha1 == upload.sha1 log "No sha1 change" return false end begin w, h = FastImage.size(path, timeout: 15, raise_on_failure: true) rescue FastImage::SizeNotFound return false end if !w || !h log "Invalid image dimensions after resizing" return false end ww, hh = ImageSizer.resize(w, h) # A different upload record that matches the sha1 of the downsized image existing_upload = Upload.find_by(sha1: sha1) @upload = existing_upload if existing_upload upload.attributes = { sha1: sha1, width: w, height: h, thumbnail_width: ww, thumbnail_height: hh, filesize: File.size(path), } if upload.filesize >= upload.filesize_was log "No filesize reduction" return false end unless existing_upload url = Discourse.store.store_upload(File.new(path), upload) unless url log "Couldn't store the upload" return false end upload.url = url end log "base62: #{original_upload.base62_sha1} -> #{Upload.base62_sha1(sha1)}" log "sha: #{original_upload.sha1} -> #{sha1}" log "(an existing upload)" if existing_upload success = true posts.each do |post| transform_post(post, original_upload, upload) if post.raw_changed? log "Updating post" elsif post.post_hotlinked_media.exists?(upload_id: original_upload.id) log "A hotlinked, unreferenced image" elsif post.raw.include?(upload.short_url) log "Already processed" elsif post.trashed? log "A deleted post" elsif !post.topic || post.topic.trashed? log "A deleted topic" elsif post.cooked.include?(original_upload.sha1) if post.raw.include?("#{Discourse.base_url.sub(%r{\Ahttps?://}i, "")}/t/") log "Updating a topic onebox" else log "Updating an external onebox" end else log "Could not find the upload URL" success = false end log "#{Discourse.base_url}/p/#{post.id}" end unless success if @interactive print "Press any key to continue with the upload" STDIN.beep STDIN.getch puts " k" else if !existing_upload && !Upload.where(url: upload.url).exists? # We're bailing, so clean up the just uploaded file Discourse.store.remove_upload(upload) end log "⏩ Skipping" return false end end unless upload.save if !existing_upload && !Upload.where(url: upload.url).exists? # We're bailing, so clean up the just uploaded file Discourse.store.remove_upload(upload) end log "⏩ Skipping an invalid upload" return false end if existing_upload begin UploadReference .where(target_type: "Post") .where(upload_id: original_upload.id) .update_all(upload_id: upload.id) rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation end else upload.optimized_images.each(&:destroy!) end posts.each do |post| DistributedMutex.synchronize("process_post_#{post.id}") do current_post = Post.unscoped.find(post.id) # If the post became outdated, reapply changes if current_post.updated_at != post.updated_at transform_post(current_post, original_upload, upload) post = current_post end post.update_columns(raw: post.raw, updated_at: Time.zone.now) if post.raw_changed? if existing_upload post .post_hotlinked_media .where(upload_id: original_upload.id) .update_all(upload_id: upload.id) end post.rebake! end end if existing_upload original_upload.reload.destroy! else Discourse.store.remove_upload(original_upload) end true end |