Ruby 2.3.1 how to set the project root directory in config.rb

547 views Asked by At

I have a project in Ruby 2.3.1 and I'm using at the moment absolute path to a folder where I'm having uploaded files. What I would like to use instead is a relative path. I would like to know how to set in Ruby config.rb the root directory / so I could call when I need the relative path to a folder like root_dir + '/public/files'

The project is a Ruby RESTFull API which works with Solr for index/search PDF files. The app is a custom app with Sinatra and Rsolr. This API has an upload function which points to a folder.

My Upload script:

post '/api/uploads' do
  #puts params
  fns = params['files'].map {|f| f[:filename]}.join(";")
  param = fns.chomp.split(";")
  #find pdf files
  param.select! {|f| f =~ /.*\.pdf/}

  array_length = param.length # or $param.size

  puts "length of $param is : #{array_length}"

  if(array_length==0)
    raise( "files not pdf: #{fns}")
  end
  i = 0
  resp = []
  while i.to_i < array_length do
    puts i
    filename = params[:documents][i][:filename]
    tmpfile = params[:documents][i][:tempfile]
    target = settings.homedir+'/'+filename
    localpath = File.join(settings.filesdir, filename)
    File.open(localpath, 'wb') do |f|
      f.write tmpfile.read
    end

    resp << {:fileurl => target}
    i = i + 1
  end

My config.rb:

set :root, File.dirname(__FILE__)
    set :filesdir, 'absolute_path/public/documents/pdf' # Set pdf files folder absolute path
    set :homedir, '/pdf'                         # Set pdf home directory
    set :solrbase, 'http://localhost:8983/solr/' # Set Solr connection path
    set :solrcore, 'docs'                        # Set name of the Solr core


    set :bind, '0.0.0.0'
    set :port, 3003                              # Set port of the API

    set :show_exceptions, false

    configure do
        mime_type :pdf, 'application/pdf'
  end

I would like to not write the absolute path and get a relative path so for example, another developer can use it immediately without set again the absolute path.

0

There are 0 answers