ensl.org/app/models/directory.rb

221 lines
6.1 KiB
Ruby
Raw Normal View History

# == Schema Information
#
# Table name: directories
#
# id :integer not null, primary key
# description :string(255)
2020-03-31 03:36:19 +00:00
# hidden :boolean default(FALSE), not null
# name :string(255)
# path :string(255)
2020-03-31 03:36:19 +00:00
# title :string(255)
# created_at :datetime
# updated_at :datetime
# parent_id :integer
#
# Indexes
#
# index_directories_on_parent_id (parent_id)
#
2020-03-31 03:36:19 +00:00
ENV['FILES_ROOT'] ||= File.join(Rails.root, 'public', 'files')
# class SteamIdValidator < ActiveModel::Validator
# def validate(record)
# record.errors.add :steamid unless \
# record.steamid.nil? ||
# (m = record.steamid.match(/\A([01]):([01]):(\d{1,10})\Z/)) &&
# (id = m[3].to_i) &&
# id >= 1 && id <= 2147483647
# end
# end
class Directory < ActiveRecord::Base
include Extra
ROOT = 1
DEMOS = 5
DEMOS_DEFAULT = 19
DEMOS_GATHERS = 92
MOVIES = 30
ARTICLES = 39
#attr_protected :id, :updated_at, :created_at, :path
2020-03-26 02:26:30 +00:00
belongs_to :parent, :class_name => "Directory", :optional => true
has_many :subdirs, :class_name => "Directory", :foreign_key => :parent_id
2019-06-02 01:26:36 +00:00
has_many :files, -> { order("name") }, :class_name => "DataFile"
2019-06-02 01:26:36 +00:00
scope :ordered, -> { order("name ASC") }
2020-03-22 17:47:24 +00:00
scope :filtered, -> { where(hidden: false) }
2019-06-02 01:26:36 +00:00
scope :of_parent, -> (parent) { where(parent_id: parent.id) }
validates_length_of [:name, :path], :in => 1..255
validates_format_of :name, :with => /\A[A-Za-z0-9]{1,20}\z/, :on => :create
validates_length_of :name, :in => 1..25
validates_inclusion_of :hidden, :in => [true, false]
2020-03-31 03:36:19 +00:00
# TODO: add validation for path
before_validation :init_variables
after_create :make_path
after_save :update_timestamp
before_destroy :remove_files
after_destroy :remove_path
def to_s
name
end
def init_variables
2020-03-31 03:36:19 +00:00
self.path = full_path if parent
self.hidden = false if hidden.nil?
end
2020-03-31 03:36:19 +00:00
def path_exists?
File.directory?(full_path)
end
def full_path
parent ? File.join(parent.full_path, name.downcase) : path
end
2020-03-31 03:36:19 +00:00
def make_path
Dir.mkdir(full_path) unless File.exists?(full_path)
end
def update_timestamp
2020-03-31 03:36:19 +00:00
self.created_at = File.mtime(full_path) if File.exists?(full_path)
end
def remove_files
files.each do |subdir|
subdir.destroy
end
subdirs.each do |subdir|
subdir.destroy
end
end
def remove_path
2020-03-31 03:36:19 +00:00
Dir.unlink(full_path) if File.exists?(full_path)
end
# TODO: make tests for this, moving etc.
# TODO: mutate instead of return.
def recreate_transaction(root = ENV['FILES_ROOT'])
logger = Rails.logger
logger.info 'Starting recreate on %d, root: %s' % [id, root]
ActiveRecord::Base.transaction do
# We use destroy lists so technically there can be seperate roots
2020-03-31 17:26:55 +00:00
destroy_dirs = Hash.new
2020-03-31 03:36:19 +00:00
update_attribute :path, root
2020-03-31 17:26:55 +00:00
destroy_dirs = recreate(destroy_dirs)
destroy_dirs.each do |key, dir|
2020-03-31 03:36:19 +00:00
logger.info 'Removed dir: %s' % dir.full_path
# dir.destroy!
end
end
logger.info 'Finish recreate'
return nil
# TODO: check items that weren't checked.
end
2020-03-31 03:36:19 +00:00
# QUESTION Symlinks?
2020-03-31 17:26:55 +00:00
def recreate(destroy_dirs, path = self.full_path)
2020-03-31 03:36:19 +00:00
# Convert all subdirs into a hash and mark them to be deleted
# FIXME: better oneliner
2020-03-31 17:26:55 +00:00
destroy_dirs.merge!(subdirs.all.map{ |s| [s.id,s] }.to_h)
2020-03-31 03:36:19 +00:00
# Go through all subdirectories (no recursion)
2020-03-31 17:26:55 +00:00
Dir.glob("%s/*" % path).each do |subitem_path|
if File.directory? subitem_path
subdir_name = File.basename(subitem_path)
2020-03-31 03:36:19 +00:00
# We find by name only, ignore path
# Find existing subdirs from current path. Keep those we find
2020-03-31 17:26:55 +00:00
if (subdir = find_existing(subdir_name, subitem_path))
2020-03-31 03:36:19 +00:00
if subdir.parent_id != self.id
old_path = subdir.full_path
subdir.parent = self
subdir.save!
logger.info 'Renamed dir: %s -> %s' % [old_path, subdir.full_path]
end
2020-03-31 17:26:55 +00:00
destroy_dirs.delete subdir.id
2020-03-31 03:36:19 +00:00
# In case its a new directory
else
# Attempt to find it in existing directories
subdir = subdirs.build(name: subdir_name)
# FIXME: find a better solution
subdir.save!(validate: false)
logger.info 'New dir: %s' % subdir.full_path
end
# Recreate the directory
2020-03-31 17:26:55 +00:00
destroy_dirs = subdir.recreate(destroy_dirs)
elsif File.file? subitem_path
if dbfile = DataFile.find_existing(subitem_path, subitem_name)
dbfile.directory = self
dbfile.save!
elsif (File.mtime(file) + 100).past?
dbfile = DataFile.new
dbfile.path = file
dbfile.directory = self
dbfile.save!
end
# TODO: handle files that are only in database
2020-03-31 03:36:19 +00:00
end
end
2020-03-31 17:26:55 +00:00
return destroy_dirs
2020-03-31 03:36:19 +00:00
end
2020-03-31 17:26:55 +00:00
def find_existing(subdir_name, subitem_path)
2020-03-31 03:36:19 +00:00
# Find by name
if (dir = subdirs.where(name: subdir_name)).exists?
return dir.first
# Problem is we can't find it if haven't got that far
else
Directory.where(name: subdir_name).all.each do |dir|
unless dir.path_exists?
return dir
end
end
2020-03-31 17:26:55 +00:00
# TODO: use filter_map here
# NOTE: we don't use the logic from dat_file
file_count = Dir["%s/*" % subitem_path].count{|f| File.file?(f) }
Directory.joins(:files).group('data_files.directory_id')\
.having('count(data_files.id) = ? and count(data_files.id) > 0', file_count).each do |dir|
Dir.glob(File.join(dir.full_path, '*')).each do |filename|
return false if File.size(file) != dir.files.where(name: filename).first&.size
end
return dir
end
2020-03-31 03:36:19 +00:00
# TODO: Find by number of files + hash of files
end
return false
end
2020-03-31 17:26:55 +00:00
# TODO
def recreate_check
true
end
2020-03-31 17:26:55 +00:00
# TODO check that you can download files
def can_create? cuser
cuser and cuser.admin?
end
def can_update? cuser, params = {}
cuser and cuser.admin? and Verification.contain params, [:description, :hidden]
end
def can_destroy? cuser
cuser and cuser.admin?
end
def self.params(params, cuser)
params.require(:directory).permit(:description, :hidden, :name, :parent_id)
end
end