Browse Source

feat: automatically convert heic files to jpg upon running organize

Lukasz Badura 3 months ago
parent
commit
b0594c7fdd
5 changed files with 21 additions and 7 deletions
  1. 2 1
      bin/fotos
  2. 8 0
      lib/fotos/asset.rb
  3. 1 1
      lib/fotos/html_generator.rb
  4. 9 4
      lib/fotos/organizer.rb
  5. 1 1
      lib/fotos/thumbnail.rb

+ 2 - 1
bin/fotos

@@ -24,7 +24,8 @@ when 'build'
   parser.on('-t', '--thumbnails', 'Automatically generate missing thumbnails')
   parser.on('-t', '--thumbnails', 'Automatically generate missing thumbnails')
   options = {dest: '.'}
   options = {dest: '.'}
   parser.parse!(into: options)
   parser.parse!(into: options)
-  Fotos::HtmlGenerator.new(options).call
+  html = Fotos::HtmlGenerator.new(options).call
+  File.write(File.join(options[:dest], 'index.html'), html)
 else
 else
   puts "Unknown command! Available commands: #{COMMANDS.join(',')}."
   puts "Unknown command! Available commands: #{COMMANDS.join(',')}."
   exit(-1)
   exit(-1)

+ 8 - 0
lib/fotos/asset.rb

@@ -44,6 +44,14 @@ module Fotos
       file_name.match?(THUMBNAIL_PREFIX)
       file_name.match?(THUMBNAIL_PREFIX)
     end
     end
 
 
+    def file_base
+      file_name.split('.').first
+    end
+
+    def heic?
+      ext_name.match?(/heic/i)
+    end
+
     def web_supported?
     def web_supported?
       WEB_TYPES.include?(ext_name.upcase)
       WEB_TYPES.include?(ext_name.upcase)
     end
     end

+ 1 - 1
lib/fotos/html_generator.rb

@@ -22,7 +22,7 @@ module Fotos
         assets.reject(&:is_thumbnail?).each do |asset|
         assets.reject(&:is_thumbnail?).each do |asset|
           ensure_thumbnail(asset)
           ensure_thumbnail(asset)
 
 
-          if File.exist?(asset.thumbnail_path(@dest_dir.path))
+          if File.exist?(File.join(@dest_dir.path, asset.thumbnail_path))
             html_output << "<a href=\"#{asset.path}\"><img src=\"#{asset.thumbnail_path}\" /></a>"
             html_output << "<a href=\"#{asset.path}\"><img src=\"#{asset.thumbnail_path}\" /></a>"
           else
           else
             html_output << "<a href=\"#{asset.dirname}/#{asset.file_name}\"><img src=\"#{asset.path}\" /></a>"
             html_output << "<a href=\"#{asset.dirname}/#{asset.file_name}\"><img src=\"#{asset.path}\" /></a>"

+ 9 - 4
lib/fotos/organizer.rb

@@ -19,13 +19,18 @@ module Fotos
         exif_date_str = asset.exif_date.strftime(Fotos::DATE_FORMAT)
         exif_date_str = asset.exif_date.strftime(Fotos::DATE_FORMAT)
 
 
         dest_dir = File.join(@destination_path, exif_date_str)
         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
         # create destination dir if not already there
         FileUtils.mkdir_p(dest_dir) unless Dir.exist?(dest_dir)
         FileUtils.mkdir_p(dest_dir) unless Dir.exist?(dest_dir)
+
+        if asset.web_supported?
+          dest_path = File.join(dest_dir, asset.file_name)
+          FileUtils.cp(fp, dest_path)
+        elsif asset.heic?
+          target_file_name = [asset.file_base, '.JPG'].join
+          dest_path = File.join(dest_dir, target_file_name)
+          asset.image.write_to_file(dest_path)
+        end
         puts "#{asset.file_name} --> #{dest_path}"
         puts "#{asset.file_name} --> #{dest_path}"
-        FileUtils.cp(fp, dest_path)
-        dest_path
       end
       end
     end
     end
   end
   end

+ 1 - 1
lib/fotos/thumbnail.rb

@@ -7,7 +7,7 @@ module Fotos
     end
     end
 
 
     def create(destination_path = ".")
     def create(destination_path = ".")
-      target_path = @asset.thumbnail_path(destination_path)
+      target_path = File.join(destination_path, @asset.thumbnail_path)
       return target_path if File.exist?(target_path)
       return target_path if File.exist?(target_path)
       th_image = Vips::Image.thumbnail(@asset.file_path, WIDTH)
       th_image = Vips::Image.thumbnail(@asset.file_path, WIDTH)
       th_image.write_to_file(target_path)
       th_image.write_to_file(target_path)