stream site download zip

This commit is contained in:
Kyle Drake 2023-08-08 15:45:05 -05:00
parent d16bc0bace
commit 6c4a80123c
3 changed files with 19 additions and 17 deletions

View file

@ -189,15 +189,25 @@ end
get '/site_files/:username.zip' do |username| get '/site_files/:username.zip' do |username|
require_login require_login
if current_site.too_big_to_download? content_type 'application/zip'
flash[:error] = 'Cannot download site as zip as it is too large (or contains too many files)'
redirect '/dashboard'
end
zipfile_path = current_site.files_zip
content_type 'application/octet-stream'
attachment "neocities-#{current_site.username}.zip" attachment "neocities-#{current_site.username}.zip"
send_file zipfile_path
directory_path = current_site.files_path
stream do |out|
ZipTricks::Streamer.open(out) do |zip|
Dir["#{directory_path}/**/*"].each do |file|
next if File.directory?(file)
zip_path = file.sub("#{directory_path}/", '')
zip.write_deflated_file(zip_path) do |file_writer|
File.open(file, 'rb') do |file|
IO.copy_stream(file, file_writer)
end
end
end
end
end
end end
get %r{\/site_files\/download\/(.+)} do get %r{\/site_files\/download\/(.+)} do

View file

@ -83,8 +83,6 @@ class Site < Sequel::Model
THUMBNAIL_RESOLUTIONS = ['210x158'] THUMBNAIL_RESOLUTIONS = ['210x158']
MAX_FILE_SIZE = 10**8 # 100 MB MAX_FILE_SIZE = 10**8 # 100 MB
MAX_SITE_DOWNLOAD_SIZE = 200_000_000 # 200MB
MAX_SITE_FILES_DOWNLOAD = 500
CLAMAV_THREAT_MATCHES = [ CLAMAV_THREAT_MATCHES = [
/^VBS/, /^VBS/,
@ -1353,10 +1351,6 @@ class Site < Sequel::Model
((total_space_used.to_f / maximum_space) * 100).round(1) ((total_space_used.to_f / maximum_space) * 100).round(1)
end end
def too_big_to_download?
space_used > MAX_SITE_DOWNLOAD_SIZE || site_files_dataset.count > MAX_SITE_FILES_DOWNLOAD
end
# Note: Change Stat#prune! and the nginx map compiler if you change this business logic. # Note: Change Stat#prune! and the nginx map compiler if you change this business logic.
def supporter? def supporter?
owner.plan_type != 'free' owner.plan_type != 'free'

View file

@ -218,9 +218,7 @@
<% if !current_site.plan_feature(:no_file_restrictions) %> <% if !current_site.plan_feature(:no_file_restrictions) %>
<a href="/site_files/allowed_types">Allowed file types</a> | <a href="/site_files/allowed_types">Allowed file types</a> |
<% end %> <% end %>
<% unless current_site.too_big_to_download? %>
<a href="/site_files/<%= current_site.username %>.zip">Download entire site</a> | <a href="/site_files/<%= current_site.username %>.zip">Download entire site</a> |
<% end %>
<% unless is_education? %> <% unless is_education? %>
<a href="/site_files/mount_info">Mount your site as a drive on your computer</a> <a href="/site_files/mount_info">Mount your site as a drive on your computer</a>
<% end %> <% end %>