Search code examples
ruby-on-railsrubybundler

How can I write a method that extracts :path and file name from the file instance, to use in a scanner method?


I'm trying to extract the path and file name from an uploaded file so I can run scanner on them. Thanks!

The ideas I had were:

scanner = Bundler::Audit::Scanner.new(root=:path,gemfile_lock=file_file_name)

scanner = Bundler::Audit::Scanner.new(File.dirname,File.file_file_name)


Solution

  • Have you checked the guide for uploading files? The params will return a StringIO or File object that provides the upload as a Tempfile. Using a gem like Paperclip makes the process of uploading and scanning much simpler, so would definitely recommend trying it out.

    Also, the File methods you called are class methods, so you need to pass an instance to them.

    Assuming that you want to scan the Tempfile, then save or discard it, you could do something like this in your controller:

    def upload_file
      uploaded_io = params[:uploaded_file]
      tempfile = uploaded_io.tempfile
      path = tempfile.path
    
      # If you need to separate the dir and filename
      dir = File.dirname(path)
      filename = File.basename(path)
      scanner = Bundler::Audit::Scanner.new(dir, filename)
    end