Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add S3 upload field to video edit form #22187

Merged
merged 7 commits into from May 10, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions dashboard/app/controllers/videos_controller.rb
Expand Up @@ -37,10 +37,12 @@ def new
end

def edit
@s3_metadata = Video.s3_metadata(@video.download)
end

def create
@video = Video.new(video_params)
filename = upload_to_s3
@video = Video.new(video_params.merge(download: "https://videos.code.org/#{filename}"))

if @video.save
Video.merge_and_write_i18n({@video.key => i18n_params[:title]})
Expand All @@ -55,7 +57,9 @@ def create
# PATCH/PUT /videos/1
# PATCH/PUT /videos/1.json
def update
if @video.update(video_params)
filename = upload_to_s3

if @video.update(video_params.merge(download: "https://videos.code.org/#{filename}"))
Video.merge_and_write_i18n({@video.key => i18n_params[:title]})
Video.merge_and_write_attributes(@video.key, @video.youtube_code, @video.download)

Expand All @@ -67,6 +71,19 @@ def update

private

def upload_to_s3
raise 'Expected a video/mp4 (.mp4) file' unless video_params[:download].content_type == 'video/mp4'

filename = File.basename(video_params[:download].original_filename).parameterize + '.mp4'
AWS::S3.upload_to_bucket(
'videos.code.org',
"levelbuilder/#{filename}",
video_params[:download],
acl: 'public-read',
no_random: true,
)
end

def allow_iframe
response.headers['X-Frame-Options'] = 'ALLOWALL'
end
Expand Down
8 changes: 8 additions & 0 deletions dashboard/app/models/video.rb
Expand Up @@ -17,6 +17,7 @@ class Video < ActiveRecord::Base
default_scope {order(:key)}

validates_uniqueness_of :key
validates_presence_of :download

before_save :fetch_thumbnail

Expand Down Expand Up @@ -74,6 +75,13 @@ def self.youtube_base_url
'https://www.youtube.com'
end

def self.s3_metadata(url)
key = url.sub(/^https?:\/\/videos.code.org\//, '')
AWS::S3.create_client.head_object(bucket: 'videos.code.org', key: key)
rescue Aws::S3::Errors::NoSuchKey
{}
end

def fetch_thumbnail
return unless Rails.application.config.levelbuilder_mode

Expand Down
11 changes: 10 additions & 1 deletion dashboard/app/views/videos/_form.html.haml
Expand Up @@ -21,7 +21,16 @@
= f.text_field :youtube_code
.field
= f.label 'S3 download link'
- if @s3_metadata.present?
%p
= link_to @video.download, @video.download
%p
Size:
= number_to_human_size(@s3_metadata.content_length)
%p{title: @s3_metadata.last_modified.to_formatted_s(:long_ordinal)}
Last modified:
= time_ago_in_words(@s3_metadata.last_modified) + ' ago'
%br/
= f.text_field :download
= f.file_field :download, style: 'line-height: initial'
.actions
= f.submit