FileTooBig

This commit is contained in:
Karchnu 2020-10-22 01:21:44 +02:00
parent edc7cdb6f3
commit 1a8f95320e
4 changed files with 35 additions and 0 deletions

View File

@ -20,6 +20,7 @@ class FileStorage::Client < IPC::Client
em << FileStorage::Errors::FileExists em << FileStorage::Errors::FileExists
em << FileStorage::Errors::FileDoesNotExist em << FileStorage::Errors::FileDoesNotExist
em << FileStorage::Errors::FileFullyUploaded em << FileStorage::Errors::FileFullyUploaded
em << FileStorage::Errors::FileTooBig
em.parse_ipc_json message em.parse_ipc_json message
end end
end end

View File

@ -71,4 +71,14 @@ class FileStorage::Errors
end end
end end
FileStorage.errors << FileFullyUploaded FileStorage.errors << FileFullyUploaded
IPC::JSON.message FileTooBig, 207 do
property mid : String
property reason = "file too big"
property limit : UInt64
def initialize(@mid, @limit)
end
end
FileStorage.errors << FileTooBig
end end

View File

@ -12,6 +12,8 @@ class FileStorage::Request
raise NotLoggedException.new if user.nil? raise NotLoggedException.new if user.nil?
raise FileTooBig.new if @file.size > filestoraged.max_file_size
# FIXME: Maybe this should be moved to FileStorage::Service # FIXME: Maybe this should be moved to FileStorage::Service
fd = event.fd fd = event.fd

View File

@ -44,6 +44,8 @@ module FileStorage
end end
class AdminAuthorizationException < ::Exception class AdminAuthorizationException < ::Exception
end end
class FileTooBig < ::Exception
end
end end
@ -61,6 +63,8 @@ class FileStorage::Service < IPC::Server
getter logged_users : Hash(Int32, AuthD::User::Public) getter logged_users : Hash(Int32, AuthD::User::Public)
getter all_connections : Array(Int32) getter all_connections : Array(Int32)
property max_file_size : UInt64 = 10_000_000 # Bytes
@auth : AuthD::Client @auth : AuthD::Client
@auth_key : String @auth_key : String
@ -127,6 +131,9 @@ class FileStorage::Service < IPC::Server
rescue e : NotLoggedException rescue e : NotLoggedException
Baguette::Log.warning "#{request_name} user not logged" Baguette::Log.warning "#{request_name} user not logged"
Errors::GenericError.new request_id, "user not logged" Errors::GenericError.new request_id, "user not logged"
rescue e : FileTooBig
Baguette::Log.warning "#{request_name} file too big: #{e.message}"
Errors::FileTooBig.new request_id, @max_file_size
rescue e rescue e
Baguette::Log.error "#{request_name} generic error #{e}" Baguette::Log.error "#{request_name} generic error #{e}"
Errors::GenericError.new request_id, "unexpected error" Errors::GenericError.new request_id, "unexpected error"
@ -206,6 +213,8 @@ class FileStorage::Service < IPC::Server
reindex = false reindex = false
max_file_size : UInt64 = 10_000_000 # default, 10 MB
OptionParser.parse do |parser| OptionParser.parse do |parser|
parser.banner = "usage: filestoraged [options]" parser.banner = "usage: filestoraged [options]"
@ -213,26 +222,38 @@ class FileStorage::Service < IPC::Server
"--root-directory dir", "--root-directory dir",
"The root directory for FileStoraged." do |opt| "The root directory for FileStoraged." do |opt|
storage_directory = opt storage_directory = opt
Baguette::Log.info "Storage directory: #{storage_directory}"
end end
parser.on "-t timer", parser.on "-t timer",
"--timer timer", "--timer timer",
"Timer. Default: 30 000 (30 seconds)." do |t| "Timer. Default: 30 000 (30 seconds)." do |t|
timer = t.to_i timer = t.to_i
Baguette::Log.info "Timer: #{timer}"
end end
parser.on "-v verbosity", parser.on "-v verbosity",
"--verbosity level", "--verbosity level",
"Verbosity level. From 0 to 3. Default: 1" do |v| "Verbosity level. From 0 to 3. Default: 1" do |v|
Baguette::Context.verbosity = v.to_i Baguette::Context.verbosity = v.to_i
Baguette::Log.info "Verbosity: #{v}"
end end
parser.on "-I", parser.on "-I",
"--re-index", "--re-index",
"Reindex the database." do "Reindex the database." do
Baguette::Log.info "Re-index everything!"
reindex = true reindex = true
end end
parser.on "-m size",
"--max-size size",
"Maximum file size (KB). Default: 10 MB." do |s|
Baguette::Log.info "Maximum file size: #{s}"
max_file_size = s.to_u64 * 1000
end
parser.on "-h", parser.on "-h",
"--help", "--help",
"Displays this help and exits." do "Displays this help and exits." do
@ -251,6 +272,7 @@ class FileStorage::Service < IPC::Server
service = ::FileStorage::Service.new storage_directory, key, reindex service = ::FileStorage::Service.new storage_directory, key, reindex
service.base_timer = timer service.base_timer = timer
service.timer = timer service.timer = timer
service.max_file_size = max_file_size
service service
end end