aboutsummaryrefslogtreecommitdiff
path: root/src/guff/theme/installer.cr
blob: a5461565a7baef587fcfcab08fd48ae90b50e424 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
require "file_utils"

class Guff::Theme::Installer
  def self.run(context : Context, zip_path : String)
    new(context).run(zip_path)
  end

  def initialize(@context : Context)
  end

  def run(zip_path : String)
    # parse manifest
    manifest = Manifest.load(zip_path)

    # add theme to db
    @context.models.theme.add(manifest, hash_file(zip_path)) do |theme_id|
      # copy theme to themes dir
      FileUtils.cp(zip_path, File.join(themes_dir, theme_id.to_s))
    end
  end

  private def themes_dir
    File.join(@context.config.data_dir, "themes")
  end

  private def hash_file(path : String)
    d = OpenSSL::Digest.new("SHA1")
    d.file(path)
    d.hexdigest
  end
end