files_controller.rb revision c28fc8aa2cdf7f9c9ae76899354350473d155981
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenclass FilesController < InheritedResources::Base
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen defaults resource_class: RepositoryFile
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen defaults singleton: true
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen helper_method :repository, :ref, :oid, :path, :branch_name
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen before_filter :check_write_permissions, only: [:new, :create, :update]
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen before_filter :check_read_permissions
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen def show
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen if owl_api_header_in_accept_header?
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen send_download(path, oid)
fb08a91e3f2949ecefb647fa38206ca9aad5307fTimo Sirainen elsif existing_file_requested_as_html?
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen # TODO: the query_string check should be done in the iri router
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen if request.query_string.present?
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen ontology = resource.ontologies.first.children.where(name: request.query_string).first
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen redirect_to [repository, ontology]
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen else
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen send_download(path, oid)
d0720f3037064af4b92eccfc20a8814adcacf827Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen
b2ed2b25c4c457ec1c99ebe5e9bd66a2e2f89cfdTimo Sirainen def download
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen send_download(path, oid)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
cdc8485491045d82bb98405d4b995f277d12838eTimo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen def entries_info
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen render json: repository.entries_info(oid, path)
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen def new
c0e5c6a86e1de5d4f5591d39b4aa921a23c807d7Timo Sirainen @repository_file = resource_class.build(params.merge(user: current_user))
c0e5c6a86e1de5d4f5591d39b4aa921a23c807d7Timo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen def create
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen @repository_file = resource_class.create(params.merge(user: current_user))
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen if resource.valid?
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen flash[:success] = "Successfully saved the uploaded file."
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen if ontology = repository.ontologies.with_path(resource.target_path).without_parent.first
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen redirect_to edit_repository_ontology_path(repository, ontology)
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen else
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen redirect_to fancy_repository_path(repository, path: resource.target_path)
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen else
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen render :new
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen def update
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen @repository_file = resource_class.create(params.merge(user: current_user))
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen if resource.valid?
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen flash[:success] = "Successfully changed the file."
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen redirect_to fancy_repository_path(repository, path: resource.target_path)
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen else
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen render :show
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen protected
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen def resource
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen @repository_file ||= resource_class.find_with_path(params)
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen def repository
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen @repository ||= Repository.active.find_by_path!(params[:repository_id])
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen def ref
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen params[:ref] || 'master'
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen def check_read_permissions
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen authorize! :show, repository
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen def check_write_permissions
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen authorize! :write, repository
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen
c0e5c6a86e1de5d4f5591d39b4aa921a23c807d7Timo Sirainen def send_download(path, oid)
c0e5c6a86e1de5d4f5591d39b4aa921a23c807d7Timo Sirainen render text: repository.get_file(path, oid).content,
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen content_type: Mime::Type.lookup('application/force-download')
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen def commit_id
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen @commit_id ||= repository.commit_id(params[:ref])
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen def oid
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen @oid ||= commit_id[:oid] unless commit_id.nil?
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen end
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen
d8552f9f65e5ff64be5de9faf9a8171799a0bbecTimo Sirainen def branch_name
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen commit_id[:branch_name]
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen def path
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen params[:path]
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen def owl_api_header_in_accept_header?
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen # OWL API sends those two http accept headers in different requests:
55bc6a7a0940ec48a68558ef70838991c5d301d2Timo Sirainen # application/rdf+xml, application/xml; q=0.5, text/xml; q=0.3, */*; q=0.2
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen # text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen # The latter conflicts with what browsers send.
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen accepts = request.accepts.compact
55bc6a7a0940ec48a68558ef70838991c5d301d2Timo Sirainen (accepts.present? && accepts.first != Mime::HTML) ||
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen accepts[0..2] == [Mime::HTML, Mime::GIF, Mime::JPEG]
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
55bc6a7a0940ec48a68558ef70838991c5d301d2Timo Sirainen def existing_file_requested_as_html?
55bc6a7a0940ec48a68558ef70838991c5d301d2Timo Sirainen request.accepts.first == Mime::HTML || !resource.file?
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenend
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen