class StoreAgent::Node::FileObject

ファイル

Attributes

body[W]

Public Instance Methods

chown(*) click to toggle source
Calls superclass method StoreAgent::Node::Object#chown
# File lib/store_agent/node/object/file_object.rb, line 118
def chown(*)
  super do
  end
end
copy(dest_path = nil, *) click to toggle source
Calls superclass method StoreAgent::Node::Object#copy
# File lib/store_agent/node/object/file_object.rb, line 73
def copy(dest_path = nil, *)
  super do
    file_body = read
    dest_file = build_dest_file(dest_path)
    if dest_file.exists?
      dest_file.update(file_body)
    else
      dest_file.create(read)
    end
  end
end
create(*params, &block) click to toggle source
Calls superclass method StoreAgent::Node::Object#create
# File lib/store_agent/node/object/file_object.rb, line 23
def create(*params, &block)
  super do
    set_body(*params, &block)
    save
    workspace.version_manager.add(storage_object_path)
  end
end
delete(*) click to toggle source
Calls superclass method StoreAgent::Node::Object#delete
# File lib/store_agent/node/object/file_object.rb, line 59
def delete(*)
  super do
    FileUtils.rm(storage_object_path)
    workspace.version_manager.remove(storage_object_path)
  end
end
move(dest_path = nil, *) click to toggle source
Calls superclass method StoreAgent::Node::Object#move
# File lib/store_agent/node/object/file_object.rb, line 85
def move(dest_path = nil, *)
  super do
    dest_file = build_dest_file(dest_path)
    if dest_file.exists?
      disk_usage_diff = metadata.disk_usage - dest_file.metadata.disk_usage
      file_count = 0
    else
      disk_usage_diff = metadata.disk_usage
      file_count = 1
    end
    FileUtils.mv(storage_object_path, dest_file.storage_object_path)
    FileUtils.mv(metadata.file_path, dest_file.metadata.file_path)
    FileUtils.mv(permission.file_path, dest_file.permission.file_path)
    dest_file.touch
    dest_file.parent_directory.metadata.update(disk_usage: disk_usage_diff, directory_file_count: file_count, tree_file_count: file_count, recursive: true)
    parent_directory.metadata.update(disk_usage: -dest_file.metadata.disk_usage, directory_file_count: -1, tree_file_count: -1, recursive: true)

    [storage_object_path, metadata.file_path, permission.file_path].each do |file_path|
      workspace.version_manager.remove(file_path)
    end
  end
end
read(revision: nil) click to toggle source
Calls superclass method StoreAgent::Node::Object#read
# File lib/store_agent/node/object/file_object.rb, line 31
def read(revision: nil)
  super do
    if revision.nil?
      encoded_data = open(storage_object_path) do |f|
        f.read
      end
    else
      encoded_data = workspace.version_manager.read(path: storage_object_path, revision: revision)
    end
    StoreAgent.config.attachment_data_encoders.reverse.inject(encoded_data) do |data, encoder|
      encoder.decode(data)
    end
  end
end
save() click to toggle source
# File lib/store_agent/node/object/file_object.rb, line 145
def save
  encoded_data = StoreAgent.config.storage_data_encoders.inject(@body) do |data, encoder|
    encoder.encode(data)
  end
  open(storage_object_path, "w") do |f|
    f.write encoded_data
  end
end
set_permission(*) click to toggle source
Calls superclass method StoreAgent::Node::Object#set_permission
# File lib/store_agent/node/object/file_object.rb, line 123
def set_permission(*)
  super do
  end
end
touch(*) click to toggle source
Calls superclass method StoreAgent::Node::Object#touch
# File lib/store_agent/node/object/file_object.rb, line 66
def touch(*)
  super do
    FileUtils.touch(storage_object_path)
    workspace.version_manager.add(storage_object_path)
  end
end
unset_permission(*) click to toggle source
# File lib/store_agent/node/object/file_object.rb, line 128
def unset_permission(*)
  super do
  end
end
update(*params, &block) click to toggle source
Calls superclass method StoreAgent::Node::Object#update
# File lib/store_agent/node/object/file_object.rb, line 46
def update(*params, &block)
  super do
    set_body(*params, &block)
    if @body.nil?
      raise "file body required"
    end
    save
    disk_usage_diff = @body.length - metadata.disk_usage
    metadata.update(disk_usage: disk_usage_diff, recursive: true)
    workspace.version_manager.add(storage_object_path)
  end
end

Private Instance Methods

build_dest_file(dest_path) click to toggle source
# File lib/store_agent/node/object/file_object.rb, line 183
def build_dest_file(dest_path)
  dest_object = workspace.find_object(dest_path)
  if dest_object.directory?
    sub_directory_object = dest_object.find_object(File.basename(path))
    if sub_directory_object.directory?
      raise InvalidNodeTypeError.new(src_object: self, dest_object: sub_directory_object)
    end
    dest_object.file(File.basename(path))
  else
    workspace.file(dest_path)
  end
end
initial_bytesize() click to toggle source
# File lib/store_agent/node/object/file_object.rb, line 164
def initial_bytesize
  (@body || "").size
end
set_body(*params) { |self| ... } click to toggle source
# File lib/store_agent/node/object/file_object.rb, line 168
def set_body(*params)
  case
  when block_given?
    yield self
  when (options = params.first).is_a?(String)
    @body = options
  when options.is_a?(Symbol)
    @body = options.to_s
  when options.is_a?(Hash)
    @body = options["body"] || options[:body]
  else
    @body = nil
  end
end