<commit_before>class Publishers::SiteBannersController < ApplicationController
  include ImageConversionHelper
  before_action :authenticate_publisher!

  MAX_IMAGE_SIZE = 10_000_000

  def new
    @site_banner = current_publisher.site_banner || SiteBanner.new
  end

  def create
    site_banner = current_publisher.site_banner || SiteBanner.new
    donation_amounts = JSON.parse(params[:donation_amounts])
    site_banner.update(
      publisher_id: current_publisher.id,
      title: params[:title],
      donation_amounts: donation_amounts,
      default_donation: donation_amounts.second,
      social_links: params[:social_links].present? ? JSON.parse(params[:social_links]) : {},
      description: params[:description]
    )
    head :ok
  end

  def fetch
    if current_publisher.site_banner.present?
      data = current_publisher.site_banner.read_only_react_property
      data[:backgroundImage] = data[:backgroundUrl]
      data[:logoImage] = data[:logoUrl]
      render(json: data.to_json)
    else
      render(json: {}.to_json)
    end
  end

  def update_logo
    if params[:image].length > MAX_IMAGE_SIZE
      # (<NAME>): We should consider supporting alerts. This might require a UI redesign
      # alert[:error] = "File size too big!"
      head :payload_too_large and return
    end
    site_banner = current_publisher.site_banner
    update_image(attachment: site_banner.logo, attachment_type: SiteBanner::LOGO)
    head :ok
  end

  def update_background_image
    if params[:image].length > MAX_IMAGE_SIZE
      # (<NAME>): We should consider supporting alerts. This might require a UI redesign
      # alert[:error] = "File size too big!"
      head :payload_too_large and return
    end
    site_banner = current_publisher.site_banner
    update_image(attachment: site_banner.background_image, attachment_type: SiteBanner::BACKGROUND)
    head :ok
  end

  private

  def update_image(attachment:, attachment_type:)
    data_url = params[:image].split(',')[0]
    if data_url.starts_with?("data:image/jpeg") || data_url.starts_with?("data:image/jpg")
      extension = ".jpg"
    elsif data_url.starts_with?("data:image/png")
      extension = ".png"
    elsif data_url.starts_with?("data:image/bmp")
      extension = ".bmp"
    else<commit_msg>Prevent users from adding site banners if they're not in the whitelist<commit_after>class Publishers::SiteBannersController < ApplicationController
  include ImageConversionHelper
  before_action :authenticate_publisher!

  MAX_IMAGE_SIZE = 10_000_000

  def new
    @site_banner = current_publisher.site_banner || SiteBanner.new
  end

  def create
    head 401 unless current_publisher.in_brave_rewards_whitelist?
    site_banner = current_publisher.site_banner || SiteBanner.new
    donation_amounts = JSON.parse(params[:donation_amounts])
    site_banner.update(
      publisher_id: current_publisher.id,
      title: params[:title],
      donation_amounts: donation_amounts,
      default_donation: donation_amounts.second,
      social_links: params[:social_links].present? ? JSON.parse(params[:social_links]) : {},
      description: params[:description]
    )
    head :ok
  end

  def fetch
    if current_publisher.site_banner.present?
      data = current_publisher.site_banner.read_only_react_property
      data[:backgroundImage] = data[:backgroundUrl]
      data[:logoImage] = data[:logoUrl]
      render(json: data.to_json)
    else
      render(json: {}.to_json)
    end
  end

  def update_logo
    head 401 unless current_publisher.in_brave_rewards_whitelist?
    if params[:image].length > MAX_IMAGE_SIZE
      # (<NAME>): We should consider supporting alerts. This might require a UI redesign
      # alert[:error] = "File size too big!"
      head :payload_too_large and return
    end
    site_banner = current_publisher.site_banner
    update_image(attachment: site_banner.logo, attachment_type: SiteBanner::LOGO)
    head :ok
  end

  def update_background_image
    head 401 unless current_publisher.in_brave_rewards_whitelist?
    if params[:image].length > MAX_IMAGE_SIZE
      # (<NAME>): We should consider supporting alerts. This might require a UI redesign
      # alert[:error] = "File size too big!"
      head :payload_too_large and return
    end
    site_banner = current_publisher.site_banner
    update_image(attachment: site_banner.background_image, attachment_type: SiteBanner::BACKGROUND)
    head :ok
  end

  private

  def update_image(attachment:, attachment_type:)
    data_url = params[:image].split(',')[0]
    if data_url.starts_with?("data:image/jpeg") || data_url.starts_with?("data:image/jpg")
      extension = ".jpg"
    elsif data_url.starts_with?("data:image/png")
      extension = ".png"
    elsif data_url.starts_with?("data:image/bmp")
      extension = ".bmp"
    else
{'fraction_non_alphanumeric': 0.09389964605454924, 'fraction_numerical': 0.005621486570893191, 'mean_word_length': 3.947476828012358, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1366391', 'n_tokens_mistral': 1551, 'n_tokens_neox': 1487, 'n_words': 318}