source_directory.rb 777 B

12345678910111213141516171819202122232425262728293031323334
  1. module Fotos
  2. class SourceDirectory
  3. Image = Data.define(:dir_name, :file_name, :file_path, :thumbnail_name, :thumbnail_path)
  4. def initialize(path)
  5. @path = path
  6. end
  7. attr_reader :path
  8. #TODO: Filter out non image subdirecories
  9. def subdirectories
  10. Dir.glob("#{@path}/*").map {|d| d.split('/').last}
  11. end
  12. def dates
  13. subdirectories.map do |d|
  14. Date.strptime(d, DATE_FORMAT)
  15. rescue Date::Error
  16. next
  17. end.compact
  18. end
  19. def files_by_date
  20. result = {}
  21. dates.each do |date|
  22. dir_name = date.strftime(DATE_FORMAT)
  23. files = Dir.glob(File.join(@path, dir_name, "*"))
  24. result[date] = files.map { |path| Fotos::Asset.new(path) }
  25. end
  26. return result
  27. end
  28. end
  29. end