发现Windows文件的问题

时间:2014-01-08 20:25:44

标签: ruby-on-rails file

我正在尝试读取存储纬度和经度数据的不同卡车的一些文本文件,但无法在Windows PC上找到保存它们的文件。我已经尝试了多个gps_file变量(见下文),但每次在生产中,我都找不到文件错误(Errno :: ENOENT(没有这样的文件或目录 - C:\ Rtmx \ Reports \ 2014 \ January \ 08 \ 53 。文本))。当我“将文件复制为路径”时,我得到这个字符串 - “C:\ Rtmx \ Reports \ 2014 \ January \ 08#{truck_number} .txt”,这似乎与我的heroku日志匹配,但仍然抛出错误。当我通过不同的文件结构在dev中尝试我的mac时,它工作正常。几个小时以来我一直坚持这个问题。任何指导将不胜感激。谢谢!

当我在Chrome中打开文件时,我会为其中一辆卡车获取以下路径:file:/// C:/Rtmx/Reports/2014/January/08/75.txt

def update_locations
    require 'csv'
    @locations = Location.where(id: [3,4])

    @locations.each do |location|
       gps_file = "C:\\Rtmx\\Reports\\#{DateTime.now.strftime('%Y').to_s}\\#{DateTime.now.strftime('%B').to_s}\\#{DateTime.now.strftime('%d').to_s}\\#{location.locationable.name.to_s}.txt"
        data = File.read(gps_file).gsub(",","")
        csv = CSV.parse(data, :headers => true)
        @location_log = Array.new
        csv.each do |row|
          row = row
          @location_log << row
        end
        @latest_location_data = @location_log.last
        csv_array = "#{@latest_location_data},String".parse_csv
        array = csv_array.first.split("\t")
        raw_latitude = array[11].split(' ')
        latitude = raw_latitude[0].gsub("N","").to_f + raw_latitude[1].to_f/60 + raw_latitude[2].to_f/3600
        raw_longitude = array.last.split(' ')
        longitude = (raw_longitude[0].gsub("W","").to_f + raw_longitude[1].to_f/60 + raw_longitude[2].to_f/3600)*-1
        location.update(latitude: latitude, longitude: longitude)
      end
    redirect_to locations_url
  end

1 个答案:

答案 0 :(得分:0)

问题是由于尝试通过控制器读取用户计算机上的文件。我改为将文件实时推送到Amazon S3,并在控制器中从那里读取。

require 'csv'
require 'open-uri'

@locations = Location.all
    def test_url(gps_file, location)
      begin
        data = gps_file.read.gsub(",","")
        csv = CSV.parse(data, :headers => true)
        @location_log = Array.new
        csv.each do |row|
          @location_log << row
        end
        @latest_location_data = @location_log.last
        csv_array = "#{@latest_location_data},String".parse_csv
        array = csv_array.first.split("\t")
        lat = array[11]
        if lat[0].present?
          raw_latitude = lat.split(' ')
          latitude = raw_latitude[0].gsub("N","").to_f + raw_latitude[1].to_f/60 + raw_latitude[2].to_f/3600
          raw_longitude = array.last.split(' ')
          longitude = (raw_longitude[0].gsub("W","").to_f + raw_longitude[1].to_f/60 + raw_longitude[2].to_f/3600)*-1
          speed = array[7]
          location.update(latitude: latitude, longitude: longitude, speed: speed)
        end
      rescue
      end
    end
    @locations.each do |location|
      s3=AWS::S3.new(access_key_id: ENV['AWS_ACCESS_KEY_ID'], secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], region: 'us-east-1')
      gps_file = s3.buckets['wheresmyconcrete'].objects["real/#{DateTime.now.strftime('%B').to_s}/#{DateTime.now.strftime('%d').to_s}/#{location.locationable.name.to_s}.txt"]
      test_url(gps_file, location)
    end