Browse Source

feat: add organize command to distribute images in directories

Lukasz Badura 4 months ago
parent
commit
5847e639a6
5 changed files with 61 additions and 1 deletions
  1. 1 0
      .tool-versions
  2. 7 1
      bin/fotos
  3. 1 0
      lib/fotos.rb
  4. 9 0
      lib/fotos/asset.rb
  5. 43 0
      lib/fotos/organizer.rb

+ 1 - 0
.tool-versions

@@ -0,0 +1 @@
+ruby 3.4.5

+ 7 - 1
bin/fotos

@@ -2,7 +2,7 @@
 
 
 require 'fotos'
 require 'fotos'
 require 'optparse'
 require 'optparse'
-COMMANDS = %w(extract build)
+COMMANDS = %w(extract organize build)
 parser = OptionParser.new
 parser = OptionParser.new
 
 
 case ARGV[0]
 case ARGV[0]
@@ -12,6 +12,12 @@ when 'extract'
   options = {dest: '.'}
   options = {dest: '.'}
   parser.parse!(into: options)
   parser.parse!(into: options)
   Fotos::ZipExtractor.new(options).call
   Fotos::ZipExtractor.new(options).call
+when 'organize'
+  options = {}
+  parser.on('-s [PATH]', '--source', 'Input directory with image directories')
+  parser.on('-d [PATH]', '--destination', 'Copy to a destinatio dir')
+  parser.parse!(into: options)
+  Fotos::Organizer.new(options).call
 when 'build'
 when 'build'
   parser.on('-s [PATH]', '--source', 'Input directory with image directories')
   parser.on('-s [PATH]', '--source', 'Input directory with image directories')
   parser.on('-d [PATH]', '--dest', 'Output directory for html and images')
   parser.on('-d [PATH]', '--dest', 'Output directory for html and images')

+ 1 - 0
lib/fotos.rb

@@ -3,6 +3,7 @@
 require_relative "fotos/version"
 require_relative "fotos/version"
 require_relative "fotos/zip_extractor"
 require_relative "fotos/zip_extractor"
 require_relative "fotos/html_generator"
 require_relative "fotos/html_generator"
+require_relative "fotos/organizer"
 require_relative "fotos/thumbnail"
 require_relative "fotos/thumbnail"
 require_relative "fotos/source_directory"
 require_relative "fotos/source_directory"
 require_relative "fotos/asset"
 require_relative "fotos/asset"

+ 9 - 0
lib/fotos/asset.rb

@@ -1,6 +1,7 @@
 module Fotos
 module Fotos
   class Asset
   class Asset
     THUMBNAIL_PREFIX = "TH_"
     THUMBNAIL_PREFIX = "TH_"
+    SUPPORTED_TYPES = ['.JPEG', '.JPG']
 
 
     def initialize(file_path)
     def initialize(file_path)
       @file_path = file_path
       @file_path = file_path
@@ -16,6 +17,14 @@ module Fotos
       File.basename(@file_path)
       File.basename(@file_path)
     end
     end
 
 
+    def ext_name
+      File.extname(@file_path)
+    end
+
+    def supported?
+      SUPPORTED_TYPES.include?(ext_name.upcase)
+    end
+
     def thumbnail_name
     def thumbnail_name
       [THUMBNAIL_PREFIX, file_name].join
       [THUMBNAIL_PREFIX, file_name].join
     end
     end

+ 43 - 0
lib/fotos/organizer.rb

@@ -0,0 +1,43 @@
+require 'exifr/jpeg'
+require 'fileutils'
+
+module Fotos
+  class Organizer
+    def initialize(options)
+      @source_path = options[:source]
+      @destination_path = options[:destination]
+    end
+
+    def call
+      date_format = '%d-%m-%Y'
+      source_path_pattern = File.join(@source_path, "**/*")
+      paths = Dir.glob(source_path_pattern)
+      paths.each do |fp|
+        asset = Asset.new(fp)
+        puts "looking at #{asset.file_name}"
+        next unless asset.supported?
+
+        exif = EXIFR::JPEG.new(fp)
+
+        # get date from file's exif data
+        exif_date_str = exif.date_time.strftime(Fotos::DATE_FORMAT)
+
+        dest_dir = File.join(@destination_path, exif_date_str)
+        dest_path = File.join(dest_dir, asset.file_name)
+
+        # create destination dir if not already there
+        FileUtils.mkdir_p(dest_dir) unless Dir.exist?(dest_dir)
+        puts "#{asset.file_name} --> #{dest_path}"
+        FileUtils.cp(fp, dest_path)
+        dest_path
+      end
+    end
+
+    private
+    def generate_thumbnails
+      @copied_paths.map do |fp|
+        Thumbnail.new(fp).create
+      end
+    end
+  end
+end