2

Something I'm not getting about the version process...

I have a zip file with a file inside, and I want to upload the file as a "version" of the zip:

Uploader:

  version :specificFile do
    process :extract_file
  end

  def extract_file
    file = nil
    Zip::ZipFile.open(current_path) do |zip_file|
      file = zip_file.select{|f| f.name.match(/specificFile/)}.first
      zip_file.extract(file, "tmp/" + file.name.gsub("/", "-")){ true }
    end    
    File.open("tmp/" + file.name.gsub("/", "-"))
  end

Usage:

=link_to "Specific File", instance.uploader.specificFile.url

Only this just nets me two copies of the zip. Clearly, there's something I'm missing about how version / process works, and I haven't been able to find documentation that actually explains the magic.

So how do I do this, and what am I missing?

1 Answer 1

2

This provided the "why", although it took a bit to understand: How do you create a new file in a CarrierWave process?

To rephrase, when you go to create a version, carrierwave makes a copy of the file and then passes the process the file path. When the process exits, carrierwave will upload the contents of that path - not the file the process returns, which is what I thought was going on.

Working code:

  version :specificFile do
    process :extract_file
    def full_filename (for_file = model.logo.file)
      "SpecificFile.ext"
    end
  end

  def extract_plist
    file = nil
    Zip::ZipFile.open(current_path) do |zip_file|
      file = zip_file.select{|f| f.name.match(/specificFile/)}.first
      zip_file.extract(file, "tmp/" + file.name.gsub("/", "-")){ true }
    end    
    File.delete(current_path)
    FileUtils.cp("tmp/" + file.name.gsub("/", "-"), current_path)
  end

So, to make what I want to happen, happen, I:

  • Tell carrierwave to use a particular filename. I'm using a hardcoded value but you should be able to use whatever you want.

  • Overwrite the contents of current_path with the contents you want under the version name. In my case, I can't just overwrite the zip while I'm "in it" (I think), so I make a copy of the file I care about and overwrite the zip via File and FileUtils.

PS - It would be nice to avoid the duplication of the zip, but it doesn't look like you can tell carrierwave to skip the duplication.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.