| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- require 'zip'
- require 'exifr/jpeg'
- require 'fileutils'
- module Fotos
- class ZipExtractor
- def initialize(options)
- @zip_file_path = options[:zip]
- @destination_path = options[:dest]
- @tmp_dir_path = File.join(Pathname.pwd, 'tmp')
- @extracted_paths = []
- @copied_paths = []
- end
- attr_reader :zip_file_path, :destination_path
- def call
- Dir.mkdir(@tmp_dir_path) unless Dir.exist?(@tmp_dir_path)
- extract
- copy
- ensure
- FileUtils.rm_r(@tmp_dir_path)
- end
- private
- def extract
- Zip::File.open(zip_file_path) do |zip_file|
- zip_file.each do |entry|
- file_name = entry.name.split('/').last
- dest_path = File.join(@tmp_dir_path, file_name)
- @extracted_paths << dest_path
- entry.extract(dest_path)
- rescue Zip::DestinationFileExistsError
- next
- end
- end
- end
- def copy
- date_format = '%d-%m-%Y'
- @copied_paths = @extracted_paths.map do |fp|
- file_name = File.basename(fp)
- puts "looking at #{file_name}"
- exif = EXIFR::JPEG.new(fp)
- # get date from file's exif data
- exif_date_str = exif.date_time.strftime(date_format)
- # destination dir for given file in public/images/dd-mm-yyyy
- dest_dir = File.join(Pathname.pwd, destination_path, exif_date_str)
- dest_path = File.join(dest_dir, File.basename(fp))
- # create destination dir if not already there
- FileUtils.mkdir_p(dest_dir) unless Dir.exist?(dest_dir)
- puts "#{file_name} --> #{dest_path}"
- FileUtils.cp(fp, dest_path)
- dest_path
- end
- end
- def generate_thumbnails
- @copied_paths.map do |fp|
- Thumbnail.new(fp).create
- end
- end
- end
- end
|