Skip to content

Commit

Permalink
Enable Trix to upload files to S3
Browse files Browse the repository at this point in the history
  • Loading branch information
thutterer committed May 23, 2020
1 parent d559f5d commit 0fdfb3c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
51 changes: 51 additions & 0 deletions app/assets/javascripts/bongo/attachments.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
(function() {
var HOST = "<%= Bongo::Engine.routes.url_helpers.files_path %>"

addEventListener("trix-attachment-add", function(event) {
if (event.attachment.file) {
uploadFileAttachment(event.attachment)
}
})

function uploadFileAttachment(attachment) {
uploadFile(attachment.file, setProgress, setAttributes)

function setProgress(progress) {
attachment.setUploadProgress(progress)
}

function setAttributes(attributes) {
attachment.setAttributes(attributes)
}
}

function uploadFile(file, progressCallback, successCallback) {
var formData = createFormData(file)
var xhr = new XMLHttpRequest()

xhr.open("POST", HOST, true)

xhr.upload.addEventListener("progress", function(event) {
var progress = event.loaded / event.total * 100
progressCallback(progress)
})

xhr.addEventListener("load", function(event) {
if (xhr.status == 200) {
var attributes = {
url: xhr.response
}
successCallback(attributes)
}
})

xhr.send(formData)
}

function createFormData(file) {
var data = new FormData()
data.append("Content-Type", file.type)
data.append("file", file)
return data
}
})();
22 changes: 22 additions & 0 deletions app/controllers/bongo/files_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require_dependency "bongo/application_controller"

require "aws-sdk-s3"

module Bongo
class FilesController < ApplicationController
before_action :authenticate_user!

skip_before_action :verify_authenticity_token

def create
extension = File.extname(params[:file].original_filename)
s3 = Aws::S3::Resource.new
obj = s3.bucket(ENV["AWS_S3_BUCKET"]).object(SecureRandom.uuid + extension)
obj.upload_file(params[:file])

respond_to do |format|
format.json { render json: obj.public_url }
end
end
end
end
1 change: 1 addition & 0 deletions bongo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ Gem::Specification.new do |spec|

spec.add_runtime_dependency "rails", "~> 6.0.3"
spec.add_runtime_dependency "mongoid", "~> 7.0.5"
spec.add_runtime_dependency "aws-sdk-s3", "~> 1"
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Bongo::Engine.routes.draw do
root to: "articles#index"
resources :articles
resources :files, only: :create
end
2 changes: 1 addition & 1 deletion lib/bongo/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Bongo
VERSION = '0.0.3'
VERSION = '0.0.4'
end

0 comments on commit 0fdfb3c

Please sign in to comment.