paperclip upload zip file

Hi, I am trying to upload a zip file, open the file which should contains image files.
My idea was to create an upload button for a file, unzip the file and get image by image, create a Photo which is the class which has has_attached_file.
I am able to upload the file and seems I am able to extract each image file, however I am having problems when creating the Photo which will be added to an event( event has_many photos).
Here is my function where I am trying to create photos.
  def create_photos_from_zip
    logger.debug "create_photos_from_zip -->"
    zip = params[:zip]
    logger.debug "unzipping file #{zip.path} class #{zip.class}"

    Zip::ZipFile.open(zip.path).each do |entry|
      # entry is a Zip::Entry
      filename = "#{Dir::tmpdir}/#{current_user.name}_#{entry.name}"
      logger.debug "will extract file to #{filename} to"
      entry.extract(filename)
      p = File.open(filename, 'r+')
      @event.photos << Photo.create(:photo=>p)
      #delete the temp file
      #FileUtils.remove_file(filename)
    end
    res = @event.save
    logger.debug "create_photos_from_zip <--"
    return res
  end

This is my create function
 def create
    if (params[:zip].present?)
      res = create_photos_from_zip
      render :nothing=>true
    else
      @photo = @event.photos.build(params[:photo])
      res = @photo.save
      respond_to do |format|
        if res
          flash[:notice] = 'Photo was successfully created.'
          format.html { redirect_to([@event,@photo]) }
          format.xml  { render :xml => @photo, :status => :created, :location => @photo }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @photo.errors, :status => :unprocessable_entity }
        end
      end      
    end
  end
* I know I need to work more here, but I am stuck !!!

I am not getting any error, however the files are not attached to the event as well, I am not sure if I am unzipping at the client or server, I am developing in the same machine ...
Any Help would be very appreciated!
Please let me know If you need any other information.
LVL 2
lusfernandosAsked:
Who is Participating?
 
lusfernandosConnect With a Mentor Author Commented:
Today I redid the function and now it is working, I have a validation for title, which I was not including and I had some confusion where I was extracting the files. so here is the unzip funtion.
  def create_photos_from_zip
    logger.debug "create_photos_from_zip -->"
    zip = params[:zip]
    logger.debug "unzipping file #{zip.path} class #{zip.class}"

    dir = File.join(RAILS_ROOT,"public","events","temp",current_user.name)
    FileUtils.mkdir_p(dir)
    Zip::ZipFile.open(zip.path).each do |entry|
      # entry is a Zip::Entry
      filename = File.join(dir,entry.name)
      logger.debug "will extract file to #{filename} to"
      entry.extract(filename)
      p = File.new(filename)
      photo = Photo.create(:photo=>p)
      photo.title = "nova imagem"
      @event.photos << photo
      p.close
      FileUtils.remove_file(filename)
    end
    #delete zip file
    logger.debug "closing/deleting zip file #{zip.path}"
    zip.close
    FileUtils.remove_file(zip.path)
    logger.debug "saving the event to database"
    logger.debug "create_photos_from_zip <--"
  end
0
All Courses

From novice to tech pro — start learning today.