app/controllers/docs_controller.rb
# add new docs to a project
add new docs to a project
add new docs to a project
def add_from_upload
		begin
			project = Project.editable(current_user).find_by_name(params[:project_id])
			raise "The project does not exist, or you are not authorized to make a change to the project.\n" unless project.present?
			raise ArgumentError, "sourcedb is not specified." unless params["sourcedb"].present?
			sourcedb = params["sourcedb"]
			filename = params[:upfile].original_filename
			ext = File.extname(filename)
			raise ArgumentError, "Unknown file type: '#{ext}'." unless ['.txt'].include?(ext)
			raise "Up to 10 jobs can be registered per a project. Please clean your jobs page." unless project.jobs.count < 10
			filepath = File.join('tmp', "add-docs-to-#{params[:project_id]}-#{Time.now.to_s[0..18].gsub(/[ :]/, '-')}#{ext}")
			FileUtils.mv params[:upfile].path, filepath
			active_job = AddDocsToProjectFromUploadJob.perform_later(project, sourcedb, filepath)
			job = Job.find_by(active_job_id: active_job.job_id)
			message = "The task, '#{active_job.job_name}', is created."
			respond_to do |format|
				format.html {redirect_back fallback_location: root_path, notice: message }
				format.json {render json: {message: message, task_location: project_job_url(project.name, job.id, format: :json)}, status: :ok}
			end
		rescue => e
			respond_to do |format|
				format.html {redirect_back fallback_location: root_path, notice: e.message }
				format.json {render json: {message: e.message}, status: :unprocessable_entity}
			end
		end
	end
def add_from_upload begin project = Project . editable ( current_user ) . find_by_name ( params [ :project_id ] ) raise "The project does not exist, or you are not authorized to make a change to the project.\n" unless project . present? raise ArgumentError , "sourcedb is not specified." unless params [ "sourcedb" ] . present? sourcedb = params [ "sourcedb" ] filename = params [ :upfile ] . original_filename ext = File . extname ( filename ) raise ArgumentError , "Unknown file type: '#{ext}'." unless [ '.txt' ] . include? ( ext ) raise "Up to 10 jobs can be registered per a project. Please clean your jobs page." unless project . jobs . count < 10 filepath = File . join ( 'tmp' , "add-docs-to-#{params[:project_id]}-#{Time.now.to_s[0..18].gsub(/[ :]/, '-')}#{ext}" ) FileUtils . mv params [ :upfile ] . path , filepath active_job = AddDocsToProjectFromUploadJob . perform_later ( project , sourcedb , filepath ) job = Job . find_by ( active_job_id : active_job . job_id ) message = "The task, '#{active_job.job_name}', is created." respond_to do | format | format . html { redirect_back fallback_location : root_path , notice : message } format . json { render json : { message : message , task_location : project_job_url ( project . name , job . id , format : :json ) } , status : :ok } end rescue => e respond_to do | format | format . html { redirect_back fallback_location : root_path , notice : e . message } format . json { render json : { message : e . message } , status : :unprocessable_entity } end end end
add new docs to a project
add new docs to a project
# AddDocsToProjectFromUploadJob.perform_now(project, sourcedb, filepath)