From 1a8f95320e09055de6a903188c0ad95d4e529d80 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Thu, 22 Oct 2020 01:21:44 +0200 Subject: [PATCH] FileTooBig --- src/client/lib.cr | 1 + src/requests/errors.cr | 10 ++++++++++ src/requests/upload.cr | 2 ++ src/server/main.cr | 22 ++++++++++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/src/client/lib.cr b/src/client/lib.cr index e99716d..797fb33 100644 --- a/src/client/lib.cr +++ b/src/client/lib.cr @@ -20,6 +20,7 @@ class FileStorage::Client < IPC::Client em << FileStorage::Errors::FileExists em << FileStorage::Errors::FileDoesNotExist em << FileStorage::Errors::FileFullyUploaded + em << FileStorage::Errors::FileTooBig em.parse_ipc_json message end end diff --git a/src/requests/errors.cr b/src/requests/errors.cr index f8afcfb..39686e9 100644 --- a/src/requests/errors.cr +++ b/src/requests/errors.cr @@ -71,4 +71,14 @@ class FileStorage::Errors end end 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 diff --git a/src/requests/upload.cr b/src/requests/upload.cr index 1a7a453..2fd97e3 100644 --- a/src/requests/upload.cr +++ b/src/requests/upload.cr @@ -12,6 +12,8 @@ class FileStorage::Request 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 fd = event.fd diff --git a/src/server/main.cr b/src/server/main.cr index 74d579b..4c29ae4 100644 --- a/src/server/main.cr +++ b/src/server/main.cr @@ -44,6 +44,8 @@ module FileStorage end class AdminAuthorizationException < ::Exception end + class FileTooBig < ::Exception + end end @@ -61,6 +63,8 @@ class FileStorage::Service < IPC::Server getter logged_users : Hash(Int32, AuthD::User::Public) getter all_connections : Array(Int32) + property max_file_size : UInt64 = 10_000_000 # Bytes + @auth : AuthD::Client @auth_key : String @@ -127,6 +131,9 @@ class FileStorage::Service < IPC::Server rescue e : NotLoggedException Baguette::Log.warning "#{request_name} 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 Baguette::Log.error "#{request_name} generic error #{e}" Errors::GenericError.new request_id, "unexpected error" @@ -206,6 +213,8 @@ class FileStorage::Service < IPC::Server reindex = false + max_file_size : UInt64 = 10_000_000 # default, 10 MB + OptionParser.parse do |parser| parser.banner = "usage: filestoraged [options]" @@ -213,26 +222,38 @@ class FileStorage::Service < IPC::Server "--root-directory dir", "The root directory for FileStoraged." do |opt| storage_directory = opt + Baguette::Log.info "Storage directory: #{storage_directory}" end parser.on "-t timer", "--timer timer", "Timer. Default: 30 000 (30 seconds)." do |t| timer = t.to_i + Baguette::Log.info "Timer: #{timer}" end parser.on "-v verbosity", "--verbosity level", "Verbosity level. From 0 to 3. Default: 1" do |v| Baguette::Context.verbosity = v.to_i + Baguette::Log.info "Verbosity: #{v}" end parser.on "-I", "--re-index", "Reindex the database." do + Baguette::Log.info "Re-index everything!" reindex = true 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", "--help", "Displays this help and exits." do @@ -251,6 +272,7 @@ class FileStorage::Service < IPC::Server service = ::FileStorage::Service.new storage_directory, key, reindex service.base_timer = timer service.timer = timer + service.max_file_size = max_file_size service end