diff --git a/webapp/tabula_web.rb b/webapp/tabula_web.rb index ab16877f..f0de96fd 100644 --- a/webapp/tabula_web.rb +++ b/webapp/tabula_web.rb @@ -77,30 +77,6 @@ def upload(file) return [job_batch, file_id] end - -def create_template(template_info) - template_name = template_info["name"] || "Unnamed Template #{Time.now.to_s}" - template_id = Digest::SHA1.hexdigest(Time.now.to_s + template_name) # just SHA1 of time isn't unique with multiple uploads - template_filename = template_id + ".tabula-template.json" - file_path = File.join(TabulaSettings::DOCUMENTS_BASEPATH, "..", "templates") - # write to file - FileUtils.mkdir_p(file_path) - open(File.join(file_path, template_filename), 'w'){|f| f << JSON.dump(template_info["template"])} - page_count = template_info.has_key?("page_count") ? template_info["page_count"] : template_info["template"].map{|f| f["page"]}.uniq.count - selection_count = template_info.has_key?("selection_count") ? template_info["selection_count"] : template_info["template"].count - Tabula::Workspace.instance.add_template({ - "id" => template_id, - "name" => template_name, - "page_count" => page_count, - "time" => Time.now.to_i, - "selection_count" => selection_count, - "template" => template_info["template"] - }) - return template_id -end - - - class InvalidTemplateError < StandardError; end TEMPLATE_REQUIRED_KEYS = ["page", "extraction_method", "x1", "x2", "y1", "y2", "width", "height"] def upload_template(template_file) @@ -163,7 +139,24 @@ def upload_template(template_file) # create a template from the GUI on post do - template_id = create_template(JSON.parse(req.params["model"])) + template_info = JSON.parse(req.params["model"]) + template_name = template_info["name"] || "Unnamed Template #{Time.now.to_s}" + template_id = Digest::SHA1.hexdigest(Time.now.to_s + template_name) # just SHA1 of time isn't unique with multiple uploads + template_filename = template_id + ".tabula-template.json" + file_path = File.join(TabulaSettings::DOCUMENTS_BASEPATH, "..", "templates") + # write to file + FileUtils.mkdir_p(file_path) + open(File.join(file_path, template_filename), 'w'){|f| f << JSON.dump(template_info["template"])} + page_count = template_info.has_key?("page_count") ? template_info["page_count"] : template_info["template"].map{|f| f["page"]}.uniq.count + selection_count = template_info.has_key?("selection_count") ? template_info["selection_count"] : template_info["template"].count + Tabula::Workspace.instance.add_template({ + "id" => template_id, + "name" => template_name, + "page_count" => page_count, + "time" => Time.now.to_i, + "selection_count" => selection_count, + "template" => template_info["template"] + }) res.status = 200 res['Content-Type'] = 'application/json' res.write(JSON.dump({template_id: template_id}))