<commit_before>require "base64"
require "open-uri"
require "uri"

def whyrun_supported?
  true
end

def manage_resource(new_resource)
  if new_resource.artifactory_username or new_resource.artifactory_password
    artifactory_headers = {
      "Authorization" => "Basic #{::Base64.encode64("#{new_resource.artifactory_username}:#{new_resource.artifactory_password}")}",
    }
  else
    artifactory_headers = {}
  end

  if new_resource.artifactory_url
    artifactory_url = ::URI.parse(new_resource.artifactory_url)
  else
    if new_resource.artifactoryonline
      artifactory_url = ::URI.parse("https://#{new_resource.artifactoryonline}.artifactoryonline.com/#{new_resource.artifactoryonline}/")
    else
      fail("Artifactory URL is not specified")
    end
  end

  repository_path = "#{new_resource.repository.sub(/\A\/+/, "")}/#{new_resource.repository_path}"
  artifact_url = ::URI.join("#{artifactory_url}/", repository_path.sub(/\A\/+/, ""))
  storage_url = ::URI.join("#{artifactory_url}/", "api/storage/#{repository_path}")

  # Retrieve Artifact's SHA256 checksum via Artifactory REST API
  # https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API#ArtifactoryRESTAPI-FileInfo
  artifact_sha256sum = nil
  begin
    storage_url.open(artifactory_headers) do |response|
      code, reason = response.status
      if code.to_i == 200
        data = ::JSON.parse(response.read)
        if data["checksums"] and data["checksums"]["sha256"]
          sha256sum = data["checksums"]["sha256"]
        end
      else
        fail("Artifactory REST API error: #{storage_url}: #{code} #{reason}")
      end
    end
  rescue => error
    ::Chef::Log.warn(error)
  end

  directory ::File.dirname(new_resource.name) do
    recursive true
  end

  remote_file new_resource.name do
    backup false
    checksum artifact_sha256sum
    headers artifactory_headers
    source artifact_url.to_s
    action new_resource.action
  end
end

action :create do
  manage_resource(new_resource)
end

action :create_if_missing do
  manage_resource(new_resource)
end

action :delete do
  manage_resource(new_resource)
end<commit_msg>Fix `FC017: LWRP does not notify when updated`<commit_after>require "base64"
require "open-uri"
require "uri"

def whyrun_supported?
  true
end

def manage_resource(new_resource)
  if new_resource.artifactory_username or new_resource.artifactory_password
    artifactory_headers = {
      "Authorization" => "Basic #{::Base64.encode64("#{new_resource.artifactory_username}:#{new_resource.artifactory_password}")}",
    }
  else
    artifactory_headers = {}
  end

  if new_resource.artifactory_url
    artifactory_url = ::URI.parse(new_resource.artifactory_url)
  else
    if new_resource.artifactoryonline
      artifactory_url = ::URI.parse("https://#{new_resource.artifactoryonline}.artifactoryonline.com/#{new_resource.artifactoryonline}/")
    else
      fail("Artifactory URL is not specified")
    end
  end

  repository_path = "#{new_resource.repository.sub(/\A\/+/, "")}/#{new_resource.repository_path}"
  artifact_url = ::URI.join("#{artifactory_url}/", repository_path.sub(/\A\/+/, ""))
  storage_url = ::URI.join("#{artifactory_url}/", "api/storage/#{repository_path}")

  # Retrieve Artifact's SHA256 checksum via Artifactory REST API
  # https://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API#ArtifactoryRESTAPI-FileInfo
  artifact_sha256sum = nil
  begin
    storage_url.open(artifactory_headers) do |response|
      code, reason = response.status
      if code.to_i == 200
        data = ::JSON.parse(response.read)
        if data["checksums"] and data["checksums"]["sha256"]
          sha256sum = data["checksums"]["sha256"]
        end
      else
        fail("Artifactory REST API error: #{storage_url}: #{code} #{reason}")
      end
    end
  rescue => error
    ::Chef::Log.warn(error)
  end

  directory ::File.dirname(new_resource.name) do
    recursive true
  end

  remote_file new_resource.name do
    backup false
    checksum artifact_sha256sum
    headers artifactory_headers
    source artifact_url.to_s
    action new_resource.action
  end
end

action :create do
  manage_resource(new_resource)
  new_resource.updated_by_last_action(true)
end

action :create_if_missing do
  manage_resource(new_resource)
  new_resource.updated_by_last_action(true)
end

action :delete do
  manage_resource(new_resource)
  new_resource.updated_by_last_action(true)
end
{'fraction_non_alphanumeric': 0.11570621468926554, 'fraction_numerical': 0.01288135593220339, 'mean_word_length': 4.2440758293838865, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '2808017', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1447, 'n_words': 267}