mirror of
https://github.com/manyfold3d/manyfold.git
synced 2026-10-22 03:32:18 -05:00
24 lines
1.3 KiB
Ruby
24 lines
1.3 KiB
Ruby
class ReadWriteValidator < ActiveModel::EachValidator
|
|
def validate_each(record, attribute, value)
|
|
return if value.nil?
|
|
# i18n-tasks-use t("activerecord.errors.models.library.attributes.path.not_a_directory")
|
|
record.errors.add attribute, :not_a_directory unless FileTest.directory?(value)
|
|
# i18n-tasks-use t("activerecord.errors.models.library.attributes.path.non_readable")
|
|
record.errors.add attribute, :non_readable unless FileTest.readable?(value)
|
|
# i18n-tasks-use t("activerecord.errors.models.library.attributes.path.non_writable")
|
|
record.errors.add attribute, :non_writable unless FileTest.writable?(value)
|
|
# Make sure subfolder permissions are OK as well
|
|
if File.exist?(value) && record.errors.empty?
|
|
# Use Dir.glob because it leaves out hidden files
|
|
Dir.glob(File.join(value, "*")).each do |path|
|
|
if FileTest.directory?(path)
|
|
# i18n-tasks-use t("activerecord.errors.models.library.attributes.path.non_readable_subfolder")
|
|
record.errors.add attribute, :non_readable_subfolder unless FileTest.readable?(path)
|
|
# i18n-tasks-use t("activerecord.errors.models.library.attributes.path.non_writable_subfolder")
|
|
record.errors.add attribute, :non_writable_subfolder unless FileTest.writable?(path)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|