class Blobsterix::Storage::FileSystemMetaData

Public Class Methods

new(path_, payload={}) click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 6
def initialize(path_, payload={})
  @payload=payload
  @path = path_
  @last_modified = ""
  load_meta_file
end

Public Instance Methods

accept_type() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 65
def accept_type
  @accept_type ||= AcceptType.new(get_mime().to_s)
end
as_json() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 129
def as_json
  {'mimetype' => mimetype, 'mediatype' => mediatype, 'etag' => etag, 'size' => size,'payload' => payload.to_json}
end
check(key) click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 17
def check(key)
  @key === key
end
data() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 38
def data
  # begin
  #   raise StandardError.new
  # rescue StandardError => e
  #   puts "VERY EXPENSIVE FILE READ"
  #   puts e.backtrace
  # end
  # logger.info "VERY EXPENSIVE FILE READ"
  File.exists?(path) ? File.read(path) : ""
end
delete() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 111
def delete
  File.delete(meta_path) if File.exists?(meta_path)
  # File.delete(path) if valid
  Pathname.new(path).ascend do |p|
    begin
      p.delete
    rescue Errno::ENOTEMPTY
      # stop deleting path when non empty dir is reached
      break
    end
  end if valid
  true
end
etag() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 21
def etag
  if @last_modified === last_modified
    @etag ||= Digest::MD5.file(path).hexdigest
  else
    @last_modified = last_modified
    @etag = Digest::MD5.file(path).hexdigest
  end
end
header() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 69
def header()
  {"Etag" => etag, "Content-Type" => mimetype, "Last-Modified" => last_modified.strftime("%a, %d %b %Y %H:%M:%S GMT"), "Cache-Control" => "max-age=#{60*60*24}", "Expires" => (Time.new+(60*60*24)).strftime("%a, %d %b %Y %H:%M:%S GMT")}
end
last_accessed() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 61
def last_accessed
  File.atime(path)#.strftime("%Y-%m-%dT%H:%M:%S.000Z")
end
last_modified() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 57
def last_modified
  File.ctime(path)#.strftime("%Y-%m-%dT%H:%M:%S.000Z")
end
mediatype() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 34
def mediatype
  (@mediatype ||= get_mime.mediatype)
end
mimetype() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 30
def mimetype
  (@mimetype ||= get_mime.type)
end
open() { |f| ... } click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 97
def open
  begin
    if block_given?
      f = File.open(path, "rb")
      yield f
      f.close
    else
      File.open(path, "rb")
    end
  rescue
    raise ::Blobsterix::StorageError.new("Could not open FilesystemMetaData")
  end
end
path() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 49
def path
  @path
end
payload() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 77
def payload
  @payload||={}
end
size() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 53
def size
  @size ||= File.exists?(path) ? File.size(path) : 0
end
to_json() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 125
def to_json
  as_json.to_json
end
to_s() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 13
def to_s
  @path
end
valid() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 73
def valid
  File.exists?(path)
end
write() { |f| ... } click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 81
def write
  begin
    if block_given?
      delete
      FileUtils.mkdir_p(File.dirname(path))
      f = File.open(path, "wb")
      yield f
      f.close
    end
    save_meta_file
    self
  rescue
    raise ::Blobsterix::StorageError.new("Could not create MetaData entry")
  end
end

Private Instance Methods

get_mime() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 144
def get_mime
  @mimeclass ||= (MimeMagic.by_path(mime_path) || ((MimeMagic.by_magic(File.open(path)) if File.exists?(path)) ) || MimeMagic.new("text/plain"))
end
load_meta_file() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 155
def load_meta_file
  return if not valid

  if not File.exists?(meta_path)
    save_meta_file
  else
    begin
      data = JSON.load(File.read(meta_path)) || {}
      @mimetype = data["mimetype"]
      @mediatype = data["mediatype"]
      @etag = data["etag"]
      @size = data["size"]
      @payload = JSON.load(data["payload"]) || {}
      @mimeclass = @mimetype ? MimeMagic.new(@mimetype) : nil
    rescue JSON::ParserError => e
      save_meta_file
    end
  end
end
meta_path() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 140
def meta_path
  @meta_path ||= "#{path}.meta"
end
mime_path() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 134
def mime_path
  unzip_trafo = (payload[:trafo].select do |t|
                  t[0] == "unzip"
                end if payload && payload[:trafo]) || [[nil,nil]]
  (unzip_trafo[0][1] if unzip_trafo[0]) || path
end
save_meta_file() click to toggle source
# File lib/blobsterix/storage/file_system_meta_data.rb, line 147
def save_meta_file
  return if not valid
  begin
    File.write(meta_path, to_json)
  rescue
    raise ::Blobsterix::StorageError.new("Could not create MetaData entry")
  end
end