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

Generalizes image upload in drag and drop in markdown to all files. #7078

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions app/assets/javascripts/markdown_area.js.coffee
@@ -1,6 +1,9 @@
formatLink = (str) ->
"![" + str.alt + "](" + str.url + ")"

text = "[" + str.alt + "](" + str.url + ")"
if str.is_image
text = "!" + text
text

$(document).ready ->
alertClass = "alert alert-danger alert-dismissable div-dropzone-alert"
alertAttr = "class=\"close\" data-dismiss=\"alert\"" + "aria-hidden=\"true\""
Expand All @@ -10,7 +13,7 @@ $(document).ready ->
iconPicture = "<i class=\"icon-picture div-dropzone-icon\"></i>"
iconSpinner = "<i class=\"icon-spinner icon-spin div-dropzone-icon\"></i>"
btnAlert = "<button type=\"button\"" + alertAttr + ">&times;</button>"
project_image_path_upload = window.project_image_path_upload or null
project_file_path_upload = window.project_file_path_upload or null

$("textarea.markdown-area").wrap "<div class=\"div-dropzone\"></div>"

Expand All @@ -23,13 +26,12 @@ $(document).ready ->


dropzone = $(".div-dropzone").dropzone(
url: project_image_path_upload
url: project_file_path_upload
dictDefaultMessage: ""
clickable: true
paramName: "markdown_img"
paramName: "markdown_file"
maxFilesize: 10
uploadMultiple: false
acceptedFiles: "image/jpg,image/jpeg,image/gif,image/png"
headers:
"X-CSRF-Token": $("meta[name=\"csrf-token\"]").attr("content")

Expand Down
17 changes: 6 additions & 11 deletions app/controllers/projects_controller.rb
Expand Up @@ -162,12 +162,12 @@ def unarchive
end
end

def upload_image
link_to_image = ::Projects::ImageService.new(repository, params, root_url).execute
def upload_file
link_to_file = ::Projects::FileService.new(repository, params, root_url).execute

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [84/80]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any tips to solve this warning? :)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you really want to:

def upload_file
  link_to_file = ::Projects::FileService.
    new(repository, params, root_url).execute
end

or

def upload_file
  ::Projects::FileService.new(repository, params, root_url).execute
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where I can find ::Projects::FileService class?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a git error. Sorry :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just do:

link_to_file = ::Projects::FileService.new(repository, params, root_url).
  execute

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [84/80]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [84/80]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [84/80]


respond_to do |format|
if link_to_image
format.json { render json: { link: link_to_image } }
if link_to_file
format.json { render json: { link: link_to_file } }
else
format.json { render json: "Invalid file.", status: :unprocessable_entity }
end
Expand All @@ -176,13 +176,8 @@ def upload_image

private

def upload_path
base_dir = FileUploader.generate_dir
File.join(repository.path_with_namespace, base_dir)
end

def accepted_images
%w(png jpg jpeg gif)
def invalid_file(error)
render json: { message: error.message }, status: :internal_server_error
end

def set_title
Expand Down
49 changes: 49 additions & 0 deletions app/services/projects/file_service.rb
@@ -0,0 +1,49 @@
module Projects
class FileService < BaseService
include Rails.application.routes.url_helpers
def initialize(repository, params, root_url)
@repository, @params, @root_url = repository, params.dup, root_url
end

def execute
uploader = FileUploader.new('uploads', upload_path, accepted_files)
file = @params['markdown_file']

if file
alt = file.original_filename
uploader.store!(file)
link = {
'alt' => File.basename(alt, '.*'),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please fix the indentation here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

'url' => File.join(@root_url, uploader.url),
'is_image' => image?(file)
}
else
link = nil
end
end

protected

def accepted_files
# insert accepted mime types here (e.g %w(jpg jpeg gif png))
nil
end

def accepted_images
%w(jpg jpeg gif png)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can make this configurable?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe on the constants section of the app? What do you think? Same goes with the #accepted_files.

end

def image?(file)
accepted_images.map { |format| file.content_type.include? format }.any?
end

def upload_path
base_dir = FileUploader.generate_dir
File.join(@repository.path_with_namespace, base_dir)
end

def correct_mime_type?(file)
accepted_files.map { |format| image.content_type.include? format }.any?
end
end
end
39 changes: 0 additions & 39 deletions app/services/projects/image_service.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/uploaders/file_uploader.rb
Expand Up @@ -21,7 +21,7 @@ def cache_dir
end

def extension_white_list
@allowed_extensions
@allowed_extensions || super
end

def store!(file)
Expand Down
4 changes: 2 additions & 2 deletions app/views/projects/issues/_form.html.haml
Expand Up @@ -23,7 +23,7 @@
= f.text_area :description, class: 'form-control js-gfm-input markdown-area', rows: 14
.col-sm-12.hint
.pull-left Issues are parsed with #{link_to "GitLab Flavored Markdown", help_page_path("markdown", "markdown"), target: '_blank'}.
.pull-right Attach images (JPG, PNG, GIF) by dragging &amp; dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }.
.pull-right Attach files by dragging &amp; dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will you give the user feedback on what type of files are allowed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually every file type is now allowed in this PR so I think the user feedback is unnecessary. But if you can give me the specific file types maybe we can provide some term for correct mime types.

.clearfix
.error-alert
%hr
Expand Down Expand Up @@ -97,4 +97,4 @@
e.preventDefault();
});

window.project_image_path_upload = "#{upload_image_project_path @project}";
window.project_file_path_upload = "#{upload_file_project_path @project}";
4 changes: 2 additions & 2 deletions app/views/projects/merge_requests/_form.html.haml
Expand Up @@ -25,7 +25,7 @@
= f.text_area :description, class: "form-control js-gfm-input markdown-area", rows: 14
.col-sm-12.hint
.pull-left Description is parsed with #{link_to "GitLab Flavored Markdown", help_page_path("markdown", "markdown"), target: '_blank'}.
.pull-right Attach images (JPG, PNG, GIF) by dragging &amp; dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }.
.pull-right Attach files by dragging &amp; dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }.
.clearfix
.error-alert
%hr
Expand Down Expand Up @@ -103,4 +103,4 @@
}
});

window.project_image_path_upload = "#{upload_image_project_path @project}";
window.project_file_path_upload = "#{upload_file_project_path @project}";
4 changes: 2 additions & 2 deletions app/views/projects/merge_requests/_new_submit.html.haml
Expand Up @@ -26,7 +26,7 @@
= f.text_area :description, class: "form-control js-gfm-input markdown-area", rows: 10
.col-sm-12.hint
.pull-left Description is parsed with #{link_to "GitLab Flavored Markdown", help_page_path("markdown", "markdown"), target: '_blank'}.
.pull-right Attach images (JPG, PNG, GIF) by dragging & dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }.
.pull-right Attach files by dragging & dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }.
.clearfix
.error-alert
.form-group
Expand Down Expand Up @@ -85,4 +85,4 @@
e.preventDefault();
});

window.project_image_path_upload = "#{upload_image_project_path @project}";
window.project_file_path_upload = "#{upload_file_project_path @project}";
4 changes: 2 additions & 2 deletions app/views/projects/milestones/_form.html.haml
Expand Up @@ -24,7 +24,7 @@
= f.text_area :description, maxlength: 2000, class: "form-control markdown-area", rows: 10
.hint
.pull-left Milestones are parsed with #{link_to "GitLab Flavored Markdown", help_page_path("markdown", "markdown"), target: '_blank'}.
.pull-left Attach images (JPG, PNG, GIF) by dragging & dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }.
.pull-left Attach files by dragging & dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }.
.clearfix
.error-alert
.col-md-6
Expand All @@ -50,4 +50,4 @@
onSelect: function(dateText, inst) { $("#milestone_due_date").val(dateText) }
}).datepicker("setDate", $.datepicker.parseDate('yy-mm-dd', $('#milestone_due_date').val()));

window.project_image_path_upload = "#{upload_image_project_path @project}";
window.project_file_path_upload = "#{upload_file_project_path @project}";
4 changes: 2 additions & 2 deletions app/views/projects/notes/_form.html.haml
Expand Up @@ -18,7 +18,7 @@

.light.clearfix
.pull-left Comments are parsed with #{link_to "GitLab Flavored Markdown", help_page_path("markdown", "markdown"), target: '_blank'}
.pull-right Attach images (JPG, PNG, GIF) by dragging &amp; dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }.
.pull-right Attach files by dragging &amp; dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }.

.note-preview-holder.hide
.js-note-preview
Expand All @@ -38,4 +38,4 @@
= f.file_field :attachment, class: "js-note-attachment-input hidden"

:javascript
window.project_image_path_upload = "#{upload_image_project_path @project}";
window.project_file_path_upload = "#{upload_file_project_path @project}";
5 changes: 2 additions & 3 deletions app/views/projects/wikis/_form.html.haml
Expand Up @@ -25,7 +25,7 @@
= f.text_area :content, class: 'form-control js-gfm-input markdown-area', rows: 18
.col-sm-12.hint
.pull-left Wiki content is parsed with #{link_to "GitLab Flavored Markdown", help_page_path("markdown", "markdown"), target: '_blank'}
.pull-right Attach images (JPG, PNG, GIF) by dragging & dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }.
.pull-right Attach files by dragging & dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }.
.clearfix
.error-alert
.form-group
Expand All @@ -41,5 +41,4 @@
= link_to "Cancel", project_wiki_path(@project, :home), class: "btn btn-cancel"

:javascript
window.project_image_path_upload = "#{upload_image_project_path @project}";

window.project_file_path_upload = "#{upload_file_project_path @project}";
2 changes: 1 addition & 1 deletion config/routes.rb
Expand Up @@ -168,7 +168,7 @@
post :fork
post :archive
post :unarchive
post :upload_image
post :upload_file
get :autocomplete_sources
get :import
put :retry_import
Expand Down
25 changes: 14 additions & 11 deletions spec/controllers/projects_controller_spec.rb
Expand Up @@ -6,37 +6,40 @@
let(:jpg) { fixture_file_upload(Rails.root + 'spec/fixtures/rails_sample.jpg', 'image/jpg') }
let(:txt) { fixture_file_upload(Rails.root + 'spec/fixtures/doc_sample.txt', 'text/plain') }

describe "POST #upload_image" do
describe "POST #upload_file" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer single-quoted strings when you don't need string interpolation or special symbols.

before do
sign_in(user)
project.team << [user, :developer]
end

context "without params['markdown_img']" do
context "without params['markdown_file']" do
it "returns an error" do
post :upload_image, id: project.to_param, format: :json
post :upload_file, id: project.to_param, format: :json
expect(response.status).to eq(422)
end
end

context "with invalid file" do
context "with valid image" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer single-quoted strings when you don't need string interpolation or special symbols.

before do
post :upload_image, id: project.to_param, markdown_img: txt, format: :json
post :upload_file, id: project.to_param, markdown_file: jpg, format: :json

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [82/80]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [82/80]

end

it "returns an error" do
expect(response.status).to eq(422)
it "returns a content with original filename, new link, and correct type." do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [83/80]
Prefer single-quoted strings when you don't need string interpolation or special symbols.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [83/80]

expect(response.body).to match "\"alt\":\"rails_sample\""
expect(response.body).to match "\"url\":\"http://test.host/uploads/#{project.path_with_namespace}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [106/80]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [106/80]

expect(response.body).to match "\"is_image\": true
end
end

context "with valid file" do
context "with valid non-image file" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer single-quoted strings when you don't need string interpolation or special symbols.

before do
post :upload_image, id: project.to_param, markdown_img: jpg, format: :json
post :upload_file, id: project.to_param, markdown_file: txt, format: :json

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [82/80]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [82/80]

end

it "returns a content with original filename and new link." do
expect(response.body).to match "\"alt\":\"rails_sample\""
it "returns a content with original filename, new link, and correct type." do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [83/80]
Prefer single-quoted strings when you don't need string interpolation or special symbols.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [83/80]

expect(response.body).to match "\"alt\":\"doc_sample\""
expect(response.body).to match "\"url\":\"http://test.host/uploads/#{project.path_with_namespace}"
expect(response.body).to match "\"is_image\": false
end
end
end
Expand Down