Commit 2b94977d by tady (Masato TADA)

Merge pull request #113 from tadyjp/imagemagick-pdf-slice

Slide cover uploader
parents 22e5e7ca d1805ee0
......@@ -141,5 +141,11 @@ gem 'kaminari', github: 'amatsuda/kaminari'
gem 'jwt', '0.1.11'
# Optional
# For notifing to HipChat
gem 'hipchat'
# For PDF upload
gem 'rmagick', :require => 'RMagick'
GIT
remote: git://github.com/amatsuda/kaminari.git
revision: 21ad994d42527fd97c8168b0d0ccdd88cec91251
revision: 5b221e01ea63852a3894c6b0c3c6de50cdace27a
specs:
kaminari (0.15.1)
kaminari (1.0.0.alpha)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
......@@ -228,7 +228,7 @@ GEM
net-ssh (>= 2.6.5)
net-ssh (2.9.1)
netrc (0.7.7)
newrelic_rpm (3.9.0.229)
newrelic_rpm (3.9.1.236)
nokogiri (1.6.3.1)
mini_portile (= 0.6.0)
ntlm-http (0.1.1)
......@@ -306,6 +306,7 @@ GEM
rest-client (1.7.2)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
rmagick (2.13.3)
rspec (3.0.0)
rspec-core (~> 3.0.0)
rspec-expectations (~> 3.0.0)
......@@ -333,7 +334,7 @@ GEM
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.4)
ruby-progressbar (1.5.1)
sass (3.3.11)
sass (3.3.14)
sdoc (0.4.0)
json (~> 1.8)
rdoc (~> 4.0, < 5.0)
......@@ -451,6 +452,7 @@ DEPENDENCIES
pry-rails
rack-mini-profiler
rails (~> 4.1)
rmagick
rspec-rails
rubocop
sass-rails!
......
......@@ -61,6 +61,14 @@ $ (bundle exec) rake db:seed
And have fun with your team !
# Optional features
## PDF uploading
```
brew install imagemagick ghostscript
```
# Test
......
......@@ -2,28 +2,59 @@ $.extend
mod_fileuploader: (options) ->
settings =
$input: null,
$textarea: null
$textarea: null,
$progressWrapper: null,
$progressBar: null
settings = $.extend settings, options
# Used for replacing text
uploadingIndex = 0
settings.$input.fileupload
dataType: 'json'
add: (e, data) ->
settings.$progressWrapper.show()
settings.$textarea.val(settings.$textarea.val() + "\n[Uploading... #" + uploadingIndex + "]\n")
data.formData = { uploading_index: uploadingIndex }
data.submit()
uploadingIndex += 1
done: (e, data) ->
setTimeout ->
settings.$progressWrapper.fadeOut()
, 1000
$.each data.result.files, (index, file) ->
# TODO: カーソル位置に挿入
textarea_value = settings.$textarea.val()
if file.type is 'image'
# image ![file-name](file-url)
settings.$textarea.val(settings.$textarea.val() + "\n![" + file.name + "](" + file.url + ")\n")
# ![file-name](file-url)
replacing_text = "![" + file.name + "](" + file.image + ")"
new_textarea_value = textarea_value.replace('[Uploading... #' + data.result.uploading_index + ']', replacing_text)
settings.$textarea.val(new_textarea_value)
else if file.type is 'slide'
# slide !slide!(file-url)
settings.$textarea.val(settings.$textarea.val() + "\n!slide!(" + file.url + ")\n")
# [![alt text](image link)](web link)
replacing_text = "[![" + file.name + "](" + file.image + ")](" + file.url + ")"
new_textarea_value = textarea_value.replace('[Uploading... #' + data.result.uploading_index + ']', replacing_text)
settings.$textarea.val(new_textarea_value)
settings.$textarea.trigger("change")
# $('<p/>').text(file.name).appendTo('#files') # TODO
# progressall: (e, data) ->
# progress = parseInt(data.loaded / data.total * 100, 10)
# $('.progress-bar').css
# width: progress + '%'
progressall: (e, data) ->
progress = parseInt(data.loaded / data.total * 100, 10)
if progress >= 99
settings.$progressWrapper.find('.progress-title').text 'Uploading files may task some time for converting.'
settings.$progressBar
.css(width: progress + '%')
.text(progress + '%')
settings.$input.prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
......@@ -31,15 +62,12 @@ $.extend
$('<p id="mod-fileuploader-notify"/>').text('ここにドロップしてください').appendTo('body')
$(window).bind 'dragover', (e) ->
console.log('dragover')
$('#mod-fileuploader-notify').show()
# $(window).bind 'dragleave', (e) ->
# console.log('dragleave')
# $('#mod-fileuploader-notify').hide()
$(window).bind 'dragend', (e) ->
console.log('dragend')
$('#mod-fileuploader-notify').hide()
$(window).bind 'drop', (e) ->
console.log('drop')
$('#mod-fileuploader-notify').hide()
......@@ -11,35 +11,41 @@ class ApisController < ApplicationController
end
# Receive file and upload to S3
# @response [JSON]
# { "status": "OK",
# "files": [
# { "name": <file name>, "url": <link url>, "image": <image url>, "type": <file type>}, ...
# ]
# }
def file_receiver
s3 = AWS::S3.new
bucket_name = "#{Settings.s3.bucket_name}/1/#{current_user.id}"
# bucket_name = "1/#{current_user.id}"
bucket = s3.buckets[bucket_name]
s3_uploader = S3Uploader.new(bucket: "#{Settings.s3.bucket_name}/1/#{current_user.id}")
s3_files = []
params[:files].each do |file|
basename = File.basename(file.path)
# Skip uploading if file ext is not listed.
next unless file.original_filename =~ /\.(jpe?g|png|gif|pdf)\Z/
object_file_name = "#{Digest::MD5.file(file.path).to_s}#{File.extname(file.original_filename)}"
obj = bucket.objects[object_file_name]
res = obj.write(file: file.path, acl: :public_read)
res = s3_uploader.upload!(file: file.path, name: object_file_name)
file_type = case file.original_filename
case file.original_filename
when /\.(jpe?g|png|gif)\Z/
:image
s3_files << { type: 'image', name: file.original_filename, image: res.public_url.to_s }
when /\.pdf\Z/
:slide
if Settings.respond_to?(:pdf_uploading) && Settings.pdf_uploading
cover_image_name = "#{Digest::MD5.file(file.path).to_s}-cover.png"
pdf = Magick::ImageList.new(file.path + '[0]')
cover_tmp = Rails.root.join('tmp', cover_image_name)
pdf[0].write(cover_tmp)
cover_res = s3_uploader.upload!(file: cover_tmp, name: cover_image_name)
s3_files << { type: 'slide', name: cover_image_name, url: res.public_url.to_s, image: cover_res.public_url.to_s }
end
end
s3_files << { name: file.original_filename, url: res.public_url.to_s, type: file_type }
end
render json: { status: 'OK', files: s3_files }
render json: { status: 'OK', files: s3_files, uploading_index: params[:uploading_index] }
end
def user_mention
......
class S3Uploader
# Preparing S3 object and bucket.
# @param [String] access_key_id.
# @param [String] secret_access_key.
# @param [String] region: uploading S3 bucket region.
# @param [String] bucket: uploading backet name.
def initialize(access_key_id: Settings.s3.access_key_id,
secret_access_key: Settings.s3.secret_access_key,
region: "ap-northeast-1",
bucket:)
@s3 = AWS::S3.new(access_key_id: access_key_id,
secret_access_key: secret_access_key,
region: region)
@bucket = @s3.buckets[bucket]
end
# Upload file to S3
# @param [String] file: uploading file path.
# @param [String] name: uploading file name.
def upload!(file:, name:)
obj = @bucket.objects[name]
obj.write(file: file, acl: :public_read)
end
end
......@@ -26,6 +26,14 @@
br/
.row
#progress-wrapper.alert.alert-warning.fade.in style="display:none;"
span.progress-title
| Uploading files...
#progress style="height:20px;"
.progress-bar.progress-bar-success.progress-bar-striped.active role="progressbar" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"
| 0%
.row
.col-xs-6
.panel.panel-default
.panel-heading
......@@ -46,8 +54,9 @@
i.fa.fa-list-ul
button.btn.btn-default.mod-mdEditor-btn-strikethrough type="button" title="打ち消し線"
i.fa.fa-strikethrough
/ button.btn.btn-default.mod-mdEditor-btn-link type="button"
/ i.fa.fa-link
.btn-group.btn-group-xs
button#uploadButton.btn.btn-default type="button" title="Upload"
| Upload
.panel-body.panel-body-nopadding
= f.text_area :body, class: 'mod-mdEditor-textarea', placehoder: 'type your text....'
......@@ -77,10 +86,18 @@ input#fileupload data-url="/apis/file_receiver" multiple="" name="files[]" style
$.mod_fileuploader({
$input: $('#fileupload'),
$textarea: $('.mod-mdEditor-textarea')
$textarea: $('.mod-mdEditor-textarea'),
$progressWrapper: $('#progress-wrapper'),
$progressBar: $('#progress .progress-bar')
});
// Display upload dialog.
$('#uploadButton').on('click', function(){
$('input#fileupload').trigger('click');
});
// 下書き保存
// TODO
$('.btn-save-draft').on('click', function(e){
e.preventDefault();
var val = $('.mod-mdEditor-body').val();
......
AWS.config(
access_key_id: Settings.s3.access_key_id,
secret_access_key: Settings.s3.secret_access_key,
s3_endpoint: "s3-ap-northeast-1.amazonaws.com"
)
......@@ -27,6 +27,9 @@ defaults: &defaults
# optional
# google_analytics:
# optional
# pdf_uploading: false
development:
<<: *defaults
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment