mirror of
https://github.com/neocities/neocities.git
synced 2025-04-24 17:22:35 +02:00
use site_files for storing directory information
This commit is contained in:
parent
f753b66ffd
commit
4502771702
3 changed files with 79 additions and 23 deletions
22
Rakefile
22
Rakefile
|
@ -218,23 +218,31 @@ end
|
|||
|
||||
desc 'prime_site_files'
|
||||
task :prime_site_files => [:environment] do
|
||||
Site.where(is_banned: false).select(:id, :username).all.each do |site|
|
||||
Site.where(is_banned: false).where(is_deleted: false).select(:id, :username).all.each do |site|
|
||||
Dir.glob(File.join(site.files_path, '**/*')).each do |file|
|
||||
next unless site.username == 'kyledrake'
|
||||
path = file.gsub(site.base_files_path, '').sub(/^\//, '')
|
||||
|
||||
site_file = site.site_files_dataset[path: path]
|
||||
|
||||
if site_file.nil?
|
||||
next if File.directory? file
|
||||
mtime = File.mtime file
|
||||
site.add_site_file(
|
||||
|
||||
site_file_opts = {
|
||||
path: path,
|
||||
size: File.size(file),
|
||||
sha1_hash: Digest::SHA1.file(file).hexdigest,
|
||||
updated_at: mtime,
|
||||
created_at: mtime
|
||||
)
|
||||
}
|
||||
|
||||
if File.directory? file
|
||||
site_file_opts.merge! is_directory: true
|
||||
else
|
||||
site_file_opts.merge!(
|
||||
size: File.size(file),
|
||||
sha1_hash: Digest::SHA1.file(file).hexdigest
|
||||
)
|
||||
end
|
||||
|
||||
site.add_site_file site_file_opts
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -671,6 +671,30 @@ class Site < Sequel::Model
|
|||
return 'Directory (or file) already exists.'
|
||||
end
|
||||
|
||||
path_dirs = path.to_s.split '/'
|
||||
|
||||
path_site_file = ''
|
||||
|
||||
until path_dirs.empty?
|
||||
if path_site_file == ''
|
||||
path_site_file += path_dirs.shift
|
||||
else
|
||||
path_site_file += '/' + path_dirs.shift
|
||||
end
|
||||
|
||||
site_file = SiteFile.where(site_id: self.id, path: path_site_file).first
|
||||
|
||||
if site_file.nil?
|
||||
SiteFile.create(
|
||||
site_id: self.id,
|
||||
path: path_site_file,
|
||||
is_directory: true,
|
||||
created_at: Time.now,
|
||||
updated_at: Time.now
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
FileUtils.mkdir_p relative_path
|
||||
true
|
||||
end
|
||||
|
@ -914,7 +938,7 @@ class Site < Sequel::Model
|
|||
path ||= ''
|
||||
clean = []
|
||||
|
||||
parts = path.split '/'
|
||||
parts = path.to_s.split '/'
|
||||
|
||||
parts.each do |part|
|
||||
next if part.empty? || part == '.'
|
||||
|
@ -1243,7 +1267,7 @@ class Site < Sequel::Model
|
|||
FileUtils.rm files_path(path)
|
||||
rescue Errno::EISDIR
|
||||
site_files.each do |site_file|
|
||||
if site_file.path.match /^#{path}\//
|
||||
if site_file.path.match(/^#{path}\//) || site_file.path == path
|
||||
site_file.destroy
|
||||
end
|
||||
end
|
||||
|
@ -1262,7 +1286,7 @@ class Site < Sequel::Model
|
|||
|
||||
DB.transaction do
|
||||
site_file = site_files_dataset.where(path: path).first
|
||||
if site_file
|
||||
if site_file && !site_file.size.nil? && !site_file.is_directory
|
||||
DB['update sites set space_used=space_used-? where id=?', site_file.size, self.id].first
|
||||
site_file.delete
|
||||
end
|
||||
|
@ -1287,6 +1311,15 @@ class Site < Sequel::Model
|
|||
return false
|
||||
end
|
||||
|
||||
if pathname.extname.match HTML_REGEX
|
||||
# SPAM and phishing checking code goes here
|
||||
end
|
||||
|
||||
relative_path_dir = Pathname(relative_path).dirname
|
||||
create_directory relative_path_dir unless relative_path_dir == '.'
|
||||
|
||||
uploaded_size = uploaded.size
|
||||
|
||||
if relative_path == 'index.html'
|
||||
begin
|
||||
new_title = Nokogiri::HTML(File.read(uploaded.path)).css('title').first.text
|
||||
|
@ -1299,18 +1332,6 @@ class Site < Sequel::Model
|
|||
end
|
||||
end
|
||||
|
||||
if pathname.extname.match HTML_REGEX
|
||||
# SPAM and phishing checking code goes here
|
||||
end
|
||||
|
||||
dirname = pathname.dirname.to_s
|
||||
|
||||
if !File.exists? dirname
|
||||
FileUtils.mkdir_p dirname
|
||||
end
|
||||
|
||||
uploaded_size = uploaded.size
|
||||
|
||||
FileUtils.cp uploaded.path, path
|
||||
File.chmod 0640, path
|
||||
|
||||
|
@ -1332,6 +1353,7 @@ class Site < Sequel::Model
|
|||
elsif pathname.extname.match IMAGE_REGEX
|
||||
ThumbnailWorker.perform_async values[:username], relative_path
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
end
|
||||
|
|
|
@ -69,8 +69,30 @@ describe 'site_files' do
|
|||
'files[]' => Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg')
|
||||
)
|
||||
delete_file filename: 'test'
|
||||
@site.site_files.select {|f| f.path == 'test'}.length.must_equal 0
|
||||
@site.site_files.select {|f| f.path =~ /^test\//}.length.must_equal 0
|
||||
@site.site_files.select {|f| f.path =~ /^test/}.length.must_equal 1
|
||||
@site.site_files.select {|f| f.path =~ /^test.jpg/}.length.must_equal 1
|
||||
end
|
||||
|
||||
it 'deletes records for nested directories' do
|
||||
upload(
|
||||
'dir' => 'derp/ing/tons',
|
||||
'files[]' => Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg')
|
||||
)
|
||||
|
||||
expected_site_file_paths = ['derp', 'derp/ing', 'derp/ing/tons', 'derp/ing/tons/test.jpg']
|
||||
|
||||
expected_site_file_paths.each do |path|
|
||||
@site.site_files.select {|f| f.path == path}.length.must_equal 1
|
||||
end
|
||||
|
||||
delete_file filename: 'derp'
|
||||
|
||||
@site.reload
|
||||
|
||||
expected_site_file_paths.each do |path|
|
||||
@site.site_files.select {|f| f.path == path}.length.must_equal 0
|
||||
end
|
||||
end
|
||||
|
||||
it 'goes back to deleting directory' do
|
||||
|
@ -248,6 +270,10 @@ describe 'site_files' do
|
|||
ThumbnailWorker.jobs.length.must_equal 1
|
||||
ThumbnailWorker.drain
|
||||
|
||||
@site.site_files_dataset.where(path: 'derpie').count.must_equal 1
|
||||
@site.site_files_dataset.where(path: 'derpie/derptest').count.must_equal 1
|
||||
@site.site_files_dataset.where(path: 'derpie/derptest/test.jpg').count.must_equal 1
|
||||
|
||||
Site::THUMBNAIL_RESOLUTIONS.each do |resolution|
|
||||
File.exists?(@site.thumbnail_path('derpie/derptest/test.jpg', resolution)).must_equal true
|
||||
@site.thumbnail_url('derpie/derptest/test.jpg', resolution).must_equal(
|
||||
|
|
Loading…
Add table
Reference in a new issue