class ImagesController < ApplicationController
  include ActiveStorage::SetCurrent
  before_action :verify_auth!, :set_blob
  protect_from_forgery with: :exception

  def show
    expires_in ActiveStorage.service_urls_expire_in
    redirect_to @blob.url(disposition: params[:disposition]), allow_other_host: true
  end

  def show_variant
    expires_in ActiveStorage.service_urls_expire_in
    variant = @blob.variant(thumbnail: "1600", quality: '65%', interlace: 'plane', auto_orient: true).processed
    redirect_to variant.url(disposition: params[:disposition]), allow_other_host: true
  end

  private
    def verify_auth!
      basic_auth_user = params[:u]
      credential = params[:c]
      if basic_auth_user.nil? || credential.nil?
        authenticate_user!
      else
        filename = params[:filename] + "." + params[:format]
        user = User.find_by(basic_auth_username: basic_auth_user)
        image_key = user.image_password
        hmac = OpenSSL::HMAC.hexdigest("SHA256", image_key, filename)
        unless hmac == credential
          raise ActiveRecord::RecordNotFound
        end
      end
    end

    def set_blob
      image = Image.find(params[:image_id])
      request_filename = params[:filename]
      @blob = image.blob_blob
      request_filename = params[:filename]
      blob_filename = @blob.filename.base
      unless (request_filename == blob_filename)
        @blob = nil
        head :not_found
      end
    rescue ActiveSupport::MessageVerifier::InvalidSignature
      head :not_found
    end
end
