Much base work started
This commit is contained in:
46
app/services/storage_adapters/base_adapter.rb
Normal file
46
app/services/storage_adapters/base_adapter.rb
Normal file
@@ -0,0 +1,46 @@
|
||||
module StorageAdapters
|
||||
class BaseAdapter
|
||||
def initialize(storage_location)
|
||||
@storage_location = storage_location
|
||||
end
|
||||
|
||||
# Scan for video files and return array of relative paths
|
||||
def scan
|
||||
raise NotImplementedError, "#{self.class} must implement #scan"
|
||||
end
|
||||
|
||||
# Generate streaming URL for a video
|
||||
def stream_url(video)
|
||||
raise NotImplementedError, "#{self.class} must implement #stream_url"
|
||||
end
|
||||
|
||||
# Check if file exists at path
|
||||
def exists?(file_path)
|
||||
raise NotImplementedError, "#{self.class} must implement #exists?"
|
||||
end
|
||||
|
||||
# Check if storage can be read from
|
||||
def readable?
|
||||
raise NotImplementedError, "#{self.class} must implement #readable?"
|
||||
end
|
||||
|
||||
# Check if storage can be written to
|
||||
def writable?
|
||||
@storage_location.writable?
|
||||
end
|
||||
|
||||
# Write/copy file to storage
|
||||
def write(source_path, dest_path)
|
||||
raise NotImplementedError, "#{self.class} must implement #write"
|
||||
end
|
||||
|
||||
# Download file to local temp path (for processing)
|
||||
def download_to_temp(video)
|
||||
raise NotImplementedError, "#{self.class} must implement #download_to_temp"
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
attr_reader :storage_location
|
||||
end
|
||||
end
|
||||
59
app/services/storage_adapters/local_adapter.rb
Normal file
59
app/services/storage_adapters/local_adapter.rb
Normal file
@@ -0,0 +1,59 @@
|
||||
module StorageAdapters
|
||||
class LocalAdapter < BaseAdapter
|
||||
VIDEO_EXTENSIONS = %w[.mp4 .mkv .avi .mov .wmv .flv .webm .m4v].freeze
|
||||
|
||||
def scan
|
||||
return [] unless readable?
|
||||
|
||||
pattern = if storage_location.scan_subdirectories
|
||||
File.join(storage_location.path, "**", "*{#{VIDEO_EXTENSIONS.join(',')}}")
|
||||
else
|
||||
File.join(storage_location.path, "*{#{VIDEO_EXTENSIONS.join(',')}}")
|
||||
end
|
||||
|
||||
Dir.glob(pattern, File::FNM_CASEFOLD).map do |full_path|
|
||||
full_path.sub(storage_location.path + "/", "")
|
||||
end
|
||||
end
|
||||
|
||||
def stream_url(video)
|
||||
full_path(video)
|
||||
end
|
||||
|
||||
def exists?(file_path)
|
||||
File.exist?(full_path_from_relative(file_path))
|
||||
end
|
||||
|
||||
def readable?
|
||||
return false unless storage_location.path.present?
|
||||
|
||||
File.directory?(storage_location.path) && File.readable?(storage_location.path)
|
||||
end
|
||||
|
||||
def writable?
|
||||
super && File.writable?(storage_location.path)
|
||||
end
|
||||
|
||||
def write(source_path, dest_path)
|
||||
dest_full_path = full_path_from_relative(dest_path)
|
||||
FileUtils.mkdir_p(File.dirname(dest_full_path))
|
||||
FileUtils.cp(source_path, dest_full_path)
|
||||
dest_path
|
||||
end
|
||||
|
||||
def download_to_temp(video)
|
||||
# Already local, return path
|
||||
full_path(video)
|
||||
end
|
||||
|
||||
def full_path(video)
|
||||
full_path_from_relative(video.file_path)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def full_path_from_relative(file_path)
|
||||
File.join(storage_location.path, file_path)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user