Ability to get zipball of entire site

This commit is contained in:
Kyle Drake 2013-06-10 13:37:59 -07:00
parent 9ed826486d
commit a4c31d4618
5 changed files with 41 additions and 5 deletions

View file

@ -8,6 +8,7 @@ gem 'bcrypt-ruby', require: 'bcrypt'
gem 'sinatra-flash', require: 'sinatra/flash'
gem 'sinatra-xsendfile', require: 'sinatra/xsendfile'
gem 'puma', require: nil
gem 'rubyzip'
platform :mri do
gem 'magic' # sudo apt-get install file, For OSX: brew install libmagic

View file

@ -60,6 +60,7 @@ GEM
raindrops (0.10.0)
rake (10.0.4)
redis (3.0.4)
rubyzip (0.9.9)
sequel (3.47.0)
sequel_pg (1.6.6)
pg (>= 0.8.0)
@ -116,6 +117,7 @@ DEPENDENCIES
rake
redis
ruby-debug
rubyzip
sequel
sequel_pg
shotgun

25
app.rb
View file

@ -148,6 +148,27 @@ post '/site_files/delete' do
redirect '/dashboard'
end
get '/site_files/:username.zip' do |username|
file_path = "/tmp/neocities-site-#{username}.zip"
Zip::ZipFile.open(file_path, Zip::ZipFile::CREATE) do |zipfile|
current_site.file_list.collect {|f| f.filename}.each do |filename|
puts filename
puts site_file_path(filename)
zipfile.add filename, site_file_path(filename)
end
end
# I don't want to have to deal with cleaning up old tmpfiles
zipfile = File.read file_path
File.delete file_path
content_type 'application/octet-stream'
attachment "#{current_site.username}.zip"
return zipfile
end
get '/site_files/download/:filename' do |filename|
send_file File.join(site_base_path(current_site.username), filename), filename: filename, type: 'Application/octet-stream'
end
@ -210,6 +231,10 @@ def site_base_path(subname)
File.join settings.public_folder, 'sites', subname
end
def site_file_path(filename)
File.join(site_base_path(current_site.username), filename)
end
def template_site_title(username)
"#{username.capitalize}#{username[username.length-1] == 's' ? "'" : "'s"} Site"
end

View file

@ -6,6 +6,7 @@ Encoding.default_internal = 'UTF-8'
require 'yaml'
require 'json'
require 'logger'
require 'zip/zip'
Bundler.require
Bundler.require :development if ENV['RACK_ENV'] == 'development'

View file

@ -50,13 +50,20 @@ javascript:
<a href="/site_files/new" class="btn btn-large btn-success">Upload New File</a>
.span5
h3 style="color: green": a href="http://#{current_site.username}.neocities.org" target="_blank" http://#{current_site.username}.neocities.org
br
.row
.span5
.progress.progress-info.progress-striped
.bar style="width: #{(current_site.total_space / Site::MAX_SPACE) * 100}%"
h3 style="color: green": a href="http://#{current_site.username}.neocities.org" target="_blank" http://#{current_site.username}.neocities.org
br
h4 You are currently using #{((current_site.total_space.to_f / Site::MAX_SPACE) * 100).round(1)}% (#{(current_site.total_space.to_f / 2**20).round(2)}MB) of your #{(Site::MAX_SPACE.to_f / 2**20).to_i}MB of free space.
.progress.progress-info.progress-striped
.bar style="width: #{(current_site.total_space / Site::MAX_SPACE) * 100}%"
h4 You are currently using #{((current_site.total_space.to_f / Site::MAX_SPACE) * 100).round(1)}% (#{(current_site.total_space.to_f / 2**20).round(2)}MB) of your #{(Site::MAX_SPACE.to_f / 2**20).to_i}MB of free space.
.row style="margin-top: 20px"
.span5
h4: a href="/site_files/#{current_site.username}.zip" Download Entire Site
form method="POST" action="/site_files/delete" id="deleteFilenameForm"
input type="hidden" id="deleteFilenameInput" name="filename"