56 lines
1.3 KiB
Ruby
56 lines
1.3 KiB
Ruby
class FileScannerService
|
|
def initialize(storage_location)
|
|
@storage_location = storage_location
|
|
end
|
|
|
|
def scan
|
|
return failure_result("Storage location not accessible") unless @storage_location.accessible?
|
|
|
|
video_files = find_video_files
|
|
new_videos = process_files(video_files)
|
|
|
|
success_result(new_videos)
|
|
rescue => e
|
|
failure_result(e.message)
|
|
end
|
|
|
|
private
|
|
|
|
def find_video_files
|
|
Dir.glob(File.join(@storage_location.path, "**", "*.{mp4,avi,mkv,mov,wmv,flv,webm,m4v}"))
|
|
end
|
|
|
|
def process_files(file_paths)
|
|
new_videos = []
|
|
|
|
file_paths.each do |file_path|
|
|
filename = File.basename(file_path)
|
|
|
|
next if Video.exists?(filename: filename, storage_location: @storage_location)
|
|
|
|
video = Video.create!(
|
|
filename: filename,
|
|
storage_location: @storage_location,
|
|
work: Work.find_or_create_by(title: extract_title(filename))
|
|
)
|
|
|
|
new_videos << video
|
|
VideoProcessorJob.perform_later(video.id)
|
|
end
|
|
|
|
new_videos
|
|
end
|
|
|
|
def extract_title(filename)
|
|
# Simple title extraction - can be enhanced
|
|
File.basename(filename, ".*").gsub(/[\[\(].*?[\]\)]/, "").strip
|
|
end
|
|
|
|
def success_result(videos = [])
|
|
{ success: true, videos: videos, message: "Found #{videos.length} new videos" }
|
|
end
|
|
|
|
def failure_result(message)
|
|
{ success: false, message: message }
|
|
end
|
|
end |