Sha256: 7ce1389be7fdf8a3c074a13290c5f2281a85197f20efa4da1a5c10a0f6656807

Contents?: true

Size: 923 Bytes

Versions: 1

Compression:

Stored size: 923 Bytes

Contents

# frozen_string_literal: true

module Kitkat
  # File-level operations.
  class FileInfo
    BLANK               = ''
    MIME_TYPE_SEPARATOR = '/'

    attr_reader :path, :root

    def initialize(path, root: BLANK)
      @path = path
      @root = root
    end

    def relative_path
      path.delete_prefix(root).delete_prefix(File::SEPARATOR)
    end

    def full_mime_type
      @full_mime_type ||= IO.popen(
        ['file', '--brief', '--mime-type', path],
        in: :close, err: :close
      ) { |io| io.read.chomp }.to_s
    end

    def mime_type
      full_mime_type.split(MIME_TYPE_SEPARATOR).first
    end

    def mime_subtype
      full_mime_type.split(MIME_TYPE_SEPARATOR).last
    end

    def digest
      File.directory?(path) ? BLANK : Digest::SHA256.file(path).hexdigest
    end

    def last_modified_at
      File.mtime(path).utc
    end

    def bytesize
      File.size(path)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kitkat-0.0.1 lib/kitkat/file_info.rb