Skip to content

Commit

Permalink
Copy over initial upload ui by Gerard Hickey
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilja Bobkevic committed Sep 2, 2015
1 parent 3ab3e0a commit 6c5bd47
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/puppet_forge_server/app/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ def initialize(root, http_client = PuppetForgeServer::Http::HttpClient.new)
haml :modules, :locals => {:query => query, :modules => modules}
end

get '/upload' do
haml :upload, :locals => {:upload_status => ''}
end

post '/upload' do
halt(200, haml(:upload, :locals => {:upload_status => 'No file selected'})) unless params[:file]
response = @http_client.post_file("#{request.base_url}/v2/releases", params[:file])
haml :upload, :locals => {:upload_status => response.code}
end

private
def get(relative_url)
begin
Expand Down
3 changes: 3 additions & 0 deletions lib/puppet_forge_server/app/views/layout.haml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
%ul.nav.navbar-nav.navbar-right
%li.active
%a{:href => 'https://forge.puppetlabs.com/', :target => 'official-puppet-forge'} Official Puppet Forge
%li
%a{:href => '/upload', :target => 'Upload Puppet module'}
Upload Puppet Module
%li
%a{:href => 'https://github.com/unibet/puppet-forge-server', :target => 'puppet-forge-server-github'} Help
%script{ :src => 'https://code.jquery.com/jquery-2.1.3.min.js' }
Expand Down
45 changes: 45 additions & 0 deletions lib/puppet_forge_server/app/views/upload.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-# -*- encoding: utf-8 -*-
-#
-# Copyright 2015 North Development AB
-#
-# Author: Gerard Hickey
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
/ TODO Need better formatting
/ TODO Better error notification
/ TODO Information on programatic uploads
%h3 Upload Puppet Module

Specify below the location of the Puppet module .tar.gz file that you wish to publish.

The Puppet module .tar.gz file should be generated by executing

%div
%pre
puppet module build

The result will be a pkg directory at the top level of the module with the .tag.gz within it.

%span.search_error
-if upload_status
-case upload_status
-when '200'
File has been uploaded
-when ''
-else
Upload has failed. #{upload_status}

%form{:action => '/upload', :method=>'post', :enctype=>'multipart/form-data' }
%input{ :type => 'file', :name=>'file'}
%input{ :type=>'submit', :value=>'Upload'}
17 changes: 17 additions & 0 deletions lib/puppet_forge_server/http/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,26 @@
require 'open-uri'
require 'open_uri_redirections'
require 'timeout'
require 'net/http'
require 'net/http/post/multipart'


module PuppetForgeServer::Http
class HttpClient

def post_file(url, file_hash, options = {})
options = { :http => {}, :headers => {}}.merge(options)

uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
options[:http].each {|k,v| http.call(k, v) }

req = Net::HTTP::Post::Multipart.new uri.path, "file" => UploadIO.new(File.open(file_hash[:tempfile]), file_hash[:type], file_hash[:filename])
options[:headers].each {|k,v| req[k] = v }

http.request(req)
end

def get(url)
open_uri(url).read
end
Expand Down
1 change: 1 addition & 0 deletions puppet-forge-server.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'open_uri_redirections', '~> 0.1'
spec.add_dependency 'haml', '~> 4.0'
spec.add_dependency 'deep_merge', '~> 1.0'
spec.add_dependency 'multipart-post', '~> 2.0.0'

spec.add_development_dependency 'rake', '~> 10.3'
spec.add_development_dependency 'rspec', '~> 3.1'
Expand Down

0 comments on commit 6c5bd47

Please sign in to comment.