files_controller.rb revision 556d9e007120cfbfbb79070544b4928f26bfa0eb
429N/Aclass FilesController < ApplicationController
429N/A
429N/A helper_method :repository, :ref, :oid, :path, :branch_name
429N/A before_filter :check_write_permissions, only: [:new, :create, :update]
429N/A before_filter :check_read_permissions
429N/A
429N/A def files
429N/A @info = repository.path_info(params[:path], oid)
429N/A
429N/A raise Repository::FileNotFoundError, path if @info.nil?
429N/A
429N/A if owl_api_header_in_accept_header?
429N/A send_download(path, oid)
429N/A elsif existing_file_requested_as_html?
429N/A case @info[:type]
429N/A when :file
429N/A @file = repository.read_file(path, oid)
429N/A when :file_base
429N/A ontology = repository.ontologies.with_basepath(
429N/A File.basepath(@info[:entry][:path])).
429N/A order('id asc').first
941N/A if request.query_string.present?
941N/A ontology = ontology.children.
941N/A where(name: request.query_string).first
429N/A end
941N/A redirect_to [repository, ontology]
429N/A end
429N/A else
429N/A send_download(path, oid)
429N/A end
516N/A end
546N/A
429N/A def download
429N/A send_download(path, oid)
429N/A end
429N/A
429N/A def entries_info
941N/A render json: repository.entries_info(oid, path)
941N/A end
941N/A
546N/A def diff
941N/A @message = repository.commit_message(oid)
546N/A @changed_files = repository.changed_files(oid)
546N/A end
546N/A
516N/A def history
429N/A @per_page = 25
429N/A page = @page = params[:page].nil? ? 1 : params[:page].to_i
429N/A offset = page > 0 ? (page - 1) * @per_page : 0
429N/A
429N/A @ontology = repository.primary_ontology(path)
429N/A
429N/A if repository.empty?
429N/A @commits = []
429N/A else
429N/A @current_file = repository.read_file(path, oid) if path && !repository.dir?(path)
429N/A @commits = repository.commits(start_oid: oid, path: path, offset: offset, limit: @per_page)
429N/A end
429N/A end
429N/A
441N/A def new
441N/A build_file
441N/A end
441N/A
441N/A def create
429N/A if build_file.valid?
429N/A repository.save_file @file.file.path, @file.filepath, @file.message, current_user
429N/A flash[:success] = "Successfully saved the uploaded file."
429N/A if ontology = repository.ontologies.with_path(@file.filepath).without_parent.first
429N/A redirect_to edit_repository_ontology_path(repository, ontology)
429N/A else
429N/A redirect_to fancy_repository_path(repository, path: @file.filepath)
429N/A end
429N/A else
429N/A render :new
429N/A end
429N/A end
429N/A
429N/A def update
429N/A if update_file.valid?
429N/A repository.save_file @file_changed.file.path, @file_changed.filepath, @file_changed.message, current_user
429N/A FileUtils.rm_rf(@file_changed.file.path)
429N/A flash[:success] = "Successfully changed the file."
429N/A redirect_to fancy_repository_path(repository, path: @file_changed.filepath)
429N/A else
429N/A @info = repository.path_info(params[:path], oid)
441N/A @file = repository.read_file(path, oid)
429N/A
546N/A @info[:file][:content] = params[:content]
429N/A @file[:content] = params[:content]
429N/A
429N/A flash[:error] = @file_changed.errors
429N/A
429N/A render :files
429N/A end
429N/A end
429N/A
429N/A protected
429N/A
429N/A def repository
429N/A @repository ||= Repository.find_by_path!(params[:repository_id])
429N/A end
429N/A
441N/A def ref
429N/A params[:ref] || 'master'
429N/A end
429N/A
429N/A def build_file
429N/A args = params[:upload_file].merge({repository: repository}) unless params[:upload_file].nil?
429N/A @file ||= UploadFile.new(args)
429N/A end
429N/A
429N/A def update_file
429N/A filepath = Rails.root.join('tmp', 'files', oid, "#{Time.now.nsec}_#{params[:path].split('/')[-1]}")
441N/A FileUtils.mkdir_p(filepath.split[0])
429N/A
429N/A tmp_file = File.open(filepath, 'w+') do |f|
429N/A f.write(params[:content])
429N/A end
429N/A
429N/A @file_changed = UploadFile.new(
429N/A target_directory: params[:path].split('/')[0..-2].join('/'),
429N/A target_filename: params[:path].split('/')[-1],
429N/A message: params[:message],
429N/A repository: repository,
429N/A file: File.new(filepath))
429N/A end
429N/A
429N/A def check_read_permissions
429N/A authorize! :show, repository
429N/A end
429N/A
429N/A def check_write_permissions
429N/A authorize! :write, repository
429N/A end
429N/A
429N/A def send_download(path, oid)
429N/A render text: repository.read_file(path, oid)[:content],
429N/A content_type: Mime::Type.lookup('application/force-download')
429N/A end
429N/A
429N/A def commit_id
429N/A @commit_id ||= repository.commit_id(params[:ref])
429N/A end
429N/A
429N/A def oid
429N/A @oid ||= commit_id[:oid] unless commit_id.nil?
429N/A end
429N/A
429N/A def branch_name
429N/A commit_id[:branch_name]
429N/A end
429N/A
429N/A def path
429N/A params[:path]
429N/A end
429N/A
941N/A def owl_api_header_in_accept_header?
941N/A # OWL API sends those two http accept headers in different requests:
941N/A # application/rdf+xml, application/xml; q=0.5, text/xml; q=0.3, */*; q=0.2
429N/A # text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
429N/A # The latter conflicts with what browsers send.
429N/A accepts = request.accepts.compact
429N/A (accepts.present? && accepts.first != Mime::HTML) ||
429N/A accepts[0..2] == [Mime::HTML, Mime::GIF, Mime::JPEG]
429N/A end
429N/A
429N/A def existing_file_requested_as_html?
429N/A request.accepts.first == Mime::HTML || @info[:type] != :file
429N/A end
429N/A
429N/Aend
429N/A