Skip to content

Commit 6c5bd47

Browse files
author
Ilja Bobkevic
committed
Copy over initial upload ui by Gerard Hickey
1 parent 3ab3e0a commit 6c5bd47

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

lib/puppet_forge_server/app/frontend.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ def initialize(root, http_client = PuppetForgeServer::Http::HttpClient.new)
4747
haml :modules, :locals => {:query => query, :modules => modules}
4848
end
4949

50+
get '/upload' do
51+
haml :upload, :locals => {:upload_status => ''}
52+
end
53+
54+
post '/upload' do
55+
halt(200, haml(:upload, :locals => {:upload_status => 'No file selected'})) unless params[:file]
56+
response = @http_client.post_file("#{request.base_url}/v2/releases", params[:file])
57+
haml :upload, :locals => {:upload_status => response.code}
58+
end
59+
5060
private
5161
def get(relative_url)
5262
begin

lib/puppet_forge_server/app/views/layout.haml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
%ul.nav.navbar-nav.navbar-right
4040
%li.active
4141
%a{:href => 'https://forge.puppetlabs.com/', :target => 'official-puppet-forge'} Official Puppet Forge
42+
%li
43+
%a{:href => '/upload', :target => 'Upload Puppet module'}
44+
Upload Puppet Module
4245
%li
4346
%a{:href => 'https://github.com/unibet/puppet-forge-server', :target => 'puppet-forge-server-github'} Help
4447
%script{ :src => 'https://code.jquery.com/jquery-2.1.3.min.js' }
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-# -*- encoding: utf-8 -*-
2+
-#
3+
-# Copyright 2015 North Development AB
4+
-#
5+
-# Author: Gerard Hickey
6+
-#
7+
-# Licensed under the Apache License, Version 2.0 (the "License");
8+
-# you may not use this file except in compliance with the License.
9+
-# You may obtain a copy of the License at
10+
-#
11+
-# http://www.apache.org/licenses/LICENSE-2.0
12+
-#
13+
-# Unless required by applicable law or agreed to in writing, software
14+
-# distributed under the License is distributed on an "AS IS" BASIS,
15+
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
-# See the License for the specific language governing permissions and
17+
-# limitations under the License.
18+
19+
/ TODO Need better formatting
20+
/ TODO Better error notification
21+
/ TODO Information on programatic uploads
22+
%h3 Upload Puppet Module
23+
24+
Specify below the location of the Puppet module .tar.gz file that you wish to publish.
25+
26+
The Puppet module .tar.gz file should be generated by executing
27+
28+
%div
29+
%pre
30+
puppet module build
31+
32+
The result will be a pkg directory at the top level of the module with the .tag.gz within it.
33+
34+
%span.search_error
35+
-if upload_status
36+
-case upload_status
37+
-when '200'
38+
File has been uploaded
39+
-when ''
40+
-else
41+
Upload has failed. #{upload_status}
42+
43+
%form{:action => '/upload', :method=>'post', :enctype=>'multipart/form-data' }
44+
%input{ :type => 'file', :name=>'file'}
45+
%input{ :type=>'submit', :value=>'Upload'}

lib/puppet_forge_server/http/http_client.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,26 @@
1717
require 'open-uri'
1818
require 'open_uri_redirections'
1919
require 'timeout'
20+
require 'net/http'
21+
require 'net/http/post/multipart'
22+
2023

2124
module PuppetForgeServer::Http
2225
class HttpClient
26+
27+
def post_file(url, file_hash, options = {})
28+
options = { :http => {}, :headers => {}}.merge(options)
29+
30+
uri = URI.parse(url)
31+
http = Net::HTTP.new(uri.host, uri.port)
32+
options[:http].each {|k,v| http.call(k, v) }
33+
34+
req = Net::HTTP::Post::Multipart.new uri.path, "file" => UploadIO.new(File.open(file_hash[:tempfile]), file_hash[:type], file_hash[:filename])
35+
options[:headers].each {|k,v| req[k] = v }
36+
37+
http.request(req)
38+
end
39+
2340
def get(url)
2441
open_uri(url).read
2542
end

puppet-forge-server.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Gem::Specification.new do |spec|
4343
spec.add_dependency 'open_uri_redirections', '~> 0.1'
4444
spec.add_dependency 'haml', '~> 4.0'
4545
spec.add_dependency 'deep_merge', '~> 1.0'
46+
spec.add_dependency 'multipart-post', '~> 2.0.0'
4647

4748
spec.add_development_dependency 'rake', '~> 10.3'
4849
spec.add_development_dependency 'rspec', '~> 3.1'

0 commit comments

Comments
 (0)