-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |