I have images uploaded to amazon s3 bucket. When I tried to recreate_versions!, It gives me a nil body exception.
I think this is due to changes in previous uploader settings in our code. However, when I do pr.image.url, it still gives me the original image, so what I tried is below:
begin
User.all.each do |pr|
if pr.user.present?
pr.remote_avatar_url = pr.avatar.url
pr.save!
end
end
rescue
end
But it throws an error:
ActiveRecord::RecordInvalid: Validation failed: Avatar trying to
download a file which is not served over HTTP
Which I know is carrierwave exception. What I'm trying to do is, I want to reupload all the images (because pr.avatar.url give me the original image), but I don't know how to do it. Any help will be greatly appreciated.
You are correct in attempting to store the remote URL in an attribute called remote_avatar_url.
CarrierWave throws the Validation failed: ATTRIBUTE trying to download a file which is not served over HTTP exception when attempting to save an invalid URL to the model. More specifically, CarrierWave::Uploader::Download raises a CarrierWave::DownloadError when the downloaded file "scheme" attribute does not match the regex /^https/ (meaning the URL does not start with "https"). You can view this logic here. (In particular, see lines 31 and 69.)
I'm not sure if this is the problem, but you might try checking the pr.avatar.url to see whether it begins with the https prefix before assigning it to the remote_avatar_url.
I hope this was at least somewhat helpful.
To re-upload the image, you need to download the image, if your carrierwave attr is remote_avatar, then maybe you can do something like:
begin
User.all.each do |pr|
if pr.user.present?
pr.remote_avatar = File.open(pr.avatar.url, 'rb')
pr.save!
end
end
rescue
end
Related
I am building an application that has a chat component to it. The application allows users to upload files to the chat. The chat is all javascript but i wanted to use Carrierwave for the uploads because i am using it elsewhere in the application. I am doing the handling of the uploads through AJAX so that i can get into Rails land and let Carrierwave take over.
I have been able to get the chat to successfully upload the files to the correct location in my S3 bucket. The thing i can't figure out is how to delete the files. Here is my code the uploads the files - this is the method that is called from the route that the AJAX call hits.
def upload
file = File.open(params[:file_0].tempfile)
uploader = ChatUploader.new
uploader.store!(file)
end
There is little to no documentation with Carrierwave on how to upload files without going through a model and basically NO documentation on how to remove files without going through a model. I assume it is possible though - i just need to know what to call. So i guess my question is how do i delete files?
UPDATE (11/23)
I got the code to save and delete files from S3 using these methods:
# code to save the file
def upload
file = File.open(params[:file_0].tempfile)
uploader = ChatUploader.new
uploader.store!(file)
uploader.store_path()
end
# code to remove files
def remove_file
file = params[:file]
uploader = ChatUploader.new
uploader.retrieve_from_store!(file)
uploader.remove!
end
My only issue now is that the filename for the uploaded file is not correct. It saves all files with a "RackMultipart" and then some numbers which look like a date, time, and identifier? (example: RackMultipart20141123-17740-1tq4j1g) Need to try and use the original filename plus maybe a timestamp for uniqueness.
I believe it has something to do with these two lines:
file = File.open(params[:file_0].tempfile)
and
uploader.store!(file)
In my rails app, I'm using Carrierwave to upload images on Amazon S3. I'd like to point to existing Amazon S3 images without having to re-upload the image. For example, if I have an existing Amazon S3 image at http://test.s3.amazonaws.com/image/path/0001/image.jpg, can I update an image's path to point to this image? I don't want to use the remote upload option because I really just want to use the same exact image that's already there (but save it in my record's "path" attribute).
In the console, I've tried:
image.update_attributes(:path=> "http://test.s3.amazonaws.com/image/path/0001/image.jpg")
but this fails to override the image's path.
Chiming in, better late than never! Caveat: This is for rails 4, and I am testing on rails 4.1 only at the moment.
This is harder than it should be, methinks! The reason this was absolutely crucial to me was that I am attaching 100MB+ MP3 files, which I cannot receive on my host, due to CloudFlare SSL limitations (and common sense). Fortunately, AWS supports preauthorized uploads, and I got carrierwave to do the right thing for me:
Step 1: get carrierwave to tell me where it would store a file if it could:
m.raw_write_attribute('file','file.mp3');
url = m.file.url
signed = aws_presigned_url(url)
raw_write_attribute does not save anything, it just bypasses carrierwave when setting the value. This makes the object act as if it read 'file.mp3' out of the database. Then you can ask Carrierwave "where the file lives". I then upload the file directly from the client to S3. When that's done, I make another API call to Rails, which performs the following code:
m.raw_write_attribute('file','file.mp3');
m.update_attribute('file','file.mp3');
These two paired get around Carrierwave. The first makes carrierwave think that the 'file' column is set to 'file.mp3', the second explicitly tells rails to persist 'file.mp3' to the DB. Because of the raw_write_attribute call, Carrierwave allows the second through un-changed.
In my case update_column and update_columns worked great:
model.update_columns file_1: 'filename.txt'
Update column is with comma:
model.update_column :file_1, 'filename.txt'
This will not run any callback and set column to filename.txt.
When I do model.file_1.url I get the right S3 URL.
I am a bit late to the party, but you can use Active Record's raw_write_attribute method something like:
#image.raw_write_attribute(:path, "http://test.s3.amazonaws.com/image/path/0001/image.jpg")
I found that you can actually do this, for example if your mount_uploader is :path, then:
image.remote_path_url = "http://test.s3.amazonaws.com/image/path/0001/image.jpg"
image.save
I'm not sure if this is a paperclip issue. Tried it on gitlab and the same thing happened.
I have a back end for an iOS app written in Rails, and when I upload an image file with the # character in the filename, it gets erased upon uploading, if I have a file named,
aaa#2x.jpg
it gets saved as
aaa2x.jpg
Also, ~ gets converted into a _.
This is a problem because iOS apps presume that retina supported images are named with the #2x prefix.
I can regex the file name post upload and change it in the database and rename the file, but that seems like an odd hack to do, anyone have any idea whats happening? How to have the file name saved properly to begin with?
According to this article: http://en.wikipedia.org/wiki/HFS_Plus, you should be able to use any character, including NUL in file names. But OS APIs may limit some characters for legacy reasons.
It can be server or client issue, try to debug your application and check file name provided in request.request_parameters it should contain valid file name.
If you going to use uploaded files in URLs you should transliterate them before upload, this also resolve your problem. To do this you can use this extension:
module TransliteratePaperclip
def transliterate_file_name(paperclip_file)
paperclip_file=[paperclip_file] unless paperclip_file.is_a?(Enumerable)
paperclip_file.each do |file|
filename=read_attribute("#{file}_file_name")
if filename.present?
extension = File.extname(filename).gsub(/^\.+/, '')
filename = filename.gsub(/\.#{extension}$/, '')
self.send(file).instance_write(:file_name, "#{filename.parameterize}.#{extension.parameterize}")
end
end
end
end
# include the extension
ActiveRecord::Base.send(:include, TransliteratePaperclip)
put this code in /config/initializers/paperclip_transliterate.rb and in your paperclip model:
before_post_process { |c| transliterate_file_name(:file) }
where :file is attribute defined by has_attached_file.
Are there any gems out there for Rails 3.2.1 that generate website thumbnails? I see a lot of 3rd party solutions but I don't like the fact that they aren't hosted on my server. It's really important the app I'm building is as stable as possible and I think this is not a good solution in the long run.
My ruby knowledge is fairly good, I think enough to use a gem and implement it, but definitely not good enough to write something like this from scratch if no gems exist.
Thanks!
You could try dragonfly or carrierwave
Well, here's the first thing that came up on Rubygems: thumbnailer. It uses Amazon and costs a small fee per image it generates, so you probably don't want this...
But there's also thumbnailer-ruby which looks like it works completely on the local machine. Haven't tested it out, though. It appears that this doesn't actually do what you want. Nevermind.
Now another gem called snapurl looks pretty fancy. Once again, I haven't tried it out yet. I'll do that now.
EDIT: Won't run for me; keeps failing with an error.
https://url2png.com/ has worked great so far
aBrowshot has a gem available.
There's no need to use a third party service for this.
You can do something like this in your model:
class MySexyModel < ActiveRecord::Base
... stuff
# Generate the thumbnail on validate so we can return errors on failure
validate :generate_thumbnail_from_url
# Cleanup temp files when we are done
after_save :cleanup_temp_thumbnail
# Generate a thumbnail from the remote URL
def generate_thumbnail_from_url
# Skip thumbnail generation if:
# a) there are already other validation errors
# b) an image was manually specified
# c) an image is already stored and the URL hasn't changed
skip_generate = self.errors.any? || (self.image_changed? ||
(self.image_stored? && !self.url_changed?))
# p "*** generating thumbnail: #{!skip_generate}"
return if skip_generate
# Generate and assign an image or set a validation error
begin
tempfile = temp_thumbnail_path
cmd = "wkhtmltoimage --quality 95 \"#{self.url}\" \"#{tempfile}\""
# p "*** grabbing thumbnail: #{cmd}"
system(cmd) # sometimes returns false even if image was saved
self.image = File.new(tempfile) # will throw if not saved
rescue => e
# p "*** thumbnail error: #{e}"
self.errors.add(:base, "Cannot generate thumbnail. Is your URL valid?")
ensure
end
end
# Return the absolute path to the temporary thumbnail file
def temp_thumbnail_path
File.expand_path("#{self.url.parameterize.slice(0, 20)}.jpg", Dragonfly.app.datastore.root_path)
end
# Cleanup the temporary thumbnail image
def cleanup_temp_thumbnail
File.delete(temp_thumbnail_path) rescue 0
end
end
The original post is on this blog: http://sourcey.com
I'm using paperclip for attachments in my application. I'm writing an import script for a bunch of old data, but I don't know how to create paperclip objects from files on disk. My first guess is to create mock CGI multipart objects, but that seems like a bit of a crude solution, and my initial attempt failed, I think because I didn't get the to_tempfile method right.
Is there a Right Way to do this? It seems like something that should be fairly easy.
I know that I've done the same thing, and I believe that I just created a File object from the path to each file, and assigned it to the image attribute. Paperclip will run on that file:
thing.image = File.new("/path/to/file.png")
thing.save
This works great for local files but it doesn't work as well for remote files. I have an app that uses paperclip for uploading images. Those images are getting stored on amazon s3. Anyway, I had some old data that I needed to import so I tried the following:
thing.image = open('http://www.someurl.com/path/to/image.jpg')
thing.save
If the file is small (say, less than 10K) then openuri returns a stringio object and my file would get stored on s3 as stringio.txt
If the file is larger than around 10K, then openuri returns a TempFile object. But the filename on s3 ends up being unique, but not really relating to the original filename of image.jpg
I was able to fix the problem by doing the following:
remote_photo = open('http://www.someurl.com/path/to/image.jpg')
def remote_photo.original_filename;base_uri.path.split('/').last; end
thing.image = remote_photo
thing.save