delete old gems and upgrade aws/paperclip (#676)

* remove old gems from gemfile, upgrade aws/paperclip

* update paperclip config

* upload screenshots as a blob instead of base64 to maps controller
This commit is contained in:
Devin Howard 2016-10-03 06:29:35 +08:00 committed by Connor Turland
parent 7ec9feff80
commit 87228a9631
11 changed files with 74 additions and 143 deletions

View file

@ -14,6 +14,7 @@ export SECRET_KEY_BASE='267c8a84f63963282f45bc3010eaddf027abfab58fc759d6e239c800
# # you can safely leave these blank, unless you're deploying an instance, in # # you can safely leave these blank, unless you're deploying an instance, in
# # which case you'll need to set them up # # which case you'll need to set them up
# #
# export S3_REGION
# export S3_BUCKET_NAME # export S3_BUCKET_NAME
# export AWS_ACCESS_KEY_ID # export AWS_ACCESS_KEY_ID
# export AWS_SECRET_ACCESS_KEY # export AWS_SECRET_ACCESS_KEY

26
Gemfile
View file

@ -5,44 +5,34 @@ ruby '2.3.0'
gem 'rails', '~> 5.0.0' gem 'rails', '~> 5.0.0'
gem 'active_model_serializers' gem 'active_model_serializers'
gem 'aws-sdk', '< 2.0' gem 'aws-sdk'
gem 'best_in_place' gem 'best_in_place'
gem 'delayed_job' gem 'delayed_job'
gem 'delayed_job_active_record' gem 'delayed_job_active_record'
gem 'devise' gem 'devise'
gem 'doorkeeper', '~> 4.0.0.rc4' gem 'doorkeeper'
gem 'dotenv-rails' gem 'dotenv-rails'
gem 'exception_notification' gem 'exception_notification'
gem 'formtastic'
gem 'formula'
gem 'httparty' gem 'httparty'
gem 'json' gem 'json'
gem 'kaminari' gem 'kaminari'
gem 'paperclip', '~> 4.3.6' gem 'paperclip'
gem 'pg' gem 'pg'
gem 'pundit' gem 'pundit'
gem 'pundit_extra' gem 'pundit_extra'
gem 'rack-cors'
gem 'rack-attack' gem 'rack-attack'
gem 'rack-cors'
gem 'redis' gem 'redis'
gem 'slack-notifier' gem 'slack-notifier'
gem 'snorlax' gem 'snorlax'
gem 'uservoice-ruby' gem 'uservoice-ruby'
# asset stuff
gem 'coffee-rails'
gem 'jquery-rails' gem 'jquery-rails'
gem 'jquery-ui-rails' gem 'jquery-ui-rails'
gem 'jbuilder' gem 'sass-rails'
gem 'rails3-jquery-autocomplete' gem 'uglifier'
group :assets do
gem 'coffee-rails'
gem 'sass-rails'
gem 'uglifier'
end
group :production do
gem 'rails_12factor'
end
group :test do group :test do
gem 'factory_girl_rails' gem 'factory_girl_rails'

View file

@ -46,11 +46,12 @@ GEM
addressable (2.3.8) addressable (2.3.8)
arel (7.1.2) arel (7.1.2)
ast (2.3.0) ast (2.3.0)
aws-sdk (1.66.0) aws-sdk (2.6.3)
aws-sdk-v1 (= 1.66.0) aws-sdk-resources (= 2.6.3)
aws-sdk-v1 (1.66.0) aws-sdk-core (2.6.3)
json (~> 1.4) jmespath (~> 1.0)
nokogiri (>= 1.4.4) aws-sdk-resources (2.6.3)
aws-sdk-core (= 2.6.3)
bcrypt (3.1.11) bcrypt (3.1.11)
best_in_place (3.1.0) best_in_place (3.1.0)
actionpack (>= 3.2) actionpack (>= 3.2)
@ -91,7 +92,7 @@ GEM
warden (~> 1.2.3) warden (~> 1.2.3)
diff-lcs (1.2.5) diff-lcs (1.2.5)
docile (1.1.5) docile (1.1.5)
doorkeeper (4.0.0) doorkeeper (4.2.0)
railties (>= 4.2) railties (>= 4.2)
dotenv (2.1.1) dotenv (2.1.1)
dotenv-rails (2.1.1) dotenv-rails (2.1.1)
@ -108,18 +109,12 @@ GEM
factory_girl_rails (4.7.0) factory_girl_rails (4.7.0)
factory_girl (~> 4.7.0) factory_girl (~> 4.7.0)
railties (>= 3.0.0) railties (>= 3.0.0)
formtastic (3.1.4)
actionpack (>= 3.2.13)
formula (1.1.1)
rails (> 3.0.0)
globalid (0.3.7) globalid (0.3.7)
activesupport (>= 4.1.0) activesupport (>= 4.1.0)
httparty (0.14.0) httparty (0.14.0)
multi_xml (>= 0.5.2) multi_xml (>= 0.5.2)
i18n (0.7.0) i18n (0.7.0)
jbuilder (2.6.0) jmespath (1.3.1)
activesupport (>= 3.0.0, < 5.1)
multi_json (~> 1.2)
jquery-rails (4.2.1) jquery-rails (4.2.1)
rails-dom-testing (>= 1, < 3) rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0) railties (>= 4.2.0)
@ -142,10 +137,9 @@ GEM
mime-types (3.1) mime-types (3.1)
mime-types-data (~> 3.2015) mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521) mime-types-data (3.2016.0521)
mimemagic (0.3.0) mimemagic (0.3.2)
mini_portile2 (2.1.0) mini_portile2 (2.1.0)
minitest (5.9.0) minitest (5.9.1)
multi_json (1.12.1)
multi_xml (0.5.5) multi_xml (0.5.5)
nio4r (1.2.1) nio4r (1.2.1)
nokogiri (1.6.8) nokogiri (1.6.8)
@ -153,12 +147,12 @@ GEM
pkg-config (~> 1.1.7) pkg-config (~> 1.1.7)
oauth (0.5.1) oauth (0.5.1)
orm_adapter (0.5.0) orm_adapter (0.5.0)
paperclip (4.3.7) paperclip (5.1.0)
activemodel (>= 3.2.0) activemodel (>= 4.2.0)
activesupport (>= 3.2.0) activesupport (>= 4.2.0)
cocaine (~> 0.5.5) cocaine (~> 0.5.5)
mime-types mime-types
mimemagic (= 0.3.0) mimemagic (~> 0.3.0)
parser (2.3.1.4) parser (2.3.1.4)
ast (~> 2.2) ast (~> 2.2)
pg (0.19.0) pg (0.19.0)
@ -199,13 +193,6 @@ GEM
nokogiri (~> 1.6.0) nokogiri (~> 1.6.0)
rails-html-sanitizer (1.0.3) rails-html-sanitizer (1.0.3)
loofah (~> 2.0) loofah (~> 2.0)
rails3-jquery-autocomplete (1.0.15)
rails (>= 3.2)
rails_12factor (0.0.3)
rails_serve_static_assets
rails_stdout_logging
rails_serve_static_assets (0.0.5)
rails_stdout_logging (0.0.5)
railties (5.0.0.1) railties (5.0.0.1)
actionpack (= 5.0.0.1) actionpack (= 5.0.0.1)
activesupport (= 5.0.0.1) activesupport (= 5.0.0.1)
@ -290,7 +277,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
active_model_serializers active_model_serializers
aws-sdk (< 2.0) aws-sdk
best_in_place best_in_place
better_errors better_errors
binding_of_caller binding_of_caller
@ -299,20 +286,17 @@ DEPENDENCIES
delayed_job delayed_job
delayed_job_active_record delayed_job_active_record
devise devise
doorkeeper (~> 4.0.0.rc4) doorkeeper
dotenv-rails dotenv-rails
exception_notification exception_notification
factory_girl_rails factory_girl_rails
formtastic
formula
httparty httparty
jbuilder
jquery-rails jquery-rails
jquery-ui-rails jquery-ui-rails
json json
json-schema json-schema
kaminari kaminari
paperclip (~> 4.3.6) paperclip
pg pg
pry-byebug pry-byebug
pry-rails pry-rails
@ -321,8 +305,6 @@ DEPENDENCIES
rack-attack rack-attack
rack-cors rack-cors
rails (~> 5.0.0) rails (~> 5.0.0)
rails3-jquery-autocomplete
rails_12factor
redis redis
rspec-rails rspec-rails
rubocop rubocop
@ -339,4 +321,4 @@ RUBY VERSION
ruby 2.3.0p0 ruby 2.3.0p0
BUNDLED WITH BUNDLED WITH
1.12.5 1.13.2

View file

@ -1,13 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
class MapsController < ApplicationController class MapsController < ApplicationController
before_action :require_user, only: [:create, :update, :destroy, :access, :events, before_action :require_user, only: [:create, :update, :destroy, :access, :events]
:screenshot]
before_action :set_map, only: [:show, :update, :destroy, :access, :contains, before_action :set_map, only: [:show, :update, :destroy, :access, :contains,
:events, :export, :screenshot] :events, :export]
after_action :verify_authorized after_action :verify_authorized
autocomplete :map, :name, full: true, extra_data: [:user_id]
# GET maps/:id # GET maps/:id
def show def show
respond_to do |format| respond_to do |format|
@ -136,17 +133,6 @@ class MapsController < ApplicationController
end end
end end
# POST maps/:id/upload_screenshot
def screenshot
@map.base64_screenshot(params[:encoded_image])
if @map.save
render json: { message: 'Successfully uploaded the map screenshot.' }
else
render json: { message: 'Failed to upload image.' }
end
end
private private
def set_map def set_map
@ -159,7 +145,7 @@ class MapsController < ApplicationController
end end
def update_map_params def update_map_params
params.require(:map).permit(:id, :name, :arranged, :desc, :permission) params.require(:map).permit(:id, :name, :arranged, :desc, :permission, :screenshot)
end end
def create_topics! def create_topics!

View file

@ -95,21 +95,6 @@ class Map < ApplicationRecord
json json
end end
def decode_base64(imgBase64)
decoded_data = Base64.decode64(imgBase64)
data = StringIO.new(decoded_data)
data.class_eval do
attr_accessor :content_type, :original_filename
end
data.content_type = 'image/png'
data.original_filename = File.basename('map-' + id.to_s + '-screenshot.png')
self.screenshot = data
save
end
# user param helps determine what records are visible # user param helps determine what records are visible
def contains(user) def contains(user)
{ {
@ -144,14 +129,4 @@ class Map < ApplicationRecord
end end
removed.compact removed.compact
end end
def base64_screenshot(encoded_image)
png = Base64.decode64(encoded_image['data:image/png;base64,'.length..-1])
StringIO.open(png) do |data|
data.class.class_eval { attr_accessor :original_filename, :content_type }
data.original_filename = 'map-' + @map.id.to_s + '-screenshot.png'
data.content_type = 'image/png'
@map.screenshot = data
end
end
end end

View file

@ -60,8 +60,4 @@ class MapPolicy < ApplicationPolicy
def unstar? def unstar?
user.present? user.present?
end end
def screenshot?
update?
end
end end

View file

@ -43,5 +43,17 @@ module Metamaps
# pundit errors return 403 FORBIDDEN # pundit errors return 403 FORBIDDEN
config.action_dispatch.rescue_responses['Pundit::NotAuthorizedError'] = :forbidden config.action_dispatch.rescue_responses['Pundit::NotAuthorizedError'] = :forbidden
# S3 file storage
config.paperclip_defaults = {
storage: :s3,
s3_protocol: 'https',
s3_region: ENV['S3_REGION'],
s3_credentials: {
bucket: ENV['S3_BUCKET_NAME'],
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
}
}
end end
end end

View file

@ -14,17 +14,6 @@ Rails.application.configure do
config.consider_all_requests_local = true config.consider_all_requests_local = true
config.action_controller.perform_caching = false config.action_controller.perform_caching = false
# S3 file storage
config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV['S3_BUCKET_NAME'],
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
},
s3_protocol: 'https'
}
config.action_mailer.delivery_method = :smtp config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { config.action_mailer.smtp_settings = {
address: ENV['SMTP_SERVER'], address: ENV['SMTP_SERVER'],

View file

@ -5,6 +5,11 @@ Rails.application.configure do
config.log_level = :warn config.log_level = :warn
config.eager_load = true config.eager_load = true
# 12 factor: log to stdout
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
# Code is not reloaded between requests # Code is not reloaded between requests
config.cache_classes = true config.cache_classes = true
@ -13,24 +18,13 @@ Rails.application.configure do
config.action_controller.perform_caching = true config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this) # Disable Rails's static asset server (Apache or nginx will already do this)
config.public_file_server.enabled = false config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
# Don't fallback to assets pipeline if a precompiled asset is missed # Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false config.assets.compile = false
config.assets.js_compressor = :uglifier config.assets.js_compressor = :uglifier
# S3 file storage
config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV['S3_BUCKET_NAME'],
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
},
s3_protocol: 'https'
}
config.action_mailer.delivery_method = :smtp config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { config.action_mailer.smtp_settings = {
address: ENV['SMTP_SERVER'], address: ENV['SMTP_SERVER'],

View file

@ -19,7 +19,6 @@ Metamaps::Application.routes.draw do
get :export get :export
post 'events/:event', action: :events post 'events/:event', action: :events
get :contains get :contains
post :upload_screenshot, action: :screenshot
post :access, default: { format: :json } post :access, default: { format: :json }
post :star, to: 'stars#create', defaults: { format: :json } post :star, to: 'stars#create', defaults: { format: :json }
post :unstar, to: 'stars#destroy', defaults: { format: :json } post :unstar, to: 'stars#destroy', defaults: { format: :json }

View file

@ -1,5 +1,7 @@
/* global Metamaps, $ */ /* global Metamaps, $ */
import outdent from 'outdent'
import Active from '../Active' import Active from '../Active'
import AutoLayout from '../AutoLayout' import AutoLayout from '../AutoLayout'
import Create from '../Create' import Create from '../Create'
@ -323,9 +325,7 @@ const Map = {
node.visited = !T node.visited = !T
}) })
var imageData = { var imageData = canvas.canvas.toDataURL()
encoded_image: canvas.canvas.toDataURL()
}
var map = Active.Map var map = Active.Map
@ -341,24 +341,31 @@ const Map = {
} }
today = mm + '/' + dd + '/' + yyyy today = mm + '/' + dd + '/' + yyyy
var mapName = map.get('name').split(' ').join([separator = '-']) var mapName = map.get('name').split(' ').join(['-'])
var downloadMessage = '' const filename = `metamap-${map.id}-${mapName}-${today}.png`
downloadMessage += 'Captured map screenshot! '
downloadMessage += "<a href='" + imageData.encoded_image + "' " var downloadMessage = outdent`
downloadMessage += "download='metamap-" + map.id + '-' + mapName + '-' + today + ".png'>DOWNLOAD</a>" Captured map screenshot!
<a href="${imageData.encodedImage}" download="${filename}">DOWNLOAD</a>`
GlobalUI.notifyUser(downloadMessage) GlobalUI.notifyUser(downloadMessage)
$.ajax({ canvas.canvas.toBlob(imageBlob => {
type: 'POST', const formData = new window.FormData();
dataType: 'json', formData.append('map[screenshot]', imageBlob, filename)
url: '/maps/' + Active.Map.id + '/upload_screenshot', $.ajax({
data: imageData, type: 'PATCH',
success: function (data) { dataType: 'json',
console.log('successfully uploaded map screenshot') url: `/maps/${map.id}`,
}, data: formData,
error: function () { processData: false,
console.log('failed to save map screenshot') contentType: false,
} success: function (data) {
console.log('successfully uploaded map screenshot')
},
error: function () {
console.log('failed to save map screenshot')
}
})
}) })
} }
} }