Use normal file upload mechanism to upload artifacts

This commit is contained in:
Kamil Trzcinski 2015-11-09 22:01:26 +01:00
parent 7e4e3fb3b6
commit db3213fc1c
3 changed files with 34 additions and 23 deletions

View file

@ -294,19 +294,23 @@ module API
# file helpers
def uploaded_file!(uploads_path)
required_attributes! [:file]
def uploaded_file!(field, uploads_path)
if params[field]
bad_request!("#{field} is not a file") unless params[field].respond_to?(:filename)
return params[field]
end
# sanitize file paths
# this requires for all paths to exist
# this requires all paths to exist
required_attributes! %W(#{field}.path)
uploads_path = File.realpath(uploads_path)
file_path = File.realpath(params[:file])
file_path = File.realpath(params["#{field}.path"])
bad_request!('Bad file path') unless file_path.start_with?(uploads_path)
UploadedFile.new(
file_path,
params[:filename],
params[:filetype] || 'application/octet-stream',
params["#{field}.name"],
params["#{field}.type"] || 'application/octet-stream',
)
end

View file

@ -53,7 +53,7 @@ module Ci
# Parameters:
# id (required) - The ID of a build
# token (required) - The build authorization token
# size (optional) - the size of uploaded file
# filesize (optional) - the size of uploaded file
# Example Request:
# POST /builds/:id/artifacts/authorize
post ":id/artifacts/authorize" do
@ -77,18 +77,16 @@ module Ci
# Parameters:
# id (required) - The ID of a build
# token (required) - The build authorization token
# file (required) - The uploaded file
# Parameters (accelerated by GitLab Workhorse):
# file.path - path to locally stored body (generated by Workhorse)
# file.name - real filename as send in Content-Disposition
# file.type - real content type as send in Content-Type
# Headers:
# Content-Type - File content type
# Content-Disposition - File media type and real name
# BUILD-TOKEN (required) - The build authorization token, the same as token
# Body:
# The file content
#
# Parameters (set by GitLab Workhorse):
# file - path to locally stored body (generated by Workhorse)
# filename - real filename as send in Content-Disposition
# filetype - real content type as send in Content-Type
# filesize - real file size as send in Content-Length
# Example Request:
# POST /builds/:id/artifacts
post ":id/artifacts" do
@ -98,7 +96,7 @@ module Ci
authenticate_build_token!(build)
forbidden!('build is not running') unless build.running?
file = uploaded_file!(ArtifactUploader.artifacts_upload_path)
file = uploaded_file!(:file, ArtifactUploader.artifacts_upload_path)
file_to_large! unless file.size < max_artifacts_size
if build.update_attributes(artifacts_file: file)

View file

@ -194,8 +194,14 @@ describe Ci::API::API do
build.run!
end
it do
upload_artifacts(file_upload, headers_with_token)
it "uses regual file post" do
upload_artifacts(file_upload, headers_with_token, false)
expect(response.status).to eq(201)
expect(json_response["artifacts_file"]["filename"]).to eq(file_upload.original_filename)
end
it "uses accelerated file post" do
upload_artifacts(file_upload, headers_with_token, true)
expect(response.status).to eq(201)
expect(json_response["artifacts_file"]["filename"]).to eq(file_upload.original_filename)
end
@ -263,12 +269,15 @@ describe Ci::API::API do
end
end
def upload_artifacts(file, headers = {})
params = {
file: file.path,
filename: file.original_filename,
}
post post_url, params, headers
def upload_artifacts(file, headers = {}, accelerated = true)
if accelerated
post post_url, {
'file.path' => file.path,
'file.name' => file.original_filename
}, headers
else
post post_url, { file: file }, headers
end
end
end