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