File: multipart.rb

package info (click to toggle)
ruby-aliyun-sdk 0.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 792 kB
  • sloc: ruby: 7,909; ansic: 204; makefile: 4
file content (74 lines) | stat: -rw-r--r-- 1,645 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
65
66
67
68
69
70
71
72
73
74
# -*- encoding: utf-8 -*-

require 'json'
require 'digest/md5'

module Aliyun
  module OSS

    ##
    # Multipart upload/download structures
    #
    module Multipart

      ##
      # A multipart transaction. Provide the basic checkpoint methods.
      #
      class Transaction < Common::Struct::Base

        attrs :id, :object, :bucket, :creation_time, :options

        def initialize(opts = {})
          super(opts)

          @mutex = Mutex.new
        end

        private
        # Persist transaction states to file
        def write_checkpoint(states, file)
          md5= Util.get_content_md5(states.to_json)

          @mutex.synchronize {
            File.open(file, 'wb') {
              |f| f.write(states.merge(md5: md5).to_json)
            }
          }
        end

        # Load transaction states from file
        def load_checkpoint(file)
          states = {}

          @mutex.synchronize {
            states = JSON.load(File.read(file))
          }
          states = Util.symbolize(states)
          md5 = states.delete(:md5)

          fail CheckpointBrokenError, "Missing MD5 in checkpoint." unless md5
          unless md5 == Util.get_content_md5(states.to_json)
            fail CheckpointBrokenError, "Unmatched checkpoint MD5."
          end

          states
        end

        def get_file_md5(file)
          Digest::MD5.file(file).to_s
        end

      end # Transaction

      ##
      # A part in a multipart uploading transaction
      #
      class Part < Common::Struct::Base

        attrs :number, :etag, :size, :last_modified

      end # Part

    end # Multipart
  end # OSS
end # Aliyun