0

My script edits an XML file using:

stringComplete = File.read("TheUpload.xml",encoding: "UTF-8").gsub(my mystery gsub)

and regenerates using:

newfile = File.open("EditXML.xml", "w") 
newfile.puts "method(xyz)"

How do I correct my script so that it doesn't matter what file name has the ".xml" that is uploaded and that every single user gets his own edited .xml file and not by someone else?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
lucid lynx
  • 13
  • 3
  • Rule 1: [Don't modify XML files using regular expressions](http://stackoverflow.com/a/1732454/128421). While that answer is tongue-in-cheek, the rest of the question's answers discuss the pitfalls in wait for you. Use a parser, such as Nokogiri, to read the content, turn it into something you can use to reliably find and replace your target text and then regenerate the new XML. Only if you own the initial XML generation should you consider using regex. – the Tin Man Nov 24 '14 at 16:26

1 Answers1

0

When uploading a file, you don't need to save it to disk on the server - simply read the upload IO (see here):

view.erb:

<%= form_tag({action: :upload}, multipart: true) do %>
  <%= file_field_tag 'xml' %>
<% end %>

<%= form_for @upload do |f| %>
  <%= f.file_field :xml %>
<% end %>

controller:

uploaded_io = params[:upload][:xml]
string_complete = uploaded_io.read.gsub(whatever)

For downloading, you also don't need to save to disk - simply serve the new file contents (see here)

send_data string_complete, filename: 'EditXML.xml'

If the files were uploaded to a directory in some other way, you can scan the input directory using Dir.glob, and then iterate over each, creating a new file in the output file with the name based on the input file's name:

Dir.glob(File.join(input_directory, '*.xml')).each do |input_file|
  stringComplete = File.read(input_file,encoding: "UTF-8").gsub(my mystery gsub)
  input_file_basename = File.basename(input_file)

  newfile = File.open(File.join(output_directory, input_file_basename + '_output.xml', "w") 
  newfile.puts "method(xyz)"
end
Community
  • 1
  • 1
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
  • There will be some users uplaoding .xml files on a server. How do my ruby script gets every single .xml file to edit it and give each edited .xml file to each user? – lucid lynx Nov 24 '14 at 12:53
  • @lucidlynx - i was under the impression that this is a _web service_... how are the users uploading the files? where to? what names do they give the files? what is the trigger which runs the script? – Uri Agassi Nov 24 '14 at 14:19
  • The users upload their .xml files on an apache webserver. The website is an php script with html upload field. The names of the files are always different. The problem is that there isn't ruby installed on this apache... I asked the question here http://stackoverflow.com/questions/27099612/do-i-have-to-install-ruby-on-my-apache-server-to-run-scripts whether ruby is necessary on an apache. – lucid lynx Nov 24 '14 at 14:56
  • @lucidlynx - added a solution for a file-based input/output – Uri Agassi Nov 24 '14 at 17:03
  • Many thanks, my problem is I have three regex... I'll try with XAMPP – lucid lynx Nov 25 '14 at 08:53