Sha256: 32097d4e8871d314a4b967a1183f4195836513dd54a68b40ecf35d91d59965a1

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

require 'tempfile'
require 'ostruct'
require 'qiniu-rs'
require 'erb'

module BlastOff
  module Services
    class Qiniu
      def initialize(ipa_file_path:nil, access_key:'', secret_key:'', bucket:'')
        @ipa_file_path = ipa_file_path
        @ipa_file = ::IpaReader::IpaFile.new(ipa_file_path)
        @bucket = bucket

        ::Qiniu::RS.establish_connection!(
          enable_debug: false,
          block_size: File.size(@ipa_file_path) + 100,
          access_key: access_key,
          secret_key: secret_key
        )
      end

      def distribute
        upload_string(
          template.manifest_plist("#{base_url}/#{@ipa_file.name}.ipa"),
          'manifest.plist',
          'application/octet-stream'
        )
        upload_string(
          template.html("#{base_url}/manifest.plist"),
          'index.html',
          'text/html'
        )
        upload_file(
          @ipa_file_path,
          "#{@ipa_file.name}.ipa",
          'application/octet-stream'
        )

        "#{base_url}/index.html"
      end

      private

      def base_path
        "#{@ipa_file.name}/#{@ipa_file.version}"
      end

      def base_url
        "http://#{@bucket}.qiniudn.com/#{base_path}"
      end

      def upload_string(string, name, mime_type)
        file = Tempfile.new(name)
        begin
          file.write(string)
          file.rewind
          upload_file(file.path, name, mime_type)
        ensure
          file.close
          file.unlink
        end
      end

      def upload_file(filepath, name, mime_type)
        key = "#{base_path}/#{name}"
        upload_token = ::Qiniu::RS.generate_upload_token(
          scope: "#{@bucket}:#{key}"
        )
        ::Qiniu::RS.upload_file(
          uptoken: upload_token,
          file: filepath,
          mime_type: mime_type,
          bucket: @bucket,
          key: key
        )
      end

      def template
        @template ||= Template.new(@ipa_file)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
blast_off-0.4.0 lib/blast_off/services/qiniu.rb