Class: MP4::Source::Local

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/mp4/source/local.rb

Constant Summary

Constants included from Base

Base::REQUIRED_METHODS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#fetch, included

Constructor Details

#initialize(path_to_file) ⇒ Local

Returns a new instance of Local.



29
30
31
# File 'lib/mp4/source/local.rb', line 29

def initialize(path_to_file)
  @path_to_file = path_to_file.to_s
end

Instance Attribute Details

#path_to_fileObject (readonly)

Returns the value of attribute path_to_file.



5
6
7
# File 'lib/mp4/source/local.rb', line 5

def path_to_file
  @path_to_file
end

Class Method Details

.from_uri(uri, base_dir: nil) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
# File 'lib/mp4/source/local.rb', line 7

def self.from_uri(uri, base_dir: nil)
  raise ArgumentError, "expected local: URI, got #{uri.inspect}" unless uri.start_with?('local:')

  path = uri.sub(/\Alocal:/, '')
  resolved = resolve_path(path, base_dir)
  new(resolved.to_s)
end

.resolve_path(path, base_dir) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mp4/source/local.rb', line 15

def self.resolve_path(path, base_dir)
  candidate = Pathname.new(path)
  return candidate if base_dir.nil?

  root = Pathname.new(base_dir).expand_path
  absolute = (candidate.absolute? ? candidate : root.join(candidate)).expand_path

  unless absolute.to_s.start_with?(root.to_s + File::SEPARATOR) || absolute.to_s == root.to_s
    raise ArgumentError, "path escapes base_dir: #{path.inspect}"
  end

  absolute
end

Instance Method Details

#closeObject



68
69
70
71
# File 'lib/mp4/source/local.rb', line 68

def close
  @file&.close
  @file = nil
end

#eof?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/mp4/source/local.rb', line 54

def eof?
  file.eof?
end

#mtimeObject



62
63
64
65
66
# File 'lib/mp4/source/local.rb', line 62

def mtime
  File.mtime(@path_to_file)
rescue Errno::ENOENT
  nil
end

#open!Object



49
50
51
52
# File 'lib/mp4/source/local.rb', line 49

def open!
  file
  self
end

#posObject



58
59
60
# File 'lib/mp4/source/local.rb', line 58

def pos
  file.pos
end

#read(n) ⇒ Object



37
38
39
# File 'lib/mp4/source/local.rb', line 37

def read(n)
  file.read(n)
end

#reset_seekObject



45
46
47
# File 'lib/mp4/source/local.rb', line 45

def reset_seek
  file.seek(0)
end

#set_seek(n) ⇒ Object



41
42
43
# File 'lib/mp4/source/local.rb', line 41

def set_seek(n)
  file.seek(n)
end

#uriObject



33
34
35
# File 'lib/mp4/source/local.rb', line 33

def uri
  "local:#{@path_to_file}"
end