Browse Source

Move vips related logic from organizer to asset class

Lukasz Badura 3 months ago
parent
commit
59ec2e6a42
2 changed files with 15 additions and 8 deletions
  1. 14 0
      lib/fotos/asset.rb
  2. 1 8
      lib/fotos/organizer.rb

+ 14 - 0
lib/fotos/asset.rb

@@ -1,3 +1,5 @@
+require 'vips'
+
 module Fotos
   class Asset
     THUMBNAIL_PREFIX = "TH_"
@@ -40,5 +42,17 @@ module Fotos
     def is_thumbnail?
       file_name.match?(THUMBNAIL_PREFIX)
     end
+
+    def image
+      @image ||= Vips::Image.new_from_file(@file_path)
+    end
+
+    def exif_date
+      field_name = 'exif-ifd2-DateTimeOriginal'
+      exif_date_data = image.get_value(field_name).split(' ')[0..1].join(':')
+      dt_attrs = exif_date_data.split(':').map(&:to_i)
+      # get date from file's exif data
+      DateTime.new(*dt_attrs)
+    end
   end
 end

+ 1 - 8
lib/fotos/organizer.rb

@@ -1,9 +1,7 @@
 require 'fileutils'
-require 'vips'
 
 module Fotos
   class Organizer
-    VIPS_DATE_FIELD = 'exif-ifd2-DateTimeOriginal'
 
     def initialize(options)
       @source_path = options[:source]
@@ -18,12 +16,7 @@ module Fotos
         puts "looking at #{asset.file_name}"
         next unless asset.supported?
 
-        image = Vips::Image.new_from_file(fp)
-        exif_date_data = image.get_value(VIPS_DATE_FIELD).split(' ')[0..1].join(':')
-        dt_attrs = exif_date_data.split(':').map(&:to_i)
-
-        # get date from file's exif data
-        exif_date_str = DateTime.new(*dt_attrs).strftime(Fotos::DATE_FORMAT)
+        exif_date_str = asset.exif_date.strftime(Fotos::DATE_FORMAT)
 
         dest_dir = File.join(@destination_path, exif_date_str)
         dest_path = File.join(dest_dir, asset.file_name)