Class: UHFerret::FerretHomeServlet
- Inherits:
-
HTTPServlet::AbstractServlet
- Object
- HTTPServlet::AbstractServlet
- UHFerret::FerretHomeServlet
- Defined in:
- lib/webferret.rb
Overview
Displays a welcome page, providing a field to upload the zipped file. On pressing 'submit', runs Ferret and passes results to FerretResultsServlet.
Instance Method Summary collapse
-
#create_file_definitions(folder) ⇒ Object
find all files in given folder and add their names to a definitions file -- return true if files are text documents, or false if not.
-
#decompress_file(filename) ⇒ Object
If filename names a known compressed file format, it is decompressed and deleted.
-
#do_GET(req, res) ⇒ Object
Returns the 'welcome page' html.
-
#do_POST(req, res) ⇒ Object
this method is triggered when the user clicks on 'submit query'.
-
#endsWith?(str, str_end) ⇒ Boolean
Convenience method to check if a string ends with given ending.
-
#isCompressedFile?(filename) ⇒ Boolean
Checks if given filename is an example of a compressed file.
Instance Method Details
#create_file_definitions(folder) ⇒ Object
find all files in given folder and add their names to a definitions file -- return true if files are text documents, or false if not
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/webferret.rb', line 123 def create_file_definitions folder text_files = true Dir.chdir folder files = [] Doc.glob(folder+"**/*") do |filename| next unless File.file?(filename) # ignore directories files << filename text_files = false if Utils.is_code?(filename) end # write the names of valid files into a definitions file File.open("ferret-file-definitions.def", "w") do |defn_file| files.each do |f| defn_file.puts f if Utils.valid_document? f end end return text_files end |
#decompress_file(filename) ⇒ Object
If filename names a known compressed file format, it is decompressed and deleted.
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/webferret.rb', line 108 def decompress_file filename if endsWith?(filename, "rar") `unrar x #{filename}` if Utils.command_present? "unrar" elsif endsWith?(filename, "tbz2") || endsWith?(filename, "tar.bz2") `tar jxf #{filename}` if Utils.command_present? "tar" elsif endsWith?(filename, "tgz") || endsWith?(filename, "tar.gz") `tar zxf #{filename}` if Utils.command_present? "tar" elsif endsWith?(filename, "zip") `unzip #{filename}` if Utils.command_present? "unzip" end File.delete filename # remove the compressed folder end |
#do_GET(req, res) ⇒ Object
Returns the 'welcome page' html.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/webferret.rb', line 36 def do_GET(req, res) res["Content-Type"] = "text/html" res.body = "<html><body><h1>Ferret Server</h1>\n<p>\nFerret is a tool for detecting copying in groups of documents, \nand was created by the (now defunct) Plagiarism Detection Group, \nUniversity of Hertfordshire.\n</p>\n<form method=\"POST\" enctype=\"multipart/form-data\">\n<p>Compressed file: <input type=\"file\" name=\"data\" size=\"40\">\n <p><input type=\"submit\"/>\n</p>\n</form>\n\n<hr><h2>Instructions for use</h2>\n<p>\n<ol>\n<li>Construct a compressed folder of your files in a way suitable for \nyour own computer. The Ferret Server will handle a compressed \nfolder in one of the following forms: \n\#{Utils::CompressedFileExtensions.map {|ext| \"<tt>.\#{ext}</tt>\"}.join(\", \")}.\nThe files within it may be as:\n<ul>\n<li>plain text files</li>\n\#{if Utils.command_present?(\"abiword\")\n \"<li>word-processed files (such as <tt>doc</tt> or <tt>rtf</tt> files)</li>\"\n else\n \"\"\n end\n}\n\#{if Utils.command_present?(\"pdftotext\")\n \"<li><tt>pdf</tt> documents</li>\"\n else\n \"\"\n end\n}\n</ul>\nFiles may contain natural language text or computer programs (C-type \nlanguages).\n</li>\n<li>Use the 'Browse' button to select your compressed file.\n</li>\n<li>Once Ferret has finished analysing the documents, the display will show \na table of the top 100 results.\n</li>\n<li>Click on the 'view' link beside each pair to see a report of \nthe comparisons found in that pair of documents. Use the print option of \nyour browser to preserve a copy (e.g. using 'print to pdf').\n</li>\n</ol>\n</p>\n<hr><font size=-1>Ferret home page generated on: \#{Time.now}<br />.\n</font>\n</body>\n</html>\n" end |
#do_POST(req, res) ⇒ Object
this method is triggered when the user clicks on 'submit query'
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/webferret.rb', line 142 def do_POST(req, res) upload_dir = "Upload#{@@next_upload}" # create a unique folder for user's files @@next_upload += 1 Dir.mkdir $base unless File.exist? $base Dir.mkdir "#{$base}/#{upload_dir}" upload_data = req.query["data"] filename = upload_data.filename.gsub(" ", "-") # replace spaces uploaded_file = "#{$base}/#{upload_dir}/#{filename}" File.open(uploaded_file, "wb") do |file| # do the actual upload of the data upload_data.each_data do |data| file << data.to_s end end # if uploaded file is a compressed file, then decompress and compute similarities if isCompressedFile?(uploaded_file) Dir.chdir "#{$base}/#{upload_dir}" decompress_file File.basename(uploaded_file) is_text = create_file_definitions Dir.pwd # do the computation of similarities # -- output to html table with given folder name, using file definition list `#{FERRET} #{is_text ? "-t" : "-c"} -w -f ferret-file-definitions.def > results.html` res["Content-Type"] = "text/html" res.body = "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=#{$base}/#{upload_dir}/results.html\">" else res["Content-Type"] = "text/html" res.body = %{<html><body><h1>Error</h1> <p>You did not submit a valid zip file.</p> <p><a href="/ferret/home">Return to Ferret home page</a>.</p> </body></html>} end end |
#endsWith?(str, str_end) ⇒ Boolean
Convenience method to check if a string ends with given ending.
96 97 98 99 |
# File 'lib/webferret.rb', line 96 def endsWith?(str, str_end) return false if str.length < str_end.length str[-str_end.length .. -1] == str_end end |
#isCompressedFile?(filename) ⇒ Boolean
Checks if given filename is an example of a compressed file.
102 103 104 |
# File 'lib/webferret.rb', line 102 def isCompressedFile? filename Utils::CompressedFileExtensions.any? {|e| endsWith?(filename, e) } end |