9

I'm creating a thumb image for the files that have been uploaded on my app. The image names have a timestamp code in them. When I run recreate_versions the thumb image generated also has the timestamp in it but it uses the current timestamp which makes the thumb image name differ from the original filename.

So I figured a fix would be to have a custom filename for the thumb image. Basically have 'thumb_' + 'original filename'.

   version :thumb do
     process :resize_to_limit => [110, nil]

     def full_filename(for_file = model.image_value.file)
     'thumb_' + File.basename(model.image_value.path).to_s
     end
   end

  def filename(for_file = model.image_value.file)
   "#{model.id}" + "-v#{timestamp}" + "-" + "#{model.name}" + ".png" if original_filename.present?
  end

  def timestamp
    var = :"@#{mounted_as}_timestamp"
    model.instance_variable_get(var) or model.instance_variable_set(var, Time.now.to_i)
  end

This seemed like an easy fix but for some reason when I run recreate_versions the thumb image is generated with the current timestamp in it, not the timestamp in the name of the original filename. From what I understand it should grab the value stored in the DB that is the raw filename and add it to the end of 'thumb_'. But somehow its changing the timestamp in the name.

stored filename                      =       1-v1474175808-model-name.png
Item.find(1).image_value_url(:thumb) = thumb_1-v1474175808-model-name.png #this works correctly and looks for the correct filename
thumb filename saved                 = thumb_1-v1472111618-model-name.png #thumb saved is different. For some reason has a different timestamp in name

I thought maybe def full_filename isnt being run, but if I change it to something else the thumb filename saved is changed to what is in def full_filename.

Not sure what is going on here. hopefully someone can help. If it looks like it should work please let me know, atleast that will clarify that it could be something I'm not looking at.

Rob
  • 1,835
  • 2
  • 25
  • 53

2 Answers2

1

Ended up having to use the generated url and using slice! on the url to leave just the image name.

   version :thumb do
     process :resize_to_limit => [110, nil]

    def full_filename(for_file = model.image_value.file)
      raw_file_name = model.image_value.slice!(0..65)
     'thumb_' + raw_file_name
     end

  end
Rob
  • 1,835
  • 2
  • 25
  • 53
0

I don't understand why you're using instance_variable_get and set. Why not just use the Item's created_at? Then it'll never change.

def timestamp      
  model.created_at
end
Benjamin
  • 1,060
  • 11
  • 16
  • Because the name needs to change and be uniq when the image is updated. – Rob Sep 21 '16 at 23:57
  • So you need all versions to have a new, unique name? – Benjamin Sep 22 '16 at 04:00
  • No the versions just need to have the same name as the original file but with 'thumb_' added to the front of its name. – Rob Sep 22 '16 at 04:04
  • Then what is the function of the timestamp, that it must it be generated from `Time.now.to_i`, rather than taking the timestamp from the model the uploader is mounted on? – Benjamin Sep 22 '16 at 13:14