Commit d87fd5b7 authored by briankabiro's avatar briankabiro

Return the full path to an upload in api response

To fetch an upload, a GET request needs to be made to
/namespace_path/project_path/:upload_path. This change includes
this url in the data returned after making an upload.
parent a5e6144c
---
title: Include full path to an upload in api response
merge_request: 23500
author: briankabiro
type: other
...@@ -1836,11 +1836,12 @@ Returned object: ...@@ -1836,11 +1836,12 @@ Returned object:
{ {
"alt": "dk", "alt": "dk",
"url": "/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.png", "url": "/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.png",
"full_path": "/namespace1/project1/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.png",
"markdown": "![dk](/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.png)" "markdown": "![dk](/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.png)"
} }
``` ```
>**Note**: The returned `url` is relative to the project path. >**Note**: The returned `url` is relative to the project path. The returned `full_path` is the absolute path to the file.
In Markdown contexts, the link is automatically expanded when the format in In Markdown contexts, the link is automatically expanded when the format in
`markdown` is used. `markdown` is used.
......
# frozen_string_literal: true
module API
module Entities
class ProjectUpload < Grape::Entity
include Gitlab::Routing
expose :markdown_name, as: :alt
expose :secure_url, as: :url
expose :full_path do |uploader|
show_project_uploads_path(
uploader.model,
uploader.secret,
uploader.filename
)
end
expose :markdown_link, as: :markdown
end
end
end
...@@ -494,7 +494,9 @@ module API ...@@ -494,7 +494,9 @@ module API
requires :file, type: File, desc: 'The file to be uploaded' # rubocop:disable Scalability/FileUploads requires :file, type: File, desc: 'The file to be uploaded' # rubocop:disable Scalability/FileUploads
end end
post ":id/uploads" do post ":id/uploads" do
UploadService.new(user_project, params[:file]).execute.to_h upload = UploadService.new(user_project, params[:file]).execute
present upload, with: Entities::ProjectUpload
end end
desc 'Get the users list of a project' do desc 'Get the users list of a project' do
......
...@@ -1250,6 +1250,8 @@ describe API::Projects do ...@@ -1250,6 +1250,8 @@ describe API::Projects do
expect(json_response['alt']).to eq("dk") expect(json_response['alt']).to eq("dk")
expect(json_response['url']).to start_with("/uploads/") expect(json_response['url']).to start_with("/uploads/")
expect(json_response['url']).to end_with("/dk.png") expect(json_response['url']).to end_with("/dk.png")
expect(json_response['full_path']).to start_with("/#{project.namespace.path}/#{project.path}/uploads")
end end
end end
......
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