forked from dsjoerg/ggtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplays_controller.rb
More file actions
147 lines (121 loc) · 4.44 KB
/
replays_controller.rb
File metadata and controls
147 lines (121 loc) · 4.44 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class ReplaysController < ApplicationController
before_filter :authenticate_user!, :only => [:index, :show]
respond_to :html, :json, :xml
# Drop is used for bulk/unassociated (anonymous) replay uploads
#
# Note: we're thinking about letting users claim their battle.net ids,
# which would let them claim replays uploaded here.
def drop
ggtracker_received_at = Time.now
# Sc2Gears Protocol Support
# https://sites.google.com/site/sc2gears/features/replay-sharing
# We also set access_token - check application_controller
if params[:fileName] && params[:fileContent]
content = Base64.decode64(params[:fileContent])
md5 = Digest::MD5.hexdigest(content)
file = StringIO.new(content)
file.class.class_eval { attr_accessor :original_filename, :content_type }
file.original_filename = params[:fileName]
params[:replays] = [file]
params[:mode] = 'sc2gears'
if params[:token].present? && Rails.application.secret('partner_uploaders').include?(params[:token])
# welcome GGTracker partner
params[:channel] = params[:token]
elsif current_user.nil? || !current_user.pro?
xml = Builder::XmlMarkup.new(indent: 2)
xml.instruct!
xml.uploadResult(docVersion: '1.0') {
xml.errorCode(403)
xml.message('Uploading from Sc2gears requires a GGTracker Pro subscription. See http://ggtracker.com/go_pro for more.')
xml.replayUrl('')
}
render(xml: xml.target!, status: 200) and return
else
# welcome GGTracker Pro subscriber
params[:channel] = "drop#{current_user.id}"
end
end
# Yes, for now, we'll just render text.
# TODO: decide on API approach and give us nice helpers such as error!
render(:text => 'NOK', :status => 406) and return if !params[:replays] || params[:replays].empty?
params[:replays].each do |file|
replay = Replay.new(
replay: file,
user: current_user,
progress: 0,
status: 'Waiting to process',
channel: params[:channel]
)
replay.save!
replay.process!(ggtracker_received_at)
# what happens next:
# ggtracker/app/models/replay.rb: response = ESDB::Replay.upload(md5)
# gg/lib/esdb/replay.rb: JSON.parse(RestClient.post(replay.url, :file => file, :access_token => ESDB.api_key))
# esdb/esdb/api/replays.rb: parser_id = Resque.enqueue_to(queuename, ESDB::Jobs::Sc2::Replay::PreParse, {
# ggpyjobs/ggtracker/jobs.py: replayDB = sc2reader_to_esdb.processReplay(StringIO(k.get_contents_as_string()))
#
end
case params[:mode].to_s.downcase
when 'sc2gears' then
# Using a custom builder to set the attribute on root, but sc2gears
# doesn't really care if it's there. The 200 status is mandatory however,
# 201 will result in failure..
xml = Builder::XmlMarkup.new(indent: 2)
xml.instruct!
xml.uploadResult(docVersion: '1.0') {
xml.errorCode(0)
xml.message('OK')
# We don't know the match id yet, so I've made matches/:id respond to
# the md5 if :id is non-numeric and redirect if a match exists.
xml.replayUrl(match_url(md5))
}
render(xml: xml.target!, status: 200)
else
render(text: 'WTF', status: 201)
end
end
def s3_drop
ggtracker_received_at = Time.now
replay = Replay.new(
replay_file_name: params[:file_name],
md5: params[:s3_key],
user: current_user,
progress: 0,
status: 'Waiting to process',
channel: params[:channel]
)
replay.save!
replay.process!(ggtracker_received_at)
render(text: 'OK', status: 201)
end
# FIXME: if we're providing more endpoints that also act like an API, give
# some thought to the overall implementation.
def index
params[:page] = params[:page].to_i if params[:page]
params.reverse_merge!({
offset: 0,
limit: 20,
page: 1
})
# Page overrides offset if present
if params[:page]
params[:offset] = (params[:page] - 1) * params[:limit]
end
@replays = current_user.replays.
order("updated_at desc").
limit(params[:limit] || 10).
offset(params[:offset] || 0)
# respond_with @replays
respond_to do |format|
format.json {
respond_with({
params: params,
collection: @replays
})
}
format.html
end
end
def show
end
end