-
Notifications
You must be signed in to change notification settings - Fork 49
/
videos.rb
153 lines (133 loc) · 3.95 KB
/
videos.rb
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
148
149
150
151
152
153
class Videos < Application
before :require_login, :only => [:index, :show, :destroy, :new, :create, :add_to_queue]
before :set_video, :only => [:show, :destroy, :add_to_queue]
before :set_video_with_nice_errors, :only => [:upload_form, :done, :state]
def index
provides :html, :xml, :yaml
@videos = Video.all
display @videos
end
def show
provides :html, :xml, :yaml
case content_type
when :html
# TODO: use proper auth method
@user = User.find(session[:user_key]) if session[:user_key]
if @user
if @video.status == "original"
render :show_parent
else
render :show_encoding
end
else
redirect("/login")
end
when :xml
@video.show_response.to_simple_xml
when :yaml
@video.show_response.to_yaml
end
end
# Use: HQ
# Only used in the admin side to post to create and then forward to the form
# where the video is uploaded
def new
render :layout => :simple
end
# Use: HQ
def destroy
@video.obliterate!
redirect "/videos"
end
# Use: HQ, API
def create
provides :html, :xml, :yaml
@video = Video.create_empty
Merb.logger.info "#{@video.key}: Created video"
case content_type
when :html
redirect url(:upload_form_video, @video.key)
when :xml
headers.merge!({'Location'=> "/videos/#{@video.key}"})
@video.create_response.to_simple_xml
when :yaml
headers.merge!({'Location'=> "/videos/#{@video.key}"})
@video.create_response.to_yaml
end
end
# Use: HQ, API, iframe upload
def upload_form
render :layout => :uploader
end
# Use: HQ, http/iframe upload
def upload
begin
@video = Video.find(params[:id])
@video.initial_processing(params[:file])
rescue Amazon::SDB::RecordNotFoundError
# No empty video object exists
self.status = 404
render_error($!.to_s.gsub(/Amazon::SDB::/,""))
rescue Video::NotValid
# Video object is not empty. Likely a video has already been uploaded.
self.status = 404
render_error($!.to_s.gsub(/Video::/,""))
rescue Video::VideoError
# Generic Video error
self.status = 500
render_error($!.to_s.gsub(/Video::/,""))
rescue => e
# Other error
self.status = 500
render_error("InternalServerError", e)
else
redirect_url = @video.upload_redirect_url
render_then_call(iframe_params(:location => redirect_url)) do
@video.finish_processing_and_queue_encodings
end
end
end
# Default upload_redirect_url (set in panda_init.rb) goes here.
def done
render :layout => :uploader
end
# TODO: Why do we need this method?
def add_to_queue
@video.add_to_queue
redirect "/videos/#{@video.key}"
end
private
def render_error(msg, exception = nil)
Merb.logger.error "#{params[:id]}: (500 returned to client) #{msg}" + (exception ? "#{exception}\n#{exception.backtrace.join("\n")}" : '')
case content_type
when :html
if params[:iframe] == "true"
iframe_params(:error => msg)
else
@exception = msg
render(:template => "exceptions/video_exception", :layout => false) # TODO: Why is :action setting 404 instead of 500?!?!
end
when :xml
{:error => msg}.to_simple_xml
when :yaml
{:error => msg}.to_yaml
end
end
def set_video
# Throws Amazon::SDB::RecordNotFoundError if video cannot be found
@video = Video.find(params[:id])
end
def set_video_with_nice_errors
begin
@video = Video.find(params[:id])
rescue Amazon::SDB::RecordNotFoundError
self.status = 404
throw :halt, render_error($!.to_s.gsub(/Amazon::SDB::/,""))
end
end
# Textarea hack to get around the fact that the form is submitted with a
# hidden iframe and thus the response is rendered in the iframe.
def iframe_params(options)
"<textarea>" + options.to_json + "</textarea>"
end
end