0

I'm building a log parser app. I followed a tutorial on how to make an upload function, which worked. My new.html.erb for my upload function looks like this (the tutorial was tailored to accept resumes, so some of the methods from my code read @resume):

   <div class="container">   
       <% if @resume.errors.present? %>    
          <div>   
             <ul>   
                <% @resume.errors.full_messages.each do |msg| %>   
                   <li><%= msg %></li>   
                <% end %>
               </ul>   
          </div>   
       <% end %>   

   <div>   
      <%= form_for @resume, html: { multipart: true } do |f| %>   
         <%= f.label :name %>   
         <%= f.text_field :name %>   
         <br><br>   
         <%= f.label :attachment %>   
         <%= f.file_field :attachment %>   
         <br>   
         <%= f.submit "Save" %>   
      <% end %>   
   </div>   

My resume.rb looks like:

class Resume < ApplicationRecord
   mount_uploader :attachment, AttachmentUploader  

   validates :name, presence: true 
end

My attachment_uploader.rb looks like:

class AttachmentUploader < CarrierWave::Uploader::Base
  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

end

As a next step, I'm trying to upload a zipped file containing multiple log files. I came across this question, which explains how to extract it, but not how to extract upon uploading the zipped file. The code from there looks like:

Zip::ZipFile.open(file_path) { |zip_file|
     zip_file.each { |f|
     f_path=File.join("destination_path", f.name)
     FileUtils.mkdir_p(File.dirname(f_path))
     zip_file.extract(f, f_path) unless File.exist?(f_path)
   }
  } 

Where should I place those particular lines of code (from the url I pasted above) in my program if they are compatible at all? Should it be in a .erb file or a .rb file, and if so, which one and how?

sawa
  • 165,429
  • 45
  • 277
  • 381
C.N.N.
  • 91
  • 8

1 Answers1

0

Simplest way just to test if your code works - you can process synchronously in controller after the model is saved successfully (in create action).

For most real scenarios where files can be large and take time to process - better way is to process asynchronously in background jobs, for that rails have ActiveJob framework

Vasfed
  • 18,013
  • 10
  • 47
  • 53