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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [84/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [84/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, '.*'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please fix the indentation here? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can make this configurable? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 & dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }. | ||
.pull-right Attach files by dragging & dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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}"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [82/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [83/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [106/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [82/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [83/80] There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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]
There was a problem hiding this comment.
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? :)
There was a problem hiding this comment.
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:
or
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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 :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just do: