Class: PDTP::Server::FileService

Inherits:
FileService show all
Defined in:
lib/pdtp/server/file_service.rb,
lib/pdtp/server/file_service_protocol.rb

Overview

The file service provides utilities for determining various information about files.

Defined Under Namespace

Classes: Protocol

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileService

Returns a new instance of FileService.



35
36
37
38
# File 'lib/pdtp/server/file_service.rb', line 35

def initialize     
  @root = ''
  @default_chunk_size = 512
end

Instance Attribute Details

#default_chunk_sizeObject

Returns the value of attribute default_chunk_size.



33
34
35
# File 'lib/pdtp/server/file_service.rb', line 33

def default_chunk_size
  @default_chunk_size
end

#rootObject

Returns the value of attribute root.



33
34
35
# File 'lib/pdtp/server/file_service.rb', line 33

def root
  @root
end

Instance Method Details

#get_chunk_hash(url, chunk_id) ⇒ Object

returns the SHA256 hash of the specified chunk



70
71
72
# File 'lib/pdtp/server/file_service.rb', line 70

def get_chunk_hash(url, chunk_id)
  Digest::SHA256.hexdigest(get_info(url).chunk_data(chunk_id)) rescue nil
end

#get_info(url) ⇒ Object

Retrieve information about a file. This should really be persistent FIXME the only authentication regarding registered files is performed by checking whether the registered file exists



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pdtp/server/file_service.rb', line 43

def get_info(url)
  begin
    host = URI.parse(url).host
    
    #FIXME we should check host against a list of known hosts here
    info = FileInfo.new
    info.streaming = false
    info.base_chunk_size = @default_chunk_size
    info.path = get_local_path(url)
    raise if File.directory?(info.path)
    info.file_size = File.size?(info.path)
    return nil if info.file_size == 0 or info.file_size.nil?
  rescue
    return nil
  end
  
  info
end

#get_local_path(url) ⇒ Object

returns the path of this file on the local filesystem



63
64
65
66
67
# File 'lib/pdtp/server/file_service.rb', line 63

def get_local_path(url)
  path = URI.parse(url).path
  path = path[1..path.size-1] #remove leading /
  (Pathname.new(@root) + path).to_s  
end