2013-06-28 35 views
0

我在Heroku/Ruby Sinatra中構建了一個Web工具,用於刮取Web域並下載所有指定的文件類型(它應該提供要下載的域的文件類型的站點地圖的zip文件) 。在Heroku中構建可下載的站點地圖zip文件

我想弄清楚如何在Heroku上構建ZipFile。我如何設置主目錄?然後,一旦我有ZipFile,我如何鏈接到它,所以它是可下載的?

下面是一些relavent代碼至今:

anemone.after_crawl do 
    puts "Crawl finished. Gathering files, preparing download..." 
     datasets.each do |url| 
     u = URI.parse(url.to_s) 

     Net::HTTP.start(u.host) { |http| 
      resp = http.get(u.path) 
      if u.path[0] == "/" 
      u.path[0] = '' 
      end 
      full_path = u.path.split("/") 
      i = 0 
      len = full_path.size 
      filename = full_path[-1] 

      Zip::ZipFile.open(u.host + ".zip", Zip::ZipFile::CREATE) { 
       |zipfile| 
       while i < (len-1) do 
       directory = full_path[i] 
       unless File.directory?(directory) 
        zipfile.mkdir(directory) 
       end 
       Dir.chdir directory 
       i+=1 
       end 

       zipfile.add(filename); 

       while (i > 0) do 
       Dir.chdir File.expand_path("..",Dir.pwd) 
       i-=1 
       end 
      } 
     } 
     end 
    end 

回答

0

Heroku的文件系統是mostly read-only,但你應該能夠暫時藏匿在/tmp zip文件:

Zip::ZipFile.open("#{RAILS_ROOT}/tmp/" + u.host + ".zip", Zip::ZipFile::CREATE) 

你可能要在「下載」控制器中使用send_file以允許用戶下載該文件。如果臨時文件在用戶下載之前消失(例如,如果在zipfile創建和下載之間重新啓動dyno),您將希望構建錯誤處理。

編輯

我聯繫的文件顯然是不合時宜的。 RAILS_ROOT是Rails 2引用目錄根的方式,但Rails 3方式(Rails.root)也不起作用 - 在Heroku中它指的是./app文件夾。

但是,您可以使用Heroku的基礎文件系統文件夾/tmp,像這樣:

Zip::ZipFile.open("/tmp/" + u.host + ".zip", Zip::ZipFile::CREATE) 
+0

謝謝你,我很感激。 – dbuss1