Paperclip, id partition, and renaming images

I'm using paperclip in this Rails 2.3.8 project.

What I'm looking to do is rename all the images so they fit the :url =>
"/assets/:class/:id_partition/:attachment/:style.:extension" from my
model. I had about 30,000 records for this model and when changing to
start using the :id_partition, I changed

:url => "/assets/:class/:id/:attachment/:style.:extension"

to

:url => "/assets/:class/:id_partition/:style_:basename.:extension"

without thinking about what I was actually doing. I now changed it back
to

:url => "/assets/:class/:id_partition/:attachment/:style.:extension"

but now I have about 10,000 images that are named wrong and not showing.
I know there is a reprocess! method in paperclip, but it doesn't seem to
rename the images. Is there already a rake task, or something that will
do this?

Essentially, I would like to do

MyModel.all.each { |model|
model.image.set_all_the_styles_to_the_correct_name! }

Thanks,
~Jeremy

Jeremy Woertink wrote:

but now I have about 10,000 images that are named wrong and not showing.
I know there is a reprocess! method in paperclip, but it doesn't seem to
rename the images. Is there already a rake task, or something that will
do this?

Essentially, I would like to do

MyModel.all.each { |model|
model.image.set_all_the_styles_to_the_correct_name! }

Thanks,
~Jeremy

You're right Jeremy, reprocess! won't manipulate the naming or
interpolation. Whatever interpolation you have set *currently* is what
Paperclip will apply, so it won't be able to locate the original files
to reprocess anyhow.

I think your best bet is to work around Paperclip rather than through
it. Take a look at how the previous url would interpolate and compare
that to how the current url will interpolate, then perform directory
creation, moving, and directory removal from there. Because of the
possible url / path permutations I doubt there are any existing rake
tasks that will fit your particular example.

Something similar to:

Model.all.each { |model|
  model.file.styles.each do |style|
    extension = File.extname(model.image.original_filename)
    basename = File.basename(model.image.original_filename, extension)
    old_path = File.join(RAILS_ROOT, 'assets', model.class, model.id)
    old_file = "#{style}_#{basename}#{extension}"
    new_path = File.join(RAILS_ROOT, 'assets', model.class, model.id,
'image')
    new_file = "#{style}#{extension}"

    FileUtils.mkdir_p new_path unless File.exist?(new_path)
    FileUtils.mv File.join(old_path, old_file), File.join(new_path,
new_file)
  end
end

Of course that isn't tested, but it should give an idea. This is for the
filesystem and not S3, right?

Of course that isn't tested, but it should give an idea. This is for the
filesystem and not S3, right?

Yeah, this is for the local file system.

So some of my images look like

thumb.jpg, small.jpg, large.jpg

and some look like

thumb_23432S.jpg, small_643563456.jpg, large_2123425F.jpg

I want them all to look like the first set, so this would take care of
that problem?

I also have the issue of some of the images being `original.JPG`

I need to make sure all image file names are all downcase.

Thanks for the help!

~Jeremy

Jeremy Woertink wrote:

So some of my images look like

thumb.jpg, small.jpg, large.jpg

and some look like

thumb_23432S.jpg, small_643563456.jpg, large_2123425F.jpg

I want them all to look like the first set, so this would take care of
that problem?

So there is a mixture of files with the old path and files with the new
path? That will be more complicated, you'll have to check that the old
file exists at the old path before trying to do the move.

old_complete_path = File.join(old_path, old_file)

if File.exist?(old_complete_path)
  # Do your moving and directory making
else
  # This is 'new' content and is already in the correct place
  next
end

I also have the issue of some of the images being `original.JPG`

That is the easy part, you would change the new_file interpolation to:

new_file = "#{style}#{extension.downcase}"

- Parker