upload screenshots as a blob instead of base64 to maps controller
This commit is contained in:
parent
7ebf73db79
commit
0c710b0787
5 changed files with 29 additions and 66 deletions
|
@ -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!
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -60,8 +60,4 @@ class MapPolicy < ApplicationPolicy
|
||||||
def unstar?
|
def unstar?
|
||||||
user.present?
|
user.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def screenshot?
|
|
||||||
update?
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -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 }
|
||||||
|
|
|
@ -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')
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue