<commit_before># Controller to handle uploads

class UploadController < ApplicationController

  before_filter :require_login

  def create_model
    
    if params[:new_model].blank? or params[:new_model][:name].blank? or params[:new_model][:uploaded_body].blank?
      respond_to do |format|
        format.html do 
          flash[:notice] = "Sorry, but you must enter a model name and file."
          redirect_to :action => :new_model
        end
        format.json do
          render :json => {:status => 'MISSING_PARAMETERS'}
        end
      end
      
      return
    end

    model_name = params[:new_model][:name]

    Node.transaction do

      # ------------------------------------------------------------
      # Node and version for the model
      # ------------------------------------------------------------

      # Create a new node, without a parent
      @model = Node.new(:parent_id => nil,
                        :name => model_name)

      if !@model.save
        respond_to do |format|
          format.html do 
            logger.warn "Error(s): @model.errors"
            flash[:notice] = "Error creating a new model object; it was not saved."
            redirect_to :back
          end
          format.json do 
            render :json => {:status => 'MODEL_NOT_SAVED'}
          end
        end
        return
      end

      # Create a new version for that node, and stick the contents in there
      
      params[:new_model][:uploaded_body].rewind
      node_version_contents = params[:new_model][:uploaded_body].read

      new_version =
        Version.new(:node_id => @model.id,
                    :person_id => @person.id,
                    :contents => node_version_contents,
                    :description => 'Initial upload')
      
      begin
        new_version.save!
        flash[:notice] = "Thanks for uploading the new model called '#{model_name}'."
        
        # If we got a group and permission settings, set those as well
        group_id = params[:group_id].blank? ? nil : params[:group_id]
        group = Group.group_or_nil(group_id)
        
        read_permission = PermissionSetting.find_by_short_form(params[:read_permission])
        if group.nil? and read_permission and read_permission.is_group?
          read_permission = PermissionSetting.find_by_short_form('u')
        end
        
        write_permission = PermissionSetting.find_by_short_form(params[:write_permission])
        if group.nil? and write_permission and write_permission.is_group?
          write_permission = PermissionSetting.find_by_short_form('u')
        end
        
        @model.update_attributes!(:visibility => read_permission,
                                  :changeability => write_permission,
                                  :group => group)
        
        # ------------------------------------------------------------
        # Preview image
        # ------------------------------------------------------------
        
        if params[:new_model][:uploaded_preview].present?
          
          # Create a preview
          params[:new_model][:uploaded_preview].rewind
          attachment = Attachment.new(:node_id => @model.id,
                                      :person_id => @person.id,
                                      :description => "Preview for '#{model_name}'",
                                      :filename => model_name + '.png',
                                      :content_type => 'preview',
                                      :contents => params[:new_model][:uploaded_preview].read)
          
          expire_page :action => :display_preview, :id => @model.id<commit_msg>Fix upload problems (I hope)<commit_after># Controller to handle uploads

class UploadController < ApplicationController

  before_filter :require_login

  def create_model
    
    if params[:new_model].blank? or params[:new_model][:name].blank? or params[:new_model][:uploaded_body].blank?
      respond_to do |format|
        format.html do 
          flash[:notice] = "Sorry, but you must enter a model name and file."
          redirect_to :action => :new_model
        end
        format.json do
          render :json => {:status => 'MISSING_PARAMETERS'}
        end
      end
      
      return
    end

    model_name = params[:new_model][:name]

    Node.transaction do

      # ------------------------------------------------------------
      # Node and version for the model
      # ------------------------------------------------------------

      # Create a new node, without a parent
      @model = Node.new(:parent_id => nil,
                        :name => model_name)

      if !@model.save
        respond_to do |format|
          format.html do 
            logger.warn "Error(s): @model.errors"
            flash[:notice] = "Error creating a new model object; it was not saved."
            redirect_to :back
          end
          format.json do 
            render :json => {:status => 'MODEL_NOT_SAVED'}
          end
        end
        return
      end

      # Create a new version for that node, and stick the contents in there
      
      params[:new_model][:uploaded_body].rewind
      node_version_contents = params[:new_model][:uploaded_body].read

      new_version =
        Version.new(:node_id => @model.id,
                    :person_id => @person.id,
                    :contents => node_version_contents,
                    :description => 'Initial upload')
      
      begin
        new_version.save!
        flash[:notice] = "Thanks for uploading the new model called '#{model_name}'."
        
        # If we got a group and permission settings, set those as well
        group_id = params[:group_id].blank? ? nil : params[:group_id]
        group = Group.group_or_nil(group_id)
        
        read_permission = PermissionSetting.find_by_short_form(params[:read_permission])
        if group.nil? and read_permission and read_permission.is_group?
          read_permission = PermissionSetting.find_by_short_form('u')
        end
        
        write_permission = PermissionSetting.find_by_short_form(params[:write_permission])
        if group.nil? and write_permission and write_permission.is_group?
          write_permission = PermissionSetting.find_by_short_form('u')
        end
        
        @model.update_attributes!(:visibility => read_permission,
                                  :changeability => write_permission,
                                  :group => group)
        
        # ------------------------------------------------------------
        # Preview image
        # ------------------------------------------------------------
        
        params[:new_model][:uploaded_preview].rewind
        preview_body = params[:new_model][:uploaded_preview].read

        if preview_body.present?
          
          # Create a preview
          params[:new_model][:uploaded_preview].rewind
          attachment = Attachment.new(:node_id => @model.id,
                                      :person_id => @person.id,
                                      :description => "Preview for '#{model_name}'",
                                      :filename => model_name + '.png',
                                      :content_type => 'preview',
                                      :contents => preview_body)
          
          expire_page :action => :display_preview, :id => @model.id
{'fraction_non_alphanumeric': 0.14965072541644278, 'fraction_numerical': 0.0, 'mean_word_length': 1.482494164721574, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '912733', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1798, 'n_words': 438}