Saving images with Carrierwave inside a ruby worker - ruby-on-rails

I'm trying to write a method to store an image from a given url, inside a ruby worker. It comes along my Rails app in which I display the object image.
Here is what I've come up with :
def store(url)
#object = Object.find(1)
#object[:image] = CarrierWave::Uploader.store!(image_url)
end
It doesn't seem to work at all.
Any clues?
Is there another way around?
[EDIT]
Here is the current situation :
def store
#object = Object.find(1)
my_uploader = ImageUploader.new
image = open("http://twitpic.com/show/iphone/xxxx.jpg")
# or for a local file:
image = File.open(Rails.root.join('xxxx.png'))
#object[:image] = my_uploader.store!(image)
#object.save!
end
The filename in the [:image] attibute is still wrong. It gives "[:store_versions!]". How do I get the filename right?
[EDIT2]
Got the filename right by adding #artwork[:image] = my_uploader.filename before save.
But #object = Object.find(1) won't work. How do I access the Object class, which is inside my rails app, from the worker?

#object.image.store!(image) finally did the job!

You'll want to create a new uploader object and point it to your file
image = File.open(Rails.root.join('path', 'to', 'file.png'))
#object[:image] = YourUploader.new(image)

Related

How to get app path in grails?

I want to get application path in my application, but I haven't found any way. If anyone knows how can I get application path in my app, It would be great.
Many thanks,
If you want to get absolute path for application, you can get like this.
String applicationPath = request.getSession().getServletContext().getRealPath("")
It will provide absolute path for application.
I think , most of the time was difficult to find the path inside gsp.
but , you can use this :
var path = "${resource()}"+"/mycontroller/myaction" ;
gives you the path of the application.
Inside controller like when you need to upload file :
def webRootDir = servletContext.getRealPath("/")
def userDir = new File(webRootDir, "/payload/${session.user.login}")
userDir.mkdirs()
uploadedFile.transferTo( new File( userDir, uploadedFile.originalFilename))
copy this in your controller
print webRequest.baseUrl

Call a method from constructed string in Ruby / Rails

I've got a problem in doing some metaprogramming in Ruby / Rails which must be minor, but I can't get the clue.
I wan't to assign values to an active record relation, with my model having attributes:
MyModelClass.p1_id,
.p2_id,
...
.p8_id
SecondModel.position #Integer in (1..8)
I now want to do the following
sms = SecondModel.where(:xyz => 'bla')
sms.each do |sm|
mmc = MyModellClass.first
mmc.#somehow construct method here = sm.id
end
So that somehow this is accomplished
mmc.p1_id = sm.id
mmc.p2_id = sm.id
..
mmc.p8_id = sm.id
To sum up: I want to create that p*n*_id stuff dynamically, but I can't find out, how to tell Ruby, that this should be a method. I tried so far:
mmc.send('p#{sm.position.to_s}_id'.to_sym) = sm.id
But this doesn't work. Any clues?
You were close. Try this:
mmc.send("p#{sm.position.to_s}_id=", sm.id)
Here we call the method with = and pass the value of attribute as the second argument of send

Nokogiri Timeout::Error when scraping own site

Nokogiri works fine for me in the console, but if I put it anywhere... Model, View, or Controller, it times out.
I'd like to use it 1 of 2 ways...
Controller
def show
#design = Design.find(params[:id])
doc = Nokogiri::HTML(open(design_url(#design)))
images = doc.css('.well img') ? doc.css('.well img').map{ |i| i['src'] } : []
end
or...
Model
def first_image
doc = Nokogiri::HTML(open("http://localhost:3000/blog/#{self.id}"))
image = doc.css('.well img')[0] ? doc.css('.well img')[0]['src'] : nil
self.update_attribute(:photo_url, image)
end
Both result in a timeout, though they work perfectly in the console.
When you run your Nokogiri code from the console, you're referencing your development server at localhost:3000. Thus, there are two instances running: one making the call (your console) and one answering the call (your server)
When you run it from within your app, you are referencing the app itself, which is causing an infinite loop since there is no available resource to respond to your call (that resource is the one making the call!). So you would need to be running multiple instances with something like Unicorn (or simply another localhost instance at a different port), and you would need at least one of those instances to be free to answer the Nokogiri request.
If you plan to run this in production, just know that this setup will require an available resource to answer the Nokogiri request, so you're essentially tying up 2 instances with each call. So if you have 4 instances and all 4 happen to make the call at the same time, your whole application is screwed. You'll probably experience pretty severe degradation with only 1 or 2 calls at a time as well...
Im not sure what default value of timeout.
But you can specify some timeout value like below.
require 'net/http'
http = Net::HTTP.new('localhost')
http.open_timeout = 100
http.read_timeout = 100
Nokogiri.parse(http.get("/blog/#{self.id}").body)
Finally you can find what is the problem as you can control timeout value.
So, with tyler's advice I dug into what I was doing a bit more. Because of the disconnect that ckeditor has with the images, due to carrierwave and S3, I can't get any info direct from the uploader (at least it seems that way to me).
Instead, I'm sticking with nokogiri, and it's working wonderfully. I realized what I was actually doing with the open() command, and it was completely unnecessary. Nokogiri parses HTML. I can give it HTML in for form of #design.content! Duh, on my part.
So, this is how I'm scraping my own site, to get the images associated with a blog entry:
designs_controller.rb
def create
params[:design][:photo_url] = Nokogiri::HTML(params[:design][:content]).css('img').map{ |i| i['src']}[0]
#design = Design.new(params[:design])
if #design.save
flash[:success] = "Design created"
redirect_to designs_url
else
render 'designs/new'
end
end
def show
#design = Design.find(params[:id])
#categories = #design.categories
#tags = #categories.map {|c| c.name}
#related = Design.joins(:categories).where('categories.name' => #tags).reject {|d| d.id == #design.id}.uniq
set_meta_tags og: {
title: #design.name,
type: 'article',
url: design_url(#design),
image: Nokogiri::HTML(#design.content).css('img').map{ |i| i['src']},
article: {
published_time: #design.published_at.to_datetime,
modified_time: #design.updated_at.to_datetime,
author: 'Alphabetic Design',
section: 'Designs',
tag: #tags
}
}
end
The Update action has the same code for Nokogiri as the Create action.
Seems kind of obvious now that I'm looking at it, lol. I dwelled on this for longer than I'd like to admit...

ImageScalr + Grails + AWS plugin

Im trying to implement some pics upload to my webapp.
The problem im having is that im trying to use Imagescalr to create thumbs of the original photos, and Im using AWS Plugin to upload them to my bucket.
So, the code i've is the following: (deleted validations and things that dont influence the question/posible answer)
def uploadPic() {
def f = request.getFile('file')
.
.
.
def s3file = f.inputStream.s3upload(filename) { //this is for the normal photo
path "POI/ID/"
}
def imageIn = ImageIO.read(???); //Dont know if I can put the f file here as parameter... do I have to store it somewhere first, call the s3 file, or I can resize on - the - fly?
BufferedImage scaledImage = Scalr.resize(imageIn, 150);
//Here I should upload the thumb. How can I call something like what is done for the normal photo?
So the problems/questions are explained along the code, hope someone knows how to do this. Thanks in advance.
in Grails, request.getFile() doesn't return a java.io.File object. You could use the input stream to write out a file but I'd probably do something like this, although I'd use services and break things up a little more. But this should get you started in the right direction. Consider this more of a pseudo code workflow suggestion.
def uploadPic() {
def f = request.getFile('file')
def tempFile = new File('/some/local/dir/myImage.png')
f.transferTo(tempFile)
// upload the original image to S3
whateverApi.s3Upload(tempFile)
def bufferedImage = ImageIO.read(tempFile)
def scaledBufferedImage = Scalr.resize(bufferedImage, 150)
// write the scaledImg to disk
def scaledImage = new File('/some/local/dir/myImage-150.png');
ImageIO.write(scaledBufferedImage, "png", scaledImage);
//upload scaled image to S3
whateverApi.s3Upload(scaledImage)
// clean up
tempFile.delete()
scaledImage.delete()
}

How to save a paperclip attachement from the params hash?

I'm trying to create an instance of my CompanyLogo model if the hash params[:vacancy]['companies']['company_logo'] is pressent. I'm attempting to do this:
l = CompanyLogo.new( :company_logo => params[:vacancy]['companies']['company_logo'].tempfile.path )
but that gives me
CompanyLogo(#70304040244500) expected, got ActionDispatch::Http::UploadedFile(#70304015073440)
Is there a way to do this?
If your CompanyLogo has_attached :logo,and you are saving file manually then simply company_logo_object.logo = File.new(actual_path) and
company_logo_object.save solve your purpose
Try saving the logo first (tested in Rails 3.2.2 using Paperclip)
(logo = CompanyLogo.new(:image => params[:image])).save
company_object.update_attributes(logo: logo)

Resources