Sha256: 4b136d5a18f1b9ad1eb0d38167e71ecc7737ca7bf033f1ba6d8afd2d1a6e73d3

Contents?: true

Size: 1.71 KB

Versions: 14

Compression:

Stored size: 1.71 KB

Contents

require 'fspath'
require 'image_optim/image_meta'

class ImageOptim
  # FSPath with additional helpful methods
  class Path < FSPath
    NULL = if defined?(IO::NULL)
      IO::NULL
    else
      %w[/dev/null NUL: NUL nul NIL: NL:].find{ |dev| File.exist?(dev) }
    end

    # Get temp path for this file with same extension
    def temp_path(*args, &block)
      ext = extname
      self.class.temp_file_path([basename(ext).to_s, ext], *args, &block)
    end

    # Copy file to dst, optionally preserving attributes
    #
    # See FileUtils.copy_file
    def copy(dst, preserve = false)
      FileUtils.copy_file(self, dst, preserve)
    end

    # Move file to dst: rename on same device, copy and unlink original
    # otherwise
    #
    # See FileUtils.mv
    def move(dst)
      FileUtils.move(self, dst)
    end

    # Copy metadata: uid, gid, mode, optionally atime and mtime
    #
    # Adapted from FileUtils::Entry_#copy_metadata by Minero Aoki
    def copy_metadata(dst, time = false)
      stat = lstat
      dst.utime(stat.atime, stat.mtime) if time
      begin
        dst.chown(stat.uid, stat.gid)
      rescue Errno::EPERM
        dst.chmod(stat.mode & 0o1777)
      else
        dst.chmod(stat.mode)
      end
    end

    # Atomic replace dst with self
    def replace(dst)
      dst = self.class.new(dst)
      dst.temp_path(dst.dirname) do |temp|
        move(temp)
        dst.copy_metadata(temp)
        temp.rename(dst.to_s)
      end
    end

    # Get format using ImageSize
    def image_format
      ImageMeta.format_for_path(self)
    end

    # Returns path if it is already an instance of this class otherwise new
    # instance
    def self.convert(path)
      path.is_a?(self) ? path : new(path)
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
image_optim-0.26.3 lib/image_optim/path.rb
image_optim-0.26.2 lib/image_optim/path.rb
discourse_image_optim-0.26.2 lib/image_optim/path.rb
discourse_image_optim-0.26.1 lib/image_optim/path.rb
image_optim-0.26.1 lib/image_optim/path.rb
image_optim-0.26.0 lib/image_optim/path.rb
image_optim-0.25.0 lib/image_optim/path.rb
discourse_image_optim-0.24.5 lib/image_optim/path.rb
image_optim-0.24.3 lib/image_optim/path.rb
discourse_image_optim-0.24.4 lib/image_optim/path.rb
image_optim-0.24.2 lib/image_optim/path.rb
image_optim-0.24.1 lib/image_optim/path.rb
image_optim-0.24.0 lib/image_optim/path.rb
image_optim-0.23.0 lib/image_optim/path.rb