Sha256: 68060aeb1595ccba9ef881813d91d9e58a9b5f6940deb83b1fdcc1ec018168b0

Contents?: true

Size: 1.58 KB

Versions: 36

Compression:

Stored size: 1.58 KB

Contents

require 'fileutils'

module Spaceship
  # a wrapper around the concept of file required to make uploads to DU
  class UploadFile
    attr_reader :file_path
    attr_reader :file_name
    attr_reader :file_size
    attr_reader :content_type
    attr_reader :bytes

    class << self
      def from_path(path)
        raise "Image must exists at path: #{path}" unless File.exist?(path)

        # md5 from original. keeping track of md5s allows to skip previously uploaded in deliver
        content_md5 = Spaceship::Utilities.md5digest(path)
        path = remove_alpha_channel(path) if File.extname(path).casecmp('.png').zero?

        content_type = Utilities.content_type(path)
        self.new(
          file_path: path,
          file_name: 'ftl_' + content_md5 + '_' + File.basename(path),
          file_size: File.size(path),
          content_type: content_type,
          bytes: File.read(path)
        )
      end

      # As things like screenshots and app icon shouldn't contain the alpha channel
      # This will copy the image into /tmp to remove the alpha channel there
      # That's done to not edit the original image
      def remove_alpha_channel(original)
        path = "/tmp/#{Digest::MD5.hexdigest(original)}.png"
        FileUtils.copy(original, path)
        `sips -s format bmp '#{path}' &> /dev/null ` # &> /dev/null since there is warning because of the extension
        `sips -s format png '#{path}'`
        return path
      end
    end

    private

    def initialize(args)
      args.each do |k, v|
        instance_variable_set("@#{k}", v) unless v.nil?
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
spaceship-0.36.0 lib/spaceship/du/upload_file.rb
spaceship-0.35.0 lib/spaceship/du/upload_file.rb
spaceship-0.34.3 lib/spaceship/du/upload_file.rb
spaceship-0.34.2 lib/spaceship/du/upload_file.rb
spaceship-0.34.1 lib/spaceship/du/upload_file.rb
spaceship-0.34.0 lib/spaceship/du/upload_file.rb
spaceship-0.33.0 lib/spaceship/du/upload_file.rb
spaceship-0.32.4 lib/spaceship/du/upload_file.rb
spaceship-0.32.3 lib/spaceship/du/upload_file.rb
spaceship-0.32.2 lib/spaceship/du/upload_file.rb
spaceship-0.32.1 lib/spaceship/du/upload_file.rb
spaceship-0.32.0 lib/spaceship/du/upload_file.rb
spaceship-0.31.10 lib/spaceship/du/upload_file.rb
spaceship-0.31.9 lib/spaceship/du/upload_file.rb
spaceship-0.31.8 lib/spaceship/du/upload_file.rb
spaceship-0.31.7 lib/spaceship/du/upload_file.rb
spaceship-0.31.6 lib/spaceship/du/upload_file.rb
spaceship-0.31.5 lib/spaceship/du/upload_file.rb
spaceship-0.31.4 lib/spaceship/du/upload_file.rb
spaceship-0.31.3 lib/spaceship/du/upload_file.rb