fixes for screenshot worker

This commit is contained in:
Kyle Drake 2014-08-20 13:48:20 -07:00
parent 0aca6b3328
commit fbf78f04d6
8 changed files with 69 additions and 2 deletions

View file

@ -61,6 +61,7 @@ group :test do
gem 'phantomjs', require: 'phantomjs/poltergeist'
gem 'capybara_minitest_spec'
gem 'rack_session_access', require: nil
gem 'webmock', require: nil
platform :mri do
gem 'simplecov', require: nil

View file

@ -45,6 +45,8 @@ GEM
coderay (1.1.0)
columnize (0.3.6)
connection_pool (2.0.0)
crack (0.4.2)
safe_yaml (~> 1.0.0)
debugger (1.6.6)
columnize (>= 0.3.1)
debugger-linecache (~> 1.2.0)
@ -144,6 +146,7 @@ GEM
mime-types (>= 1.16)
retriable (1.4.1)
rmagick (2.13.2)
safe_yaml (1.0.1)
sass (3.3.8)
screencap (0.1.1)
phantomjs
@ -191,6 +194,9 @@ GEM
rack
raindrops (~> 0.7)
uuidtools (2.1.4)
webmock (1.17.4)
addressable (>= 2.2.7)
crack (>= 0.3.2)
websocket-driver (0.3.4)
xpath (2.0.0)
nokogiri (~> 1.3)
@ -244,4 +250,5 @@ DEPENDENCIES
sinatra-xsendfile
stripe!
tilt
webmock
zipruby

View file

@ -366,7 +366,7 @@ class Site < Sequel::Model
ext = File.extname(path).gsub(/^./, '')
if ext.match HTML_REGEX
ScreenshotWorker.perform_async values[:username], path
ScreenshotWorker.perform_async values[:username], relative_path
elsif ext.match IMAGE_REGEX
ThumbnailWorker.perform_async values[:username], relative_path
end

View file

@ -17,6 +17,9 @@ Bundler.require :test
#require 'minitest/pride'
require 'minitest/autorun'
require 'webmock'
include WebMock::API
require 'webmock/minitest'
require 'sidekiq/testing'
Sinatra::Application.configure do |app|

8
tests/files/index.html Normal file
View file

@ -0,0 +1,8 @@
<html>
<head>
<title>Hello?</title>
</head>
<body>
<h1>Hi there!</h1>
</body>
</html>

View file

@ -9,6 +9,24 @@ end
describe 'site_files' do
describe 'upload' do
it 'succeeds with index.html file' do
site = Fabricate :site
PurgeCacheWorker.jobs.clear
ScreenshotWorker.jobs.clear
post '/site_files/upload', {
'files[]' => Rack::Test::UploadedFile.new('./tests/files/index.html', 'text/html'),
'csrf_token' => 'abcd'
}, {'rack.session' => { 'id' => site.id, '_csrf_token' => 'abcd' }}
last_response.body.must_match /successfully uploaded/i
File.exists?(site.files_path('index.html')).must_equal true
args = ScreenshotWorker.jobs.first['args']
args.first.must_equal site.username
args.last.must_equal 'index.html'
end
it 'succeeds with valid file' do
site = Fabricate :site
PurgeCacheWorker.jobs.clear

View file

@ -0,0 +1,28 @@
require_relative '../environment.rb'
describe ScreenshotWorker do
it 'saves a screenshot for a root html file' do
worker = ScreenshotWorker.new
worker.perform 'kyledrake', 'index.html'
site = Fabricate :site
Site::SCREENSHOT_RESOLUTIONS.each do |r|
File.exists?(File.join(Site::SCREENSHOTS_ROOT, 'kyledrake', "index.html.#{r}.jpg")).must_equal true
site.screenshot_url('index.html', r).must_equal(
File.join(Site::SCREENSHOTS_URL_ROOT, site.username, "index.html.#{r}.jpg")
)
end
end
it 'saves a screenshot for a path html file' do
worker = ScreenshotWorker.new
worker.perform 'kyledrake', 'derpie/derp/index.html'
site = Fabricate :site
Site::SCREENSHOT_RESOLUTIONS.each do |r|
File.exists?(File.join(Site::SCREENSHOTS_ROOT, 'kyledrake', "derpie/derp/index.html.#{r}.jpg")).must_equal true
site.screenshot_url('derpie/derp/index.html', r).must_equal(
File.join(Site::SCREENSHOTS_URL_ROOT, site.username, "derpie/derp/index.html.#{r}.jpg")
)
end
end
end

View file

@ -80,7 +80,9 @@ class ScreenshotWorker
img = img_list.reverse.flatten_images
user_screenshots_path = File.join SCREENSHOTS_PATH, username
FileUtils.mkdir_p user_screenshots_path
screenshot_path = File.join user_screenshots_path, File.dirname(path)
FileUtils.mkdir_p screenshot_path unless Dir.exists?(screenshot_path)
Site::SCREENSHOT_RESOLUTIONS.each do |res|
img.scale(*res.split('x').collect {|r| r.to_i}).write(File.join(user_screenshots_path, "#{path}.#{res}.jpg")) {