Class: FormatParser::ActiveStorage::BlobIO

Inherits:
Object
  • Object
show all
Defined in:
lib/active_storage/blob_io.rb

Instance Method Summary collapse

Constructor Details

#initialize(blob) ⇒ BlobIO

Parameters:

  • blob (ActiveStorage::Blob)

    the file with linked service



8
9
10
11
12
# File 'lib/active_storage/blob_io.rb', line 8

def initialize(blob)
  @blob = blob
  @service = blob.service
  @pos = 0
end

Instance Method Details

#posInteger

Emulates IO#pos

Returns:

  • (Integer)

    the current offset (in bytes) of the io



46
47
48
# File 'lib/active_storage/blob_io.rb', line 46

def pos
  @pos
end

#read(n_bytes) ⇒ String

Emulates IO#read, but requires the number of bytes to read. Rely on ‘ActiveStorage::Service.download_chunk` of each hosting type (local, S3, Azure, etc)

Parameters:

  • n_bytes (Integer)

    how many bytes to read

Returns:



19
20
21
22
23
24
25
# File 'lib/active_storage/blob_io.rb', line 19

def read(n_bytes)
  # HTTP ranges are exclusive.
  http_range = (@pos..(@pos + n_bytes - 1))
  body = @service.download_chunk(@blob.key, http_range)
  @pos += body.bytesize
  body.force_encoding(Encoding::ASCII_8BIT)
end

#seek(offset) ⇒ Integer

Emulates IO#seek

Parameters:

  • offset (Integer)

    size

Returns:

  • (Integer)

    always return 0, ‘seek` only mutates `pos` attribute



31
32
33
34
# File 'lib/active_storage/blob_io.rb', line 31

def seek(offset)
  @pos = offset
  0
end

#sizeInteger

Emulates IO#size.

Returns:

  • (Integer)

    the size of the blob size from ActiveStorage



39
40
41
# File 'lib/active_storage/blob_io.rb', line 39

def size
  @blob.byte_size
end