#!/usr/bin/env ruby # frozen_string_literal: true # encoding: UTF-8 # (c) ANB Andrew Bizyaev require 'phtools/error' require 'date' require 'fileutils' module PhTools # media type constants 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 3gp].freeze FILE_TYPE_AUDIO = %w[wav].freeze # phtools file name operations class PhFile include Comparable # filename constants NICKNAME_MIN_SIZE = 3 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 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 file_type.include?(File.extname(filename).slice(1..-1).downcase) end def self.validate_author(author) case when author.size != NICKNAME_SIZE return [false, "'#{author}' wrong author size, should be #{NICKNAME_SIZE} chars long"] when /[-_\s]/.match(author) return [false, "'#{author}' author should not contain spaces [_- ]"] when /[\d]/.match(author) return [false, "'#{author}' author should not contain digits"] when /[\W]/.match(author) return [false, "'#{author}' author should contain only ASCII chars"] end [true, ''] end def self.get_date_time(date_string) /^(?\d{8})-(?