Add API project upload endpoint

This commit is contained in:
Douwe Maan 2016-01-07 13:37:14 +01:00
parent 09216e8bbd
commit 1886d727f7
8 changed files with 70 additions and 23 deletions

View file

@ -66,7 +66,7 @@ class @DropzoneInput
success: (header, response) ->
child = $(dropzone[0]).children("textarea")
$(child).val $(child).val() + formatLink(response.link) + "\n"
$(child).val $(child).val() + response.link.markdown + "\n"
return
error: (temp, errorMessage) ->
@ -99,11 +99,6 @@ class @DropzoneInput
child = $(dropzone[0]).children("textarea")
formatLink = (link) ->
text = "[#{link.alt}](#{link.url})"
text = "!#{text}" if link.is_image
text
handlePaste = (event) ->
pasteEvent = event.originalEvent
if pasteEvent.clipboardData and pasteEvent.clipboardData.items
@ -162,7 +157,7 @@ class @DropzoneInput
closeAlertMessage()
success: (e, textStatus, response) ->
insertToTextArea(filename, formatLink(response.responseJSON.link))
insertToTextArea(filename, response.responseJSON.link.markdown)
error: (response) ->
showError(response.responseJSON.message)
@ -202,8 +197,3 @@ class @DropzoneInput
e.preventDefault()
$(@).closest('.gfm-form').find('.div-dropzone').click()
return
formatLink: (link) ->
text = "[#{link.alt}](#{link.url})"
text = "!#{text}" if link.is_image
text

View file

@ -18,10 +18,15 @@ module Projects
filename = uploader.image? ? uploader.file.basename : uploader.file.filename
escaped_filename = filename.gsub("]", "\\]")
markdown = "[#{escaped_filename}](#{uploader.secure_url})"
markdown.prepend("!") if uploader.image?
{
'alt' => filename,
'url' => uploader.secure_url,
'is_image' => uploader.image?
'is_image' => uploader.image?,
'markdown' => markdown
}
end

View file

@ -12,10 +12,15 @@ module Projects
filename = uploader.image? ? uploader.file.basename : uploader.file.filename
escaped_filename = filename.gsub("]", "\\]")
markdown = "[#{escaped_filename}](#{uploader.secure_url})"
markdown.prepend("!") if uploader.image?
{
alt: filename,
url: uploader.secure_url,
is_image: uploader.image?
is_image: uploader.image?,
markdown: markdown
}
end

View file

@ -482,6 +482,34 @@ Parameters:
- `id` (required) - The ID of a project
## Uploads
### Upload a file
Uploads a file to the specified project to be used in an issue or merge request description, or a comment.
```
POST /projects/:id/uploads
```
Parameters:
- `id` (required) - The ID of the project
- `file` (required) - The file to be uploaded
```json
{
"alt": "dk",
"url": "/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.png",
"is_image": true,
"markdown": "![dk](/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.png)"
}
```
**Note**: The returned `url` is relative to the project path.
In Markdown contexts, the link is automatically expanded when the format in `markdown` is used.
## Team members
### List project team members

View file

@ -278,6 +278,16 @@ module API
user_project.forked_project_link.destroy
end
end
# Upload a file
#
# Parameters:
# id: (required) - The ID of the project
# file: (required) - The file to be uploaded
post ":id/uploads" do
::Projects::UploadService.new(user_project, params[:file]).execute
end
# search for projects current_user has access to
#
# Parameters:

View file

@ -82,10 +82,7 @@ module Gitlab
attachments = Email::AttachmentUploader.new(message).execute(sent_notification.project)
attachments.each do |link|
text = "[#{link[:alt]}](#{link[:url]})"
text.prepend("!") if link[:is_image]
reply << "\n\n#{text}"
reply << "\n\n#{link[:markdown]}"
end
reply

View file

@ -232,9 +232,7 @@ module Gitlab
return nil if res.nil?
text = "[#{res['alt']}](#{res['url']})"
text = "!#{text}" if res['is_image']
text
text = res['markdown']
end
def build_attachment_url(rel_url)

View file

@ -353,6 +353,20 @@ describe API::API, api: true do
end
end
describe "POST /projects/:id/uploads" do
before { project }
it "uploads the file and returns its info" do
post api("/projects/#{project.id}/uploads", user), file: fixture_file_upload(Rails.root + "spec/fixtures/dk.png", "image/png")
expect(response.status).to be(201)
expect(json_response['alt']).to eq("dk")
expect(json_response['url']).to start_with("/uploads/")
expect(json_response['url']).to end_with("/dk.png")
expect(json_response['is_image']).to eq(true)
end
end
describe 'GET /projects/:id' do
before { project }
before { project_member }