class Safrano::Media::StaticTree

Simple static File/Directory based media store handler similar to Rack::Static with directory Tree structure

Constants

SEP
VERS

Public Class Methods

path_builder(ids) click to toggle source
# File lib/odata/collection_media.rb, line 149
def self.path_builder(ids)
  ids.map { |id| id.to_s.chars.join('/') }.join(SEP) << VERS
end

Public Instance Methods

in_media_directory(entity) { || ... } click to toggle source
# File lib/odata/collection_media.rb, line 165
def in_media_directory(entity)
  mpi = media_directory(entity)
  FileUtils.makedirs mpi unless Dir.exist?(mpi)
  Dir.chdir(mpi) { yield }
end
media_directory(entity) click to toggle source

this is relative to abs_klass_dir(entity) eg to /@root/Photo tree-structure

media_path_ids = 1  -->  1/v
media_path_ids = 15  -->  1/5/v
media_path_ids = 555  -->  5/5/5/v
media_path_ids = 5,5,5  -->  5/00/5/00/5/v
media_path_ids = 5,00,5  -->  5/00/0/0/00/5/v
media_path_ids = 5,xyz,5  -->  5/00/x/y/z/00/5/v
# File lib/odata/collection_media.rb, line 161
def media_directory(entity)
  StaticTree.path_builder(entity.media_path_ids)
end
odata_delete(entity:) click to toggle source
# File lib/odata/collection_media.rb, line 171
def odata_delete(entity:)
  Dir.chdir(@abs_klass_dir) do
    in_media_directory(entity) do
      Dir.glob('*').sort.each { |oldf| File.delete(oldf) if File.file?(oldf) }
    end
  end
end
replace_file(data:, filename:, entity:) click to toggle source

Here as well, MVP implementation

def replace_file(data:, filename:, entity:)
  Dir.chdir(abs_klass_dir(entity)) do
    in_media_directory(entity) do
      Dir.glob('*').each { |oldf| File.delete(oldf) if File.file?(oldf) }
      File.open(filename, 'wb') { |f| IO.copy_stream(data, f) }
    end
  end
end

Here as well, MVP implementation

# File lib/odata/collection_media.rb, line 189
def replace_file(data:, filename:, entity:)
  Dir.chdir(@abs_klass_dir) do
    in_media_directory(entity) do
      version = nil
      Dir.glob('*').sort.each do |oldf|
        version = oldf
        File.delete(oldf)
      end
      filename = (version.to_i + 1).to_s
      File.open(filename, 'wb') { |f| IO.copy_stream(data, f) }
    end
  end
end