Class: Visor::Image::Store::FileSystem

Inherits:
Object
  • Object
show all
Includes:
Common::Exception
Defined in:
lib/image/store/file_system.rb

Overview

The FileSystem backend store.

This class handles the management of image files located in the local FileSystem, based on a URI like file:///path/to/my_image.format.

Constant Summary collapse

CHUNKSIZE =

Size of the chunk to stream the files out.

65536

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, config) ⇒ Object

Initializes a new FileSystem store client object.

Parameters:

  • uri (String)

    The URI of the file location.

  • config (Hash)

    A set of configurations for the wanted store, loaded from VISoR configuration file.



28
29
30
31
32
# File 'lib/image/store/file_system.rb', line 28

def initialize(uri, config)
  @uri    = URI(uri)
  @fp     = @uri.path
  @config = config[:file]
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



18
19
20
# File 'lib/image/store/file_system.rb', line 18

def config
  @config
end

#fpObject

Returns the value of attribute fp.



18
19
20
# File 'lib/image/store/file_system.rb', line 18

def fp
  @fp
end

#uriObject

Returns the value of attribute uri.



18
19
20
# File 'lib/image/store/file_system.rb', line 18

def uri
  @uri
end

Instance Method Details

#deleteObject

Deletes the image file to from its location.

Raises:

  • (Forbidden)

    If user does not have permission to manipulate the image file.

  • (NotFound)

    If the image file was not found.



83
84
85
86
87
88
89
90
# File 'lib/image/store/file_system.rb', line 83

def delete
  file_exists?
  begin
    File.delete(fp)
  rescue => e
    raise Forbidden, "Error while trying to delete image file #{fp}: #{e.message}"
  end
end

#file_exists?Boolean

Check if the image file exists.

Returns:

  • (Boolean)

Raises:

  • (NotFound)

    If the image file was not found.



96
97
98
# File 'lib/image/store/file_system.rb', line 96

def file_exists?
  raise NotFound, "No image file found at #{fp}" unless File.exists?(fp)
end

#getObject

Returns the image file to clients, streamed in chunks.

Returns:

  • (Object)

    Yields the file, a chunk at time.



38
39
40
41
42
43
44
# File 'lib/image/store/file_system.rb', line 38

def get
  file_exists?
  open(fp, "rb") do |file|
    yield file.read(CHUNKSIZE) until file.eof?
    yield nil
  end
end

#save(id, tmp_file, format) ⇒ String, Integer

Saves the image file to the its final destination, based on the temporary file created by the server at data reception time.

Parameters:

  • id (String)

    The image id.

  • tmp_file (File)

    The temporary file descriptor.

  • format (String)

    The image file format.

Returns:

  • (String, Integer)

    The generated file location URI and image file size.

Raises:

  • (Duplicated)

    If the image file already exists.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/image/store/file_system.rb', line 57

def save(id, tmp_file, format)
  dir  = File.expand_path config[:directory]
  file = "#{id}.#{format}"
  fp   = File.join(dir, file)
  uri  = "file://#{fp}"
  size = tmp_file.size

  FileUtils.mkpath(dir) unless Dir.exists?(dir)
  raise Duplicated, "The image file #{fp} already exists" if File.exists?(fp)
  STDERR.puts "Copying image tempfile #{tmp_file.path} to definitive #{fp}"

  tmp = File.open(tmp_file.path, "rb")
  new = File.open(fp, "wb")

  each_chunk(tmp, CHUNKSIZE) do |chunk|
    new << chunk
  end

  [uri, size]
end