Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File upload #226

Merged
merged 4 commits into from May 10, 2017

Conversation

Projects
None yet
3 participants
@morygonzalez
Copy link
Member

morygonzalez commented Jan 25, 2017

Currently only supports Amazon S3 for uploaded file storage.

demo gif:

file-upload

refs #15

@@ -1,5 +1,9 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

def fixture_path
File.expand_path(File.dirname(__FILE__) + '/../fixtures')

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@@ -1,5 +1,9 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

def fixture_path
File.expand_path(File.dirname(__FILE__) + '/../fixtures')

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


context 'With invalid params' do
it 'should be failure' do
put '/admin/file_upload', { bar: 'buzz' }

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Redundant curly braces around a hash parameter.


context 'With invalid params' do
it 'should be failure' do
put '/admin/file_upload', { bar: 'buzz' }

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Redundant curly braces around a hash parameter.

end

context 'With invalid params' do
it 'should be failure' do

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end

context 'With invalid params' do
it 'should be failure' do

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end
end

context 'With invalid params' do

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end
end

context 'With invalid params' do

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

s3_bucket_name: 'example',
}
follow_redirect!
last_response.body.should match(I18n.t('file_upload.successfully_updated'))

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Line is too long. [83/80]

s3_bucket_name: 'example',
}
follow_redirect!
last_response.body.should match(I18n.t('file_upload.successfully_updated'))

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Line is too long. [83/80]

aws_access_key_id: 'foo',
aws_secret_access_key: 'bar',
s3_region: 'ap-northeast-1',
s3_bucket_name: 'example',

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

aws_access_key_id: 'foo',
aws_secret_access_key: 'bar',
s3_region: 'ap-northeast-1',
s3_bucket_name: 'example',

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

put '/admin/file_upload', {
aws_access_key_id: 'foo',
aws_secret_access_key: 'bar',
s3_region: 'ap-northeast-1',

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

put '/admin/file_upload', {
aws_access_key_id: 'foo',
aws_secret_access_key: 'bar',
s3_region: 'ap-northeast-1',

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

it 'should be success' do
put '/admin/file_upload', {
aws_access_key_id: 'foo',
aws_secret_access_key: 'bar',

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

context 'With valid params' do
it 'should be success' do
put '/admin/file_upload', {
aws_access_key_id: 'foo',

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

it 'should be success' do
put '/admin/file_upload', {
aws_access_key_id: 'foo',
aws_secret_access_key: 'bar',

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

describe 'PUT /admin/file_upload' do
context 'With valid params' do
it 'should be success' do
put '/admin/file_upload', {

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Redundant curly braces around a hash parameter.

context 'With valid params' do
it 'should be success' do
put '/admin/file_upload', {
aws_access_key_id: 'foo',

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


describe 'PUT /admin/file_upload' do
context 'With valid params' do
it 'should be success' do

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

describe 'PUT /admin/file_upload' do
context 'With valid params' do
it 'should be success' do
put '/admin/file_upload', {

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Redundant curly braces around a hash parameter.

end

describe 'PUT /admin/file_upload' do
context 'With valid params' do

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.


describe 'PUT /admin/file_upload' do
context 'With valid params' do
it 'should be success' do

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

last_response.body.should match('<form')
end

describe 'PUT /admin/file_upload' do

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

it 'GET should show form for custom permalink' do
get '/admin/file_upload'
last_response.should be_ok
last_response.body.should match('<form')

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end

describe 'PUT /admin/file_upload' do
context 'With valid params' do

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

include_context 'admin login'

it 'GET should show form for custom permalink' do
get '/admin/file_upload'

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

last_response.body.should match('<form')
end

describe 'PUT /admin/file_upload' do

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

describe '/admin/file_upload' do
include_context 'admin login'

it 'GET should show form for custom permalink' do

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

it 'GET should show form for custom permalink' do
get '/admin/file_upload'
last_response.should be_ok
last_response.body.should match('<form')

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 25, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

if (textarea.value.length === 0) {
textarea.value = imageTag;
} else if (textarea.selectionStart > 0) {

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 27, 2017

Line is too long.

console.error(response.message);
});
return promise;

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 27, 2017

Missing semicolon.

editor.classList.add('is-success');
resolve(response);
}

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 27, 2017

Missing semicolon.

}
});
}

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 27, 2017

Missing semicolon.


FileUploader.prototype.dragAndDropUpload = function() {
var editor = this.editor;

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 27, 2017

'textarea' is defined but never used.

require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe '/admin/file_upload' do
include_context 'admin login'

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Jan 27, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@morygonzalez morygonzalez changed the title [WIP] File upload File upload Jan 27, 2017

@morygonzalez morygonzalez force-pushed the morygonzalez:file-upload branch 2 times, most recently from 282b2ba to eeec4e8 Jan 27, 2017

@morygonzalez

This comment has been minimized.

Copy link
Member Author

morygonzalez commented Jan 31, 2017

To Do:

  • Add Comtent-Type when uploading image to S3
  • Notify this feature is not available when textarea is WYSIWYG
  • Somebody's hands-on review
@morygonzalez

This comment has been minimized.

Copy link
Member Author

morygonzalez commented Feb 8, 2017

もしどなたか動作検証にご協力いただける方いらっしゃいましたらよろしくお願いいたします 🙇🏻

@komagata

This comment has been minimized.

Copy link
Member

komagata commented Feb 9, 2017

@morygonzalez やりますー!

@morygonzalez morygonzalez force-pushed the morygonzalez:file-upload branch 2 times, most recently from d73be19 to e6d0636 Feb 12, 2017

var ajaxData = new FormData();
var self = this;
ajaxData.append('file', file);
var promise = new Promise(function(resolve, reject) {

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 12, 2017

'Promise' is not defined.

self.insertImage(imageTag);
}).catch(function(response) {
textarea.removeAttribute('disabled');
console.error(response.message);

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 12, 2017

'console' is not defined.

xhr.send(ajaxData);
});
promise.then(function(response) {
console.log(response.message);

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 12, 2017

'console' is not defined.

document.addEventListener('DOMContentLoaded', function() {
var editor = document.querySelector('#editor');
if (typeof editor !== undefined) {
new FileUploader(editor);

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 12, 2017

Do not use 'new' for side effects.

break;
}
return imageTag;
}

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 12, 2017

Missing semicolon.

console.error(response.message);
});
return promise;
}

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 12, 2017

Missing semicolon.

editor.classList.add('is-success');
resolve(response);
}
}

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 12, 2017

Missing semicolon.

droppedFiles = null;
}
});
}

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 12, 2017

Missing semicolon.

editor.addEventListener('drop', function(e) {
var droppedFiles = e.dataTransfer.files;
if (droppedFiles) {
for (var file of droppedFiles) {

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 12, 2017

'for of' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

});
});
eventsToRemoveClass.forEach(function(event) {
editor.addEventListener(event, function(e) {

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 12, 2017

'e' is defined but never used.

@komagata
Copy link
Member

komagata left a comment

ありがとうございます!

@morygonzalez morygonzalez force-pushed the morygonzalez:file-upload branch from e6d0636 to 3f185f9 Feb 22, 2017

@@ -1,6 +1,10 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Missing frozen string literal comment.

@@ -0,0 +1,33 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Missing frozen string literal comment.


context 'Without S3 configuration' do
it 'should be failure' do
post '/admin/attachments', file: Rack::Test::UploadedFile.new(File.join(fixture_path, '1px.gif'))

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Line is too long. [105/80]

end

context 'Without S3 configuration' do
it 'should be failure' do

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end
end

context 'Without S3 configuration' do

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Aws::S3::Bucket.any_instance.stub(:upload_file).and_return(true)
Option.aws_access_key_id = 'foo'
Option.aws_secret_access_key = 'bar'
Option.s3_region = 'ap-northeast-1'

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

}
Aws::S3::Bucket.any_instance.stub(:upload_file).and_return(true)
Option.aws_access_key_id = 'foo'
Option.aws_secret_access_key = 'bar'

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

}
}
Aws::S3::Bucket.any_instance.stub(:upload_file).and_return(true)
Option.aws_access_key_id = 'foo'

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end
end

context 'With invalid params (No file)' do

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end

it 'should be success' do
post '/admin/attachments', file: Rack::Test::UploadedFile.new(File.join(fixture_path, '1px.gif'))

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Line is too long. [105/80]

@morygonzalez morygonzalez force-pushed the morygonzalez:file-upload branch from 3f185f9 to ae65a8a Feb 22, 2017

@@ -0,0 +1,54 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Missing frozen string literal comment.

content_type = MimeMagic.by_magic(tempfile).type
if bucket.object(filename).upload_file(tempfile.path, content_type: content_type)
{
message: 'File upload success',

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

extname = File.extname(tempfile.path)
filename = digest + extname
content_type = MimeMagic.by_magic(tempfile).type
if bucket.object(filename).upload_file(tempfile.path, content_type: content_type)

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Line is too long. [91/80]

credentials = Aws::Credentials.new(Option.aws_access_key_id, Option.aws_secret_access_key)
s3 = Aws::S3::Resource.new(region: Option.s3_region, credentials: credentials)
bucket = s3.bucket(Option.s3_bucket_name)
domain_name = Option.s3_domain_name.presence || "#{Option.s3_bucket_name}.s3-website-#{Option.s3_region}.amazonaws.com"

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Line is too long. [127/80]

def handle_file_upload(params)
begin
credentials = Aws::Credentials.new(Option.aws_access_key_id, Option.aws_secret_access_key)
s3 = Aws::S3::Resource.new(region: Option.s3_region, credentials: credentials)

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Line is too long. [86/80]


def handle_file_upload(params)
begin
credentials = Aws::Credentials.new(Option.aws_access_key_id, Option.aws_secret_access_key)

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Line is too long. [98/80]

@@ -376,5 +376,44 @@ def slugs
def body_attrs
{:class => slugs.join(' ')}
end

def handle_file_upload(params)
begin

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Redundant begin block detected.

flash[:notice] = t("file_upload.successfully_updated")
redirect to("/admin/file_upload")
else
error_message = (["<ul>"] + errors.map { |e| "<li>#{e}</li>" } + ["</ul>"]).join("\n")

This comment has been minimized.

Copy link
@houndci-bot

houndci-bot Feb 22, 2017

Line is too long. [94/80]

@morygonzalez

This comment has been minimized.

Copy link
Member Author

morygonzalez commented May 10, 2017

komagata さんに Approve してもらってるし既存の機能に影響はないと思うので Merge しちゃいますね。

@morygonzalez morygonzalez merged commit 9f43bbd into lokka:master May 10, 2017

1 check passed

hound 42 violations found.

@morygonzalez morygonzalez deleted the morygonzalez:file-upload branch May 10, 2017

@komagata

This comment has been minimized.

Copy link
Member

komagata commented May 11, 2017

morygonzalez added a commit to morygonzalez/lokka that referenced this pull request Feb 25, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.