class Staticd::Datastores::Local

Datastore storing files on local directory.

It use the file SHA1 digest as a filename so two identical files are not stored twice.

Example:

datastore = Staticd::Datastores::Local.new(path: "/tmp/datastore")
datastore.put(file_path) unless datastore.exist?(file_path)
# => "/tmp/datastore/sha1_digest"

Public Class Methods

new(params) click to toggle source
# File lib/staticd/datastores/local.rb, line 17
def initialize(params)
  @path = params[:path]
  check_store_directory
end

Public Instance Methods

exist?(file_path) click to toggle source
# File lib/staticd/datastores/local.rb, line 28
def exist?(file_path)
  datastore_file = stored_file_path(file_path)
  File.exist?(datastore_file) ? datastore_file : false
end
put(file_path) click to toggle source
# File lib/staticd/datastores/local.rb, line 22
def put(file_path)
  datastore_file = stored_file_path(file_path)
  FileUtils.copy_file(file_path, datastore_file) unless exist?(file_path)
  datastore_file
end

Private Instance Methods

check_store_directory() click to toggle source
# File lib/staticd/datastores/local.rb, line 35
def check_store_directory
  FileUtils.mkdir_p(@path) unless File.directory?(@path)
end
sha1(file_path) click to toggle source
# File lib/staticd/datastores/local.rb, line 39
def sha1(file_path)
  Digest::SHA1.hexdigest(File.read(file_path))
end
stored_file_path(file_path) click to toggle source
# File lib/staticd/datastores/local.rb, line 43
def stored_file_path(file_path)
  "#{@path}/#{sha1(file_path)}"
end