files_controller.rb revision 2911e63768f8ef8e57e6a8ba85f8a0016b4a5c2e
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenclass FilesController < ApplicationController
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen helper_method :repository, :ref, :oid, :path, :branch_name
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen before_filter :check_permissions, only: [:new, :create]
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen def files
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen @info = repository.path_info(params[:path], oid)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen raise Repository::FileNotFoundError, path if @info.nil?
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen if request.format == 'text/html' || @info[:type] != :file
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen case @info[:type]
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen when :file
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen @file = repository.read_file(path, oid)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen when :file_base
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ontology = repository.ontologies.
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen where(basepath: File.basepath(@info[:entry][:path])).
190237ce467d2389dfb809874b0fec86d3c7968dTimo Sirainen order('id asc').first
b0be0bead3d6963149f7f2a9504b8ab5aced9af5Timo Sirainen redirect_to [repository, ontology]
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen end
41e1c7380edda701719d8ce1fb4d465d2ec4c84dTimo Sirainen else
8f7b00599e73fe71b1d2c6c65f8ae98aac1b23fbTimo Sirainen send_download(path, oid)
8f7b00599e73fe71b1d2c6c65f8ae98aac1b23fbTimo Sirainen end
8f7b00599e73fe71b1d2c6c65f8ae98aac1b23fbTimo Sirainen end
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen def download
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen send_download(path, oid)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen end
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen def entries_info
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen render json: repository.entries_info(oid, path)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen def diff
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen @message = repository.commit_message(oid)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen @changed_files = repository.changed_files(oid)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen def history
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen @per_page = 25
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen page = @page = params[:page].nil? ? 1 : params[:page].to_i
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen offset = page > 0 ? (page - 1) * @per_page : 0
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen @ontology = repository.primary_ontology(path)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if repository.empty?
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen @commits = []
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen else
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen @current_file = repository.read_file(path, oid) if path && !repository.dir?(path)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen @commits = repository.commits(start_oid: oid, path: path, offset: offset, limit: @per_page)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen def new
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen build_file
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen def create
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if build_file.valid?
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen repository.save_file @file.file.path, @file.filepath, @file.message, current_user
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen flash[:success] = "Successfully saved uploaded file."
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen redirect_to fancy_repository_path(repository, path: @file.path)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen else
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen render :new
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen protected
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen def repository
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen @repository ||= Repository.find_by_path!(params[:repository_id])
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen def ref
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen params[:ref] || 'master'
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen def build_file
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen @file ||= UploadFile.new(params[:upload_file])
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen def check_permissions
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen authorize! :write, repository
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen def send_download(path, oid)
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen render text: repository.read_file(path, oid)[:content],
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen content_type: Mime::Type.lookup('application/force-download')
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen def commit_id
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen @commit_id ||= repository.commit_id(params[:ref])
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen def oid
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen @oid ||= commit_id[:oid]
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen def branch_name
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen commit_id[:branch_name]
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen def path
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen params[:path]
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen end
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainenend
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen