Class: Photein::MediaFile

Inherits:
Object
  • Object
show all
Defined in:
lib/photein/media_file.rb

Direct Known Subclasses

Image, Video

Constant Summary collapse

DATE_FORMAT =
'%F_%H%M%S'
NORMAL_EXTNAME_MAP =
{
  '.jpeg' => '.jpg'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, opts: {}) ⇒ MediaFile

Returns a new instance of MediaFile.



19
20
21
22
# File 'lib/photein/media_file.rb', line 19

def initialize(path, opts: {})
  @path = Pathname(path)
  @config = Photein::Config.with(opts)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/photein/media_file.rb', line 16

def config
  @config
end

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/photein/media_file.rb', line 17

def path
  @path
end

Class Method Details

.for(file, opts: {}) ⇒ Object

Raises:

  • (Errno::ENOENT)


148
149
150
151
152
153
154
155
# File 'lib/photein/media_file.rb', line 148

def for(file, opts: {})
  file = Pathname(file)
  raise Errno::ENOENT, "#{file}" unless file.exist?

  [Image, Video].find { |type| type::SUPPORTED_FORMATS.include?(file.extname.downcase) }
                .tap { |type| raise ArgumentError, "#{file}: Invalid media file" if type.nil? }
                .then { |type| type.new(file, opts: opts) }
end

Instance Method Details

#importObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/photein/media_file.rb', line 24

def import
  return if corrupted?
  return if config.interactive && denied_by_user?
  return if config.safe && in_use?

  config.destinations.map do |lib_type, lib_path|
    next if non_optimizable_format?(lib_type)

    Thread.new do
      dest_extname  = self.class::OPTIMIZATION_FORMAT_MAP.dig(lib_type, extname) || extname
      dest_path     = lib_path
                      .join(Time.parse(dest_filename).strftime('%Y'))
                      .join("#{dest_filename}#{dest_extname}")
                      .then(&method(:resolve_name_collision))
      tempfile      = Pathname(Dir.tmpdir)
                      .join('photein').join(lib_type.to_s)
                      .tap(&FileUtils.method(:mkdir_p))
                      .join(dest_path.basename)

      optimize(tempfile: tempfile, lib_type: lib_type)

      Photein.logger.info(<<~MSG.chomp)
        #{config.keep ? 'copying' : 'moving'} #{path.basename} to #{dest_path}
      MSG

      FileUtils.mkdir_p(dest_path.dirname, noop: config.dry_run)

      if File.exist?(tempfile)
        FileUtils.mv(tempfile, dest_path, noop: config.dry_run)
      else
        FileUtils.cp(path, dest_path, noop: config.dry_run)
      end

      FileUtils.chmod(0644, dest_path, noop: config.dry_run)
      update_exif_tags(dest_path.realdirpath) if !config.dry_run
    end
  end.compact.map(&:join).then do |threads|
    # e.g.: with --library-web only, raw files are skipped, so DON'T DELETE!
    FileUtils.rm(path, noop: threads.empty? || config.dry_run || config.keep)
  end
end