files_controller.rb revision 46c38bae0b97f6c470beb0c897ad6c530c4c9e0e
204N/Aclass FilesController < InheritedResources::Base
204N/A defaults resource_class: RepositoryFile
204N/A defaults singleton: true
204N/A
204N/A helper_method :repository, :ref, :oid, :path, :branch_name
204N/A before_filter :check_write_permissions, only: [:new, :create, :update]
204N/A before_filter :check_read_permissions
204N/A
204N/A def show
204N/A if owl_api_header_in_accept_header?
204N/A send_download(path, oid)
204N/A elsif existing_file_requested_as_html?
204N/A # TODO: the query_string check should be done in the iri router
204N/A if request.query_string.present?
204N/A ontology = resource.ontologies.first.children.where(name: request.query_string).first
204N/A redirect_to [repository, ontology]
204N/A end
204N/A else
204N/A send_download(path, oid)
204N/A end
204N/A end
204N/A
204N/A def download
204N/A send_download(path, oid)
204N/A end
204N/A
204N/A def entries_info
204N/A render json: repository.entries_info(oid, path)
204N/A end
204N/A
204N/A def diff
204N/A @message = repository.commit_message(oid)
204N/A @changed_files = repository.changed_files(oid)
204N/A end
204N/A
204N/A def history
204N/A @per_page = 25
204N/A page = @page = params[:page].nil? ? 1 : params[:page].to_i
204N/A offset = page > 0 ? (page - 1) * @per_page : 0
204N/A
204N/A @ontology = repository.primary_ontology(path)
204N/A
204N/A if repository.empty?
204N/A @commits = []
204N/A else
204N/A @current_file = repository.get_file(path, oid) if path && !repository.dir?(path)
204N/A @commits = repository.commits(start_oid: oid, path: path, offset: offset, limit: @per_page)
204N/A end
204N/A end
204N/A
204N/A def new
204N/A @repository_file = resource_class.build(params.merge(user: current_user))
204N/A end
204N/A
204N/A def create
204N/A @repository_file = resource_class.create(params.merge(user: current_user))
204N/A if resource.valid?
204N/A flash[:success] = "Successfully saved the uploaded file."
204N/A if ontology = repository.ontologies.find_by_file(resource.target_path)
204N/A redirect_to edit_repository_ontology_path(repository, ontology)
204N/A else
204N/A redirect_to fancy_repository_path(repository, path: resource.target_path)
204N/A end
204N/A else
204N/A render :new
204N/A end
204N/A end
204N/A
204N/A def update
204N/A @repository_file = resource_class.create(params.merge(user: current_user))
204N/A if resource.valid?
204N/A flash[:success] = "Successfully changed the file."
204N/A redirect_to fancy_repository_path(repository, path: resource.target_path)
204N/A else
204N/A render :show
204N/A end
204N/A end
204N/A
204N/A protected
204N/A
204N/A def resource
204N/A @repository_file ||= resource_class.find_with_path(params)
204N/A end
204N/A
204N/A def repository
204N/A @repository ||= Repository.find_by_path!(params[:repository_id])
204N/A end
204N/A
204N/A def ref
204N/A params[:ref] || 'master'
204N/A end
204N/A
204N/A def check_read_permissions
204N/A authorize! :show, repository
204N/A end
204N/A
204N/A def check_write_permissions
204N/A authorize! :write, repository
204N/A end
204N/A
204N/A def send_download(path, oid)
204N/A render text: repository.get_file(path, oid).content,
204N/A content_type: Mime::Type.lookup('application/force-download')
204N/A end
204N/A
204N/A def commit_id
204N/A @commit_id ||= repository.commit_id(params[:ref])
204N/A end
204N/A
204N/A def oid
204N/A @oid ||= commit_id[:oid] unless commit_id.nil?
204N/A end
204N/A
204N/A def branch_name
204N/A commit_id[:branch_name]
204N/A end
204N/A
204N/A def path
204N/A params[:path]
204N/A end
204N/A
204N/A def owl_api_header_in_accept_header?
204N/A # OWL API sends those two http accept headers in different requests:
204N/A # application/rdf+xml, application/xml; q=0.5, text/xml; q=0.3, */*; q=0.2
204N/A # text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
204N/A # The latter conflicts with what browsers send.
204N/A accepts = request.accepts.compact
204N/A (accepts.present? && accepts.first != Mime::HTML) ||
204N/A accepts[0..2] == [Mime::HTML, Mime::GIF, Mime::JPEG]
204N/A end
204N/A
204N/A def existing_file_requested_as_html?
204N/A request.accepts.first == Mime::HTML || !resource.file?
204N/A end
204N/A
204N/Aend
204N/A