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
Image upload in creating issue #6962
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 |
---|---|---|
|
@@ -13,5 +13,4 @@ def download | |
redirect_to uploader.url | ||
end | ||
end | ||
end | ||
|
||
end | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,7 +60,6 @@ def show | |
|
||
def create | ||
@issue = Issues::CreateService.new(project, current_user, params[:issue]).execute | ||
|
||
respond_to do |format| | ||
format.html do | ||
if @issue.valid? | ||
|
@@ -69,15 +68,19 @@ def create | |
render :new | ||
end | ||
end | ||
format.js | ||
format.js do |format| | ||
# issue = Issues.create() | ||
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. What does this comment try to explain? |
||
|
||
@link = @issue.attachment.url.to_js | ||
end | ||
end | ||
end | ||
|
||
def update | ||
@issue = Issues::UpdateService.new(project, current_user, params[:issue]).execute(issue) | ||
|
||
respond_to do |format| | ||
format.js | ||
format.js | ||
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. Trailing whitespace detected. |
||
format.html do | ||
if @issue.valid? | ||
redirect_to [@project, @issue] | ||
|
@@ -93,6 +96,24 @@ def bulk_update | |
redirect_to :back, notice: "#{result[:count]} issues updated" | ||
end | ||
|
||
def upload_image | ||
@main_dir = FileUploader.generate_dir | ||
upload_path = File.join(project.namespace.path, @project.path, 'issues', @main_dir) | ||
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. [87/80] |
||
accepted_types = %w(png jpg jpeg gif) | ||
uploader = FileUploader.new('uploads', upload_path, accepted_types) | ||
links = [] | ||
params['issue-imgs'].each do |img| | ||
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. Wouldn't it be simpler to limit the upload API to a single image (rather than several images at once), and let the javascript/browser send one request per image to upload? IMHO it would make the error handling easier on the browser side: each upload would receive its own success or error. If several files can be uploaded at once the error handling must deal with partial errors. |
||
alt = uploader.store!(img) | ||
links << { '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. Trailing whitespace detected. |
||
'url' => File.join(root_url, uploader.url) } | ||
end | ||
|
||
|
||
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. Extra blank line detected. |
||
respond_to do |format| | ||
format.json { render json: { links: links } } | ||
end | ||
end | ||
|
||
protected | ||
|
||
def issue | ||
|
@@ -128,7 +149,6 @@ def issues_filtered | |
# | ||
def redirect_old | ||
issue = @project.issues.find_by(id: params[:id]) | ||
|
||
if issue | ||
redirect_to project_issue_path(@project, issue) | ||
return | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,12 @@ | |
# milestone_id :integer | ||
# state :string(255) | ||
# iid :integer | ||
# attachment :string(255) | ||
# | ||
|
||
require 'carrierwave/orm/activerecord' | ||
require 'file_size_validator' | ||
|
||
class Issue < ActiveRecord::Base | ||
include Issuable | ||
include InternalId | ||
|
@@ -30,7 +34,8 @@ class Issue < ActiveRecord::Base | |
scope :of_user_team, ->(team) { where(project_id: team.project_ids, assignee_id: team.member_ids) } | ||
|
||
attr_accessible :title, :assignee_id, :position, :description, | ||
:milestone_id, :label_list, :state_event | ||
:milestone_id, :label_list, :author_id_of_changes, | ||
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. Why did you add the |
||
:state_event | ||
|
||
acts_as_taggable_on :labels | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,12 @@ class CreateService < Issues::BaseService | |
def execute | ||
issue = project.issues.new(params) | ||
issue.author = current_user | ||
|
||
if issue.save | ||
notification_service.new_issue(issue, current_user) | ||
event_service.open_issue(issue, current_user) | ||
issue.create_cross_references!(issue.project, current_user) | ||
execute_hooks(issue) | ||
end | ||
|
||
issue | ||
end | ||
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. Please avoid changes like this in files where you're not adding/changing code. |
||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,4 @@ def file_storage? | |
def reset_events_cache(file) | ||
model.reset_events_cache if model.is_a?(User) | ||
end | ||
end | ||
end | ||
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. Final newline missing. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# encoding: utf-8 | ||
class FileUploader < CarrierWave::Uploader::Base | ||
storage :file | ||
|
||
def initialize(base_dir, path, allowed_extensions = nil) | ||
@base_dir = base_dir | ||
@path = path | ||
@allowed_extensions = allowed_extensions | ||
end | ||
|
||
def base_dir | ||
@base_dir | ||
end | ||
|
||
def store_dir | ||
File.join(base_dir, @path) | ||
end | ||
|
||
def cache_dir | ||
File.join(base_dir, 'tmp', @path) | ||
end | ||
|
||
def extension_white_list | ||
@allowed_extensions | ||
end | ||
|
||
def store!(file) | ||
original_filename = file.original_filename | ||
file.original_filename = self.class.generate_filename(file) + File.extname(original_filename) | ||
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. [97/80] |
||
super | ||
original_filename | ||
end | ||
|
||
def self.generate_filename(file) | ||
Digest::MD5.hexdigest(File.basename(file.original_filename, '.*')) | ||
end | ||
|
||
def self.generate_dir | ||
SecureRandom.hex(5) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,19 @@ | |
.form-group | ||
= f.label :description, 'Description', class: 'control-label' | ||
.col-sm-10 | ||
= f.text_area :description, class: "form-control js-gfm-input", rows: 14 | ||
%p.hint Issues are parsed with #{link_to "GitLab Flavored Markdown", help_markdown_path, target: '_blank'}. | ||
#div-dropzone | ||
= f.text_area :description, class: 'form-control js-gfm-input', rows: 14 | ||
#div-dropzone-hover | ||
%i.icon-picture.div-dropzone-icon | ||
#div-dropzone-error | ||
%i.icon-remove.div-dropzone-icon-error | ||
%div#dropzone-error-message{style:'display:inline'} | ||
Can't Attach File | ||
#div-dropzone-spinner | ||
%i.icon-spinner.icon-spin.div-dropzone-icon | ||
%p.hint{style: "display: inline"} Issues are parsed with #{link_to "GitLab Flavored Markdown", help_markdown_path, target: '_blank'}. | ||
%p.hint.pull-right{style: "display: inline"} Attach images (JPG, PNG, GIF) by dragging & dropping or #{link_to "selecting them", '#', class: 'markdown-selector' }. | ||
|
||
%hr | ||
.form-group | ||
.issue-assignee | ||
|
@@ -46,8 +57,6 @@ | |
= f.text_field :label_list, maxlength: 2000, class: "form-control" | ||
%p.hint Separate labels with commas. | ||
|
||
|
||
|
||
.form-actions | ||
- if @issue.new_record? | ||
= f.submit 'Submit new issue', class: "btn btn-create" | ||
|
@@ -57,9 +66,6 @@ | |
- cancel_path = @issue.new_record? ? project_issues_path(@project) : project_issue_path(@project, @issue) | ||
= link_to "Cancel", cancel_path, class: 'btn btn-cancel' | ||
|
||
|
||
|
||
|
||
:javascript | ||
$("#issue_label_list") | ||
.bind( "keydown", function( event ) { | ||
|
@@ -94,3 +100,73 @@ | |
$('#issue_assignee_id').val("#{current_user.id}").trigger("change"); | ||
e.preventDefault(); | ||
}); | ||
|
||
function handlePaste(e) { | ||
clipboardData = e.originalEvent.clipboardData; | ||
for (var i = 0 ; i < clipboardData.items.length ; i++) { | ||
var item = clipboardData.items[i]; | ||
console.log(item); | ||
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. Dont forget to remove the console.log's :-) |
||
if (item.type.indexOf("image") != -1) { | ||
uploadFile(item.getAsFile()); | ||
} | ||
else { | ||
console.log("Discarding non-image paste data"); | ||
} | ||
} | ||
} | ||
|
||
function formatLinks(links) { | ||
var md_links = []; | ||
links.forEach(function (str) { | ||
md_links.push('![' + str.alt + '](' + str.url + ')'); | ||
}) | ||
return md_links; | ||
} | ||
|
||
$('#div-dropzone').dropzone({ | ||
url: "#{upload_image_project_issues_path}", | ||
dictDefaultMessage: "", | ||
paramName: "issue-imgs", | ||
maxFilesize: 10, | ||
uploadMultiple: true, | ||
acceptedFiles: "image/jpg,image/jpeg,image/gif,image/png", | ||
headers: { "X-CSRF-Token": $('meta[name="csrf-token"]').attr('content')}, | ||
|
||
success: function(header,response){ | ||
$("#issue_description").val($("#issue_description").val() + formatLinks(response.links).join("\n") + "\n"); | ||
}, | ||
|
||
error: function(temp, errorMessage) { | ||
$('#dropzone-error-message').text(errorMessage); | ||
$('#div-dropzone-error').css('opacity', 0.7); | ||
$('#div-dropzone-error').delay(500).fadeTo("slow", 0); | ||
}, | ||
|
||
sending: function() { | ||
$('#div-dropzone-spinner').css('opacity', 0.7); | ||
}, | ||
|
||
complete: function() { | ||
$('#div-dropzone-spinner').css('opacity',0); | ||
} | ||
}); | ||
|
||
$('#div-dropzone').on('dragenter',function(){ | ||
$(this).children('textarea').focus(); | ||
$('#div-dropzone-hover').css('opacity',0.7); | ||
}); | ||
|
||
$('body').on('dragover',function(){ | ||
$('#div-dropzone').children('textarea').blur(); | ||
$('#div-dropzone-hover').css('opacity',0); | ||
}); | ||
|
||
$('#div-dropzone').on('drop',function(){ | ||
$('.dz-preview').remove(); | ||
$('#div-dropzone-hover').css('opacity',0); | ||
}); | ||
|
||
$('.markdown-selector').click(function() { | ||
$('#div-dropzone').click(); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
= render "form" | ||
= render "form" | ||
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. No newline? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ | |
.wiki | ||
= preserve do | ||
= markdown @issue.description | ||
|
||
.context | ||
%cite.cgray | ||
= render partial: 'issue_context', locals: { issue: @issue } | ||
|
@@ -73,4 +74,4 @@ | |
= label.name | ||
| ||
|
||
.voting_notes#notes= render "projects/notes/notes_with_form" | ||
.voting_notes#notes= render "projects/notes/notes_with_form" | ||
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. No newline? |
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.
Final newline missing.