upr.git  about / heads / tags
Upload Progress for Rack
blob 871b49ffd9e2b6a41946c2a6b2299aaf0d348097 1200 bytes (raw)
$ git show HEAD:examples/rails_app-2.3.4/app/models/upr_status.rb	# shows this blob on the CLI

 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
 
class UprStatus < ActiveRecord::Base

  class << self
    def read(upid)
      find_by_upid(upid)
    end

    def start(upid, length)
      # this must be a find_or_create_by since some users have twitchy
      # fingers and hit the upload button prematurely
      find_or_create_by_upid(upid) do |x|
        x.length = length
        x.time = Time.now.to_i
        x.seen = 0
      end
    end

    def incr(upid, nr)
      transaction do
        if rv = find_by_upid(upid)
          rv.time = Time.now.to_i
          rv.seen += nr if rv.seen >= 0
          rv.save
          rv
        end
      end
    end

    def error!(upid)
      transaction do
        if rv = find_by_upid(upid)
          rv.time = Time.now.to_i
          rv.seen = -1
          rv.save
          rv
        end
      end
    end

    def finish(upid)
      transaction do
        if rv = find_by_upid(upid)
          rv.time = Time.now.to_i
          rv.length ||= rv.seen
          rv.seen = rv.length
          rv.save
          rv
        end
      end
    end

    def gc
      cutoff = Time.now.to_i - Upr::Monitor::OPT[:expires_in]
      delete_all "time < #{cutoff}"
    end
  end

  include Upr::StatusMethods
end

git clone https://yhbt.net/upr.git