Module: Mokio::Concerns::Controllers::Photos
- Extended by:
- ActiveSupport::Concern
- Included in:
- PhotosController
- Defined in:
- lib/mokio/concerns/controllers/photos.rb
Overview
Concern for PhotosController
-
before filters:
:edited_photo, only: [:get_thumb, :update_thumb, :remove_thumb, :crop_thumb, :rotate_thumb, :get_photo, :rotate_photo, :crop_photo]
Instance Method Summary collapse
-
#create ⇒ Object
Standard create action.
-
#crop_photo ⇒ Object
Cropping photo [POST] (ajax).
-
#crop_thumb ⇒ Object
Cropping thumb [POST] (ajax).
-
#destroy ⇒ Object
Standard destroy action.
-
#get_photo ⇒ Object
Load photo to html [GET] (ajax).
-
#get_thumb ⇒ Object
Load thumb to html [GET] (ajax).
- #photo_model ⇒ Object
-
#remove_thumb ⇒ Object
Removing thumb [DELETE] (ajax).
-
#rotate_photo ⇒ Object
Rotating photo [GET] (ajax).
-
#rotate_thumb ⇒ Object
Rotating thumb [GET] (ajax).
-
#update ⇒ Object
Standard update action.
-
#update_thumb ⇒ Object
Updating thumb params [PATCH] (ajax).
-
#upload_external_links ⇒ Object
Uploads photos from external links (ajax).
Instance Method Details
#create ⇒ Object
Standard create action
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mokio/concerns/controllers/photos.rb', line 25 def create @photo = photo_model.new(photo_params) respond_to do |format| if @photo.save flash[:notice] = t("photos.created", title: @photo.name) format.js { render :template => "mokio/photos/create"} else flash[:error] = t("photos.not_created", title: @photo.name) format.html { render nothing: true } end end end |
#crop_photo ⇒ Object
Cropping photo [POST] (ajax)
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/mokio/concerns/controllers/photos.rb', line 254 def crop_photo begin image_path = "#{Rails.root}/public#{@edited_photo.data_file.url}" img = MiniMagick::Image.open(edited_photo_path(:normal)) width = Mokio.photo_thumb_width height = Mokio.photo_thumb_height img.crop("#{params[:w]}x#{params[:h]}+#{params[:x]}+#{params[:y]}") img.resize "#{width}x#{height}" img.write edited_photo_path(:thumb) flash[:notice] = t("photos.crop", name: @edited_photo.name) @edited_photo.touch # # when MiniMagick.open failed, continue action with only error message for user, this prevent redirect to 500 in this case # rescue Errno::ENOENT flash[:error] = t("photos.not_crop", name: @edited_photo.name) logger.error exception_msg(image_path) end respond_to do |format| format.js {render :template => "mokio/photos/crop_photo"} end end |
#crop_thumb ⇒ Object
Cropping thumb [POST] (ajax)
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 |
# File 'lib/mokio/concerns/controllers/photos.rb', line 150 def crop_thumb begin image_path = thumb_path img = MiniMagick::Image.open(image_path) img.crop("#{params[:w]}x#{params[:h]}+#{params[:x]}+#{params[:y]}") img.write image_path if @edited_photo.thumb.recreate_versions! flash[:notice] = t("photos.crop", name: @edited_photo.name) @edited_photo.touch else flash[:error] = t("photos.not_crop", name: @edited_photo.name) end # # when MiniMagick.open failed, continue action with only error message for user, this prevent redirect to 500 in this case # rescue Errno::ENOENT flash[:error] = t("photos.not_crop", name: @edited_photo.name) logger.error exception_msg(image_path) end respond_to do |format| format.js {render :template => "mokio/photos/crop_thumb"} end end |
#destroy ⇒ Object
Standard destroy action
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/mokio/concerns/controllers/photos.rb', line 104 def destroy @id = params[:id] @photo = photo_model.friendly.find(@id) if @photo.destroy flash[:notice] = t("photos.deleted", title: @photo.name) else flash[:error] = t("photos.not_deleted", title: @photo.name) end respond_to do |format| format.js {render :template => "mokio/photos/destroy"} end end |
#get_photo ⇒ Object
Load photo to html [GET] (ajax)
243 244 245 246 247 248 249 |
# File 'lib/mokio/concerns/controllers/photos.rb', line 243 def get_photo if stale?(:etag => @edited_photo, :last_modified => @edited_photo.updated_at, :public => true) respond_to do |format| format.js {render :template => "mokio/photos/get_photo"} end end end |
#get_thumb ⇒ Object
Load thumb to html [GET] (ajax)
139 140 141 142 143 144 145 |
# File 'lib/mokio/concerns/controllers/photos.rb', line 139 def get_thumb if stale?(:etag => @edited_photo, :last_modified => @edited_photo.updated_at, :public => true) respond_to do |format| format.js {render :template => "mokio/photos/get_thumb"} end end end |
#photo_model ⇒ Object
18 19 20 |
# File 'lib/mokio/concerns/controllers/photos.rb', line 18 def photo_model Mokio::Photo end |
#remove_thumb ⇒ Object
Removing thumb [DELETE] (ajax)
225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/mokio/concerns/controllers/photos.rb', line 225 def remove_thumb @edited_photo.remove_thumb = true if @edited_photo.save! flash[:notice] = t("photos.thumb_deleted", name: @edited_photo.name) @edited_photo.touch else flash[:error] = t("photos.thumb_not_deleted", name: @edited_photo.name) end respond_to do |format| format.js {render :template => "mokio/photos/remove_thumb"} end end |
#rotate_photo ⇒ Object
Rotating photo [GET] (ajax)
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/mokio/concerns/controllers/photos.rb', line 284 def rotate_photo begin image_path = "#{Rails.root}/public#{@edited_photo.data_file.url}" img = MiniMagick::Image.open(image_path) img.rotate "90" img.write image_path if @edited_photo.data_file.recreate_versions! flash[:notice] = t("photos.rotated", name: @edited_photo.name) @edited_photo.touch else flash[:error] = t("photos.not_rotated", name: @edited_photo.name) end # # when MiniMagick.open failed, continue action with only error message for user, this prevent redirect to 500 in this case # rescue Errno::ENOENT flash[:error] = t("photos.not_rotated", name: @edited_photo.name) logger.error exception_msg(image_path) end respond_to do |format| format.js {render :template => "mokio/photos/rotate_photo"} end end |
#rotate_thumb ⇒ Object
Rotating thumb [GET] (ajax)
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/mokio/concerns/controllers/photos.rb', line 181 def rotate_thumb begin image_path = thumb_path img = MiniMagick::Image.open(image_path) img.rotate "90" img.write image_path if @edited_photo.thumb.recreate_versions! flash[:notice] = t("photos.rotated_thumb", name: @edited_photo.name) @edited_photo.touch end # # when MiniMagick.open failed, continue action with only error message for user, this prevent redirect to 500 in this case # rescue Errno::ENOENT flash[:error] = t("photos.not_rotated_thumb", name: @edited_photo.name) logger.error exception_msg(image_path) end respond_to do |format| format.js {render :template => "mokio/photos/rotate_thumb"} end end |
#update ⇒ Object
Standard update action
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/mokio/concerns/controllers/photos.rb', line 122 def update @photo = photo_model.friendly.find(params[:id]) if @photo.update(photo_params) flash[:notice] = t("photos.updated", title: @photo.name) else flash[:error] = t("photos.not_updated", title: @photo.name) end respond_to do |format| format.html { render nothing: true } end end |
#update_thumb ⇒ Object
Updating thumb params [PATCH] (ajax)
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/mokio/concerns/controllers/photos.rb', line 209 def update_thumb if @edited_photo.update(photo_params) flash[:notice] = t("photos.thumb_updated") @edited_photo.touch else flash[:error] = t("photos.thumb_not_updated") end respond_to do |format| format.js {render :template => "mokio/photos/update_thumb"} end end |
#upload_external_links ⇒ Object
Uploads photos from external links (ajax). Using ‘faraday’ gem
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 |
# File 'lib/mokio/concerns/controllers/photos.rb', line 43 def upload_external_links urls = params[:photo][:remote_data_file_url].split("\n") error = t("photos.photos_from_external_links_error") notice = "" @photos = [] # # split with "\n" can create "empty" string with '\r', when parsing textarea parameter # we don't want to have them in loop # urls.reject! {|u| u == "\r"} if urls urls.each do |url| url = url.gsub(/,/, '') begin uri = URI.parse(url) raise URI::InvalidURIError unless uri.scheme == "http" || uri.scheme == "https" # # Carrierwave use Kernel.open to find file in url which is very slow when url is wrong # Check first if url status will be 200 and then try upload this file # if Faraday.head(url).status == 200 photo = create_external_photo url if photo.save # !!! @photos << photo notice += "#{t("photos.created", title: photo.name)}\n" else raise "[Photo Save] Photo not saved" end else raise "[Faraday Gem Status] Faraday head status != 200" end # # TODO: lets do it smarter way # rescue Exception => e error += "\n#{url}" logger.error e. # becaouse we catach all exceptions we have to at least log them end end end # # we can have both error and notice messages so only those cases # flash[:error] = error unless error == t("photos.photos_from_external_links_error") flash[:notice] = notice unless notice.blank? respond_to do |format| format.js end end |