开发者

Drawing from an S3 through my photo model gets tangled

开发者 https://www.devze.com 2023-01-07 08:44 出处:网络
I just migrated my server\'s photos to an S3. Everything seems to work swimmingly except one little catch.

I just migrated my server's photos to an S3. Everything seems to work swimmingly except one little catch.

If a user chooses a photo from his existing media, the server returns a 500 error. I assume this is because it might still be either looking for it on my local, OR it doesn't draw from the photo model appropriately :

a. From my local :

Errno::ENOENT in PhotosController#edit

No such file or directory - photos/213/original/1.jpg

RAILS_ROOT: /Users/triphizzle/Sites/hq_channel

b. From my staging :

Just a 500 error

The source of the error this is a portion of the photos controller :

def edit
  @gallery = @organization.media.find_by_media_id(params[:gallery_id]).media
  @photo = @gallery.photos.find(params[:id])
  crop if params[:crop]
end

How it works:

This opens the gallery.

Controller:

def set_search_image
end

View:

= link_to 'from media', organization_media_galleries_path(@organization, :type => 'search_image'), {:class => 'button get_media'}

They choose a gallery with this javascript :

:plain
   $(".gallery_link").click(function(){
  $("#media_header").slideUp("fast").html('');
  $.ajax({type: "GET", url: $(this).attr("href") + '?hide_nav=true&type=#{params[:type]}', dataType: "script"});
  return false;
});

So far this all works!..all the thumbs galleries have active links to the S3..

Then I click on a photo.. :

Javascript:

- if params[:hide_nav]
  :plain
      $(".edit_photo_link").click(function(){
       $("#media_header").slideUp("fast").html('');
       $.ajax({type: "GET", url: $(this).attr("href") + '?crop=true&type=#{params[:type]}', dataType: "script"});
       return false;
    });

Haml:

= link_to image_tag(photo.photo.url(:cropped_thumb)), edit_organization_media_gallery_photo_path(@organization, @gallery, photo)+"?crop=true&type=photo", {:class => 'edit_photo_link'}

Blam! It breaks! I looked at the path that it's look for and it checks out on the S3. It's the right path, the models are set up appropriately because I can upload/view/edit images I set up there.

One more additional note, is that it trades two models.

Here's a snapshot of the two..

Photo Model :

 has_attached_file :photo, 
  :styles => { :cropped_thumb => {:geometry => "115x70#", :jcrop => true}, :resized_thumb => {:geometry => "115x70>"}, :deal => {:geometry => "64x56#"},  
  :cropped_large => {:geometry => "#{PHOTO_IMAGE_WIDTH}x#{PHOTO_IMAGE_HEIGHT}#", :jcrop => true},
  :resized_large => {:geometry => "#{PHOTO_IMAGE_WIDTH}x#{PHOTO_IMAGE_HEIGHT}>"},
  :orig => '300x168>',
  :cropped_orig => {:geometry => '300x168#', :jcrop => true},
  :resized_orig => {:geometry => '300x168>'} },
  :processors => [:jcropper],
  :storage => :s3,
  :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
  :path => "photos/:id/:style/:basename.:extension",
  :bucket => 'hq-photo/root/system' # TODO: this needs to be a constant based on env

And this is the search image S3 info in the Organization model:

 has_attached_file :search_image, 
                        :styles => { :orig => "300x400>", 
                        :cropped => {:geometry => "#{SEARCH_IMAGE_WIDTH}x#{SEARCH_IMAGE_HEIGHT}#", :format => :png, :jcrop => true}, 
                        :deal => ["64x56#", :png], 
                        :thumb => {:geometry => "80x58#", :format => :png, :jcrop => true} },
                        :convert_options => {:cropped => Proc.new{self.convert_options}, 
                        :deal => Proc.new{self.convert_options_thumb}, 
                        :thumb => Proc.new{self.convert_options_thumb}},
                        :processors => [:jcropper],
                        :storage => :s3,
                        :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                        :path => "se开发者_JAVA百科arch_images/:id/:style/:basename.:extension",
                        :bucket => 'hq-photo/root/system' # TODO: this needs to be a constant based on env


After looking at the controller code the author shared with me, I determined the code was trying to pull the image from the local file system. This, of course, won't work when the image is stored on S3. The resolution is to change the code to use URIs to point to the remote file and not relative paths pointing to the filesystem.

0

精彩评论

暂无评论...
验证码 换一张
取 消