lib/phtools/ph_file.rb in phtools-0.10.0 vs lib/phtools/ph_file.rb in phtools-0.11.1

- old
+ new

@@ -1,21 +1,21 @@ #!/usr/bin/env ruby +# frozen_string_literal: true # encoding: UTF-8 # (c) ANB Andrew Bizyaev require 'phtools/error' require 'date' require 'fileutils' -# Foto tools module PhTools # media type constants - FILE_TYPE_IMAGE_NORMAL = %w{jpg jpeg tif tiff png} - FILE_TYPE_IMAGE_RAW = %w{orf arw dng} + FILE_TYPE_IMAGE_NORMAL = %w(jpg jpeg tif tiff png).freeze + FILE_TYPE_IMAGE_RAW = %w(orf arw dng).freeze FILE_TYPE_IMAGE = FILE_TYPE_IMAGE_NORMAL + FILE_TYPE_IMAGE_RAW - FILE_TYPE_VIDEO = %w{avi mp4 mpg mts dv mov mkv m2t m2ts} - FILE_TYPE_AUDIO = %w{wav} + FILE_TYPE_VIDEO = %w(avi mp4 mpg mts dv mov mkv m2t m2ts 3gp).freeze + FILE_TYPE_AUDIO = %w(wav).freeze # phtools file name operations class PhFile include Comparable @@ -24,11 +24,11 @@ NICKNAME_MAX_SIZE = 6 NICKNAME_SIZE = 3 # should be in range of MIN and MAX ZERO_DATE = DateTime.new(0) def self.validate_file!(filename, file_type) - fail PhTools::Error, 'does not exist' unless + fail PhTools::Error, 'does not exist' unless filename && File.exist?(filename) fail PhTools::Error, 'not a file' if File.directory?(filename) fail PhTools::Error, 'no permission to write' unless File.writable_real?(filename) fail PhTools::Error, 'unsupported type' unless @@ -47,35 +47,55 @@ return [false, "'#{author}' author should contain only ASCII chars"] end [true, ''] end - attr_reader :filename, :dirname, :extname, :basename, :basename_part, + attr_reader :filename, :dirname, :extname, :type, :basename, :basename_part, :basename_clean, :date_time, :author def initialize(filename) set_state(filename) end def to_s - "#{@filename}" + @filename.to_s end def <=>(other) @filename <=> other.filename end def basename_standard(basename_clean: @basename_clean, date_time: @date_time, author: @author) - %Q{#{date_time.strftime('%Y%m%d-%H%M%S')}_#{(author.upcase + "XXXXXX")[0, NICKNAME_SIZE]} #{basename_clean}} + %(#{date_time.strftime('%Y%m%d-%H%M%S')}_#{(author.upcase + 'XXXXXX')[0, NICKNAME_SIZE]} #{basename_clean}) end def basename_is_standard? @basename == basename_standard end + def image? + FILE_TYPE_IMAGE.include?(@type) + end + + def image_normal? + FILE_TYPE_IMAGE_NORMAL.include?(@type) + end + + def image_raw? + FILE_TYPE_IMAGE_RAW.include?(@type) + end + + def video? + FILE_TYPE_VIDEO.include?(@type) + end + + def audio? + FILE_TYPE_AUDIO.include?(@type) + end + def standardize(dirname: @dirname, basename_clean: @basename_clean, extname: @extname, date_time: @date_time, author: @author) File.join(dirname, basename_standard(basename_clean: basename_clean, @@ -98,11 +118,11 @@ extname: @extname) File.join(dirname, basename_clean + extname) end def cleanse!(dirname: @dirname, basename_clean: @basename_clean, - extname: @extname) + extname: @extname) filename = cleanse(dirname: dirname, basename_clean: basename_clean, extname: extname) set_state(filename) filename end @@ -117,21 +137,22 @@ end def date_time_to_time Time.new(@date_time.year, @date_time.month, @date_time.day, @date_time.hour, @date_time.min, @date_time.sec) - # no use of @date_time.zone - assuming file's timezone always - # equals to photografer's computer timezone + # no use of @date_time.zone - assuming file's timezone always + # equals to photografer's computer timezone end private def set_state(filename) @dirname = File.dirname(filename) @extname = File.extname(filename) @basename = File.basename(filename, @extname) @filename = File.join(@dirname, @basename + @extname) + @type = @extname.empty? ? '' : @extname.slice(1..-1).downcase parse_basename @basename_clean = @basename_part[:clean] @date_time = reveal_date_time @author = @basename_part[:author] || '' end @@ -164,11 +185,11 @@ rescue ArgumentError return ZERO_DATE end def parse_basename - default = { prefix: '', clean: '', date: '', - time: '', author: '', id: '', flags: '' } + default = { prefix: '', clean: '', date: '', time: '', author: '', id: '', flags: '' } + case @basename # check YYYYmmdd-HHMMSS_AUT[ID]{FLAGS}cleanname when /^(?<prefix>(?<date>\d{8})-(?<time>\d{6})_(?<author>\w{#{NICKNAME_MIN_SIZE},#{NICKNAME_MAX_SIZE}})\[(?<id>.*)\]\{(?<flags>.*)\})(?<clean>.*)/ @basename_part = default.merge(prefix: Regexp.last_match(:prefix), clean: Regexp.last_match(:clean),