services_controller.rb 1.33 KB
class Slibras::ServicesController < Slibras::BaseController
  respond_to :html, :json

  def index

  end

  def data
    output = %x[ip addr show eth0 | grep 'inet' | awk '{print $2}']
    saida = output.split('/')
    getIP = saida[0]

    path = params[:subtitle]
    file = File.new("public/"+path,"r")
    array = []
    test = ""
    
    while (line = file.gets)
     array << line.delete("\n")
    end

    file.close
    
    new_array = []
    count=0
    pos = array.size / 4 
    
    0.upto(pos) do |name_pos|
      new_array << {"id" => array[count], "time" => array[count+1].split("-->"), "title" => array[count+2]}
      count+=4
    end

    render :json =>  new_array
  end	

  def write
    subtitle_updated = params[:payload].split(",")
    subtitle = params[:subtitle]

    private_params = subtitle.split("/")
    id = private_params[4]
 
    oldfile = File.new("public"+subtitle,"r")
    array = []
    while (line = oldfile.gets)
     array << line.delete("\n")
    end
   
    count=0;
    subtitle_updated.each do |sub|
      array[2+count].replace(sub)
      count+=4
    end
    oldfile.close

    newfile = File.new("public"+subtitle,"w+")
    array.each do |element|
      newfile.write(element)
      newfile.write("\n")
    end
    newfile.close
   
    redirect_to generate_path(:id => id, :closed_caption => 0)
  end		
end