files_controller.rb revision 21429036acbcc2f51ef24fc38b78c85a3ab2f6c3
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherclass FilesController < ApplicationController
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher helper_method :repository, :ref
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher before_filter :check_permissions, only: [:new, :create]
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher # FIXME
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher #load_and_authorize_resource :except => [:index, :show]
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def files
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher commit_id = repository.commit_id(params[:ref])
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @oid = commit_id[:oid]
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @branch_name = commit_id[:branch_name]
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @path = params[:path]
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @info = repository.path_info(params[:path], @oid)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher raise Repository::FileNotFoundError, params[:path] if @info.nil?
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if request.format == 'text/html' || @info[:type] != :file
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher case @info[:type]
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher when :file
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @file = repository.read_file(@path, @oid)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher when :file_base
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher ontologies = repository.ontologies.
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher where(basepath: File.basepath(@info[:entry][:path])).
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher order('id asc')
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher redirect_to [repository, ontologies.first]
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher else
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher render text: repository.read_file(@path, @oid)[:content],
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher content_type: Mime::Type.lookup('application/force-download')
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
4e2d9fe30bf8b692972a9654c60d2d90ed355815Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def entries_info
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher render json: repository.entries_info(@oid, params[:path])
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def diff
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @oid = repository.commit_id(params[:ref])[:oid]
f6cd1236c27817b97db002094b76648d92b55f82Jan Zeleny @message = repository.commit_message(@oid)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @changed_files = repository.changed_files(@oid)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def history
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @path = params[:path]
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if repository.empty?
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @commits = []
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher else
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @oid = repository.commit_id(params[:ref])[:oid]
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @current_file = repository.read_file(@path, @oid) if @path
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @commits = repository.commits(start_oid: @oid, path: @path)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def new
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher build_file
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def create
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if build_file.valid?
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher repository.save_file @file.file.path, @file.filepath, @file.message, current_user
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher flash[:success] = "Successfully saved uploaded file."
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher redirect_to fancy_repository_path(repository, path: @file.path)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher else
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher render :new
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher protected
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def repository
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @repository ||= Repository.find_by_path!(params[:repository_id])
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def ref
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if params[:ref]
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher params[:ref]
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher else
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher 'master'
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def build_file
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @file ||= UploadFile.new(params[:upload_file])
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
e82832a64fd456d1541ce0ea3902bcfb05e69642Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def check_permissions
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher authorize! :write, repository
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher end
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagherend
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher