class LibrariesController < ApplicationController before_action :get_library, except: [:index, :new, :create, :preview] skip_after_action :verify_policy_scoped, only: [:index] skip_before_action :set_up_first_library, only: [:new, :create] def index redirect_to new_library_path and return if Library.all.empty? # rubocop:disable Pundit/UsePolicyScope render layout: "settings" end def show redirect_to models_path(library: @library) end def new authorize Library @library = Library.new @title = t("libraries.general.new") end def edit end def create authorize Library @library = Library.create(library_params) @library.tag_regex = params[:tag_regex] if @library.valid? @library.detect_filesystem_changes_later @library.make_default if SiteSettings.default_library.nil? redirect_to @library, notice: t(".success") else flash.now[:alert] = t(".failure") render :new, status: :unprocessable_content end end def update @library.update(library_params) uptags = library_params[:tag_regex]&.reject(&:empty?) @library.tag_regex = uptags if @library.save @library.make_default if params.dig("library", "default") == "1" redirect_to settings_libraries_path, notice: t(".success") else flash.now[:alert] = t(".failure") render :edit, status: :unprocessable_content end end def destroy begin @library.destroy Library.first&.make_default if @library.default? rescue Shrine::Error # Not ideal, but file after_commit callbacks explode if the library has gone nil end redirect_to settings_libraries_path, notice: t(".success") end def preview # Create temporary library; we're not saving it @library = params[:id] ? Library.find_param(params[:id]) : Library.new(path: params[:path]) @library.path_template = params[:template] @library.parse_metadata_from_path = (params[:enabled] === "true") authorize @library render Components::PathTemplatePreview.new(library: @library), layout: nil end private def library_params params.expect(library: [ :path, # i18n-tasks-use t("activerecord.attributes.library.path") :create_path_if_not_on_disk, # i18n-tasks-use t("activerecord.attributes.library.create_path_if_not_on_disk") :name, # i18n-tasks-use t("activerecord.attributes.library.name") :notes, # i18n-tasks-use t("activerecord.attributes.library.notes") :caption, # i18n-tasks-use t("activerecord.attributes.library.caption") :icon, # i18n-tasks-use t("activerecord.attributes.library.icon") {tag_regex: []}, # i18n-tasks-use t("activerecord.attributes.library.tag_regex") :storage_service, # i18n-tasks-use t("activerecord.attributes.library.storage_service") :s3_endpoint, # i18n-tasks-use t("activerecord.attributes.library.s3_endpoint") :s3_bucket, # i18n-tasks-use t("activerecord.attributes.library.s3_bucket") :s3_region, # i18n-tasks-use t("activerecord.attributes.library.s3_region") :s3_access_key_id, # i18n-tasks-use t("activerecord.attributes.library.s3_access_key_id") :s3_secret_access_key, # i18n-tasks-use t("activerecord.attributes.library.s3_secret_access_key") :s3_path_style, # i18n-tasks-use t("activerecord.attributes.library.s3_path_style") :path_template, # i18n-tasks-use t("activerecord.attributes.library.path_template") :parse_metadata_from_path, # i18n-tasks-use t("activerecord.attributes.library.parse_metadata_from_path") :safe_folder_names # i18n-tasks-use t("activerecord.attributes.library.safe_folder_names") ]) end def get_library @library = Library.find_param(params[:id]) authorize @library @title = @library.name end end