diff --git a/app/controllers/admin/settings_controller.rb b/app/controllers/admin/settings_controller.rb new file mode 100644 index 0000000..b5654c5 --- /dev/null +++ b/app/controllers/admin/settings_controller.rb @@ -0,0 +1,20 @@ +class Admin::SettingsController < ApplicationController + before_action :require_admin! + + def edit + @values = Setting::INTEGERS.keys.index_with { Setting.integer(_1) } + end + + def update + submitted = params.fetch(:settings, {}).permit(*Setting::INTEGERS.keys).to_h + records = Setting.update_integers(submitted) + @errors = records.flat_map { _1.errors.full_messages } + if @errors.empty? + Rails.logger.info("[settings] #{current_user.email_address} changed #{submitted.inspect}") + redirect_to edit_admin_settings_path, notice: "Settings saved." + else + @values = Setting::INTEGERS.keys.index_with { Setting.integer(_1) }.merge(submitted) + render :edit, status: :unprocessable_entity + end + end +end diff --git a/app/controllers/direct_uploads_controller.rb b/app/controllers/direct_uploads_controller.rb index 4585809..655c85c 100644 --- a/app/controllers/direct_uploads_controller.rb +++ b/app/controllers/direct_uploads_controller.rb @@ -17,8 +17,8 @@ class DirectUploadsController < ActiveStorage::DirectUploadsController error = if !UploadValidation::ALLOWED_CONTENT_TYPES.include?(blob_args[:content_type]) "#{blob_args[:filename]} isn't an allowed file type" - elsif blob_args[:byte_size].to_i > UploadValidation::MAX_SIZE - "#{blob_args[:filename]} is over #{UploadValidation::MAX_SIZE / 1.megabyte} MB" + elsif blob_args[:byte_size].to_i > UploadValidation.max_size + "#{blob_args[:filename]} is over the #{UploadValidation.max_size / 1.megabyte} MB upload limit" end render json: { error: error }, status: :unprocessable_entity if error end diff --git a/app/javascript/controllers/bulk_upload_controller.js b/app/javascript/controllers/bulk_upload_controller.js index 8e3d322..9c2bb3d 100644 --- a/app/javascript/controllers/bulk_upload_controller.js +++ b/app/javascript/controllers/bulk_upload_controller.js @@ -29,7 +29,7 @@ export default class extends Controller { add(event) { for (const file of event.target.files) { const item = { file, path: file.webkitRelativePath || file.name, state: "pending", progress: 0 } - if (file.size > this.maxBytesValue) Object.assign(item, { state: "error", error: `over ${Math.round(this.maxBytesValue / 1048576)} MB` }) + if (file.size > this.maxBytesValue) Object.assign(item, { state: "error", error: `over the ${Math.round(this.maxBytesValue / 1048576)} MB limit` }) this.items.push(item) } event.target.value = "" diff --git a/app/models/concerns/upload_validation.rb b/app/models/concerns/upload_validation.rb index 765f323..8ebde41 100644 --- a/app/models/concerns/upload_validation.rb +++ b/app/models/concerns/upload_validation.rb @@ -7,7 +7,9 @@ module UploadValidation application/pdf text/plain application/gpx+xml application/xml text/xml text/csv application/vnd.google-earth.kml+xml application/octet-stream ].freeze - MAX_SIZE = 50.megabytes + + # Set by admins (Admin → Settings), default 50 MB. + def self.max_size = Setting.max_upload_bytes class_methods do def validates_uploads(*names) @@ -18,7 +20,8 @@ module UploadValidation unless ALLOWED_CONTENT_TYPES.include?(att.blob.content_type) errors.add(name, "#{att.filename} is not an allowed file type") end - errors.add(name, "#{att.filename} is too large") if att.blob.byte_size > MAX_SIZE + limit = UploadValidation.max_size + errors.add(name, "#{att.filename} is over the #{limit / 1.megabyte} MB upload limit") if att.blob.byte_size > limit end end end diff --git a/app/models/setting.rb b/app/models/setting.rb new file mode 100644 index 0000000..532439a --- /dev/null +++ b/app/models/setting.rb @@ -0,0 +1,43 @@ +# App-wide settings an admin can change (Admin → Settings), stored as +# key/value rows. Read straight from the database each time, so a change +# applies to every server process at once. +# +# Setting.max_upload_mb # => 50 +# Setting.max_upload_bytes # => 52428800 +class Setting < ApplicationRecord + INTEGERS = { + "max_upload_mb" => { default: 50, in: 1..1024, label: "Maximum upload size (MB)" } + }.freeze + + validates :key, presence: true, uniqueness: true, inclusion: { in: INTEGERS.keys } + validate :value_in_range + + class << self + def max_upload_mb = integer("max_upload_mb") + def max_upload_bytes = max_upload_mb.megabytes + + def integer(key) + definition = INTEGERS.fetch(key) + value = find_by(key: key)&.value + value.present? ? value.to_i.clamp(definition[:in]) : definition[:default] + end + + # Saves several settings; returns the records (check #errors) or []. + def update_integers(values) + records = values.to_h.slice(*INTEGERS.keys).map do |key, value| + find_or_initialize_by(key: key).tap { _1.value = value.to_s.strip } + end + transaction { records.each { _1.save || raise(ActiveRecord::Rollback) } } + records + end + end + + private + + def value_in_range + definition = INTEGERS[key] or return + unless value.to_s.match?(/\A\d+\z/) && definition[:in].cover?(value.to_i) + errors.add(:base, "#{definition[:label]} must be a whole number from #{definition[:in].min} to #{definition[:in].max}") + end + end +end diff --git a/app/models/system_info.rb b/app/models/system_info.rb index 9d2df1a..70e7428 100644 --- a/app/models/system_info.rb +++ b/app/models/system_info.rb @@ -48,6 +48,7 @@ class SystemInfo "Single sign-on" => Sso.enabled? ? "#{Sso.label}#{', password sign-in off' unless Sso.password_login?}" : "not configured", "AI logbook transcription" => ENV["ANTHROPIC_API_KEY"].present? ? "on (#{LogbookTranscriber::MODEL})" : "no API key", "Email" => MailSettings.configured? ? ActionMailer::Base.delivery_method.to_s.upcase : "not configured", + "Upload limit" => "#{Setting.max_upload_mb} MB per file", "HTTPS enforced" => Rails.application.config.force_ssl ? "yes" : "no", "File storage" => ActiveStorage::Blob.service.class.name.demodulize.delete_suffix("Service") } diff --git a/app/services/aircraft_archive.rb b/app/services/aircraft_archive.rb index 06c1780..e748663 100644 --- a/app/services/aircraft_archive.rb +++ b/app/services/aircraft_archive.rb @@ -21,7 +21,7 @@ module AircraftArchive OPTIONAL_KEYS = %w[features].freeze # and flight_logs before schema v2 MANIFEST_MAX_BYTES = 50.megabytes - FILE_MAX_BYTES = 512.megabytes + FILE_MAX_BYTES = 1.gigabyte # the most Admin → Settings allows for uploads ARCHIVE_MAX_BYTES = 10.gigabytes # uncompressed MAX_RATIO = 100 # compression ratio above this looks like a zip bomb diff --git a/app/views/admin/settings/edit.html.erb b/app/views/admin/settings/edit.html.erb new file mode 100644 index 0000000..2c5a554 --- /dev/null +++ b/app/views/admin/settings/edit.html.erb @@ -0,0 +1,26 @@ +<% content_for :title, "Settings" %> +

Settings

+<%= form_with url: admin_settings_path, method: :patch, class: "card card-body max-w-xl space-y-4" do |f| %> + <% if @errors.present? %> + + <% end %> + +

Uploads

+ <% upload = Setting::INTEGERS["max_upload_mb"] %> +
+ <%= label_tag "settings[max_upload_mb]", upload[:label], class: "label" %> + <%= number_field_tag "settings[max_upload_mb]", @values["max_upload_mb"], id: "settings_max_upload_mb", + min: upload[:in].min, max: upload[:in].max, step: 1, required: true, class: "input w-32" %> +

+ The largest single file anyone can upload: documents, squawk photos and bulk uploads. Default <%= upload[:default] %> MB, up to <%= upload[:in].max %> MB. +

+

+ A proxy in front of the app can have its own, smaller limit. Cloudflare's proxy stops at 100 MB on its free plan; nginx needs client_max_body_size. + Logbook transcription with Claude reads PDFs up to <%= LogbookTranscriber::MAX_PDF_BYTES / 1.megabyte %> MB and images up to <%= LogbookTranscriber::MAX_IMAGE_BYTES / 1.megabyte %> MB, whatever this is set to. +

+
+ +
<%= f.submit "Save Settings", class: "btn-primary" %><%= link_to "Cancel", admin_about_path, class: "btn-secondary" %>
+<% end %> diff --git a/app/views/documents/_form.html.erb b/app/views/documents/_form.html.erb index 71cb709..7ad9fd0 100644 --- a/app/views/documents/_form.html.erb +++ b/app/views/documents/_form.html.erb @@ -10,7 +10,7 @@ <%= f.field :description, as: :textarea, rows: 2 %> <%= f.field :pages, as: :file, multiple: true, label: document.persisted? ? "Add files" : "Files", - hint: "Images, PDFs or text. Each file is a page in the viewer, in upload order." %> + hint: "Images, PDFs or text, up to #{Setting.max_upload_mb} MB each. Each file is a page in the viewer, in upload order." %> <% if document.persisted? && document.pages.any? %>

Remove existing files

diff --git a/app/views/documents/bulk_new.html.erb b/app/views/documents/bulk_new.html.erb index c59161a..b435b57 100644 --- a/app/views/documents/bulk_new.html.erb +++ b/app/views/documents/bulk_new.html.erb @@ -3,7 +3,7 @@ <% file_classes = "block w-full text-sm text-slate-600 file:mr-3 file:rounded-md file:border-0 file:bg-slate-100 file:px-3 file:py-1.5 file:text-sm file:font-medium hover:file:bg-slate-200" %> <%= form_with url: bulk_aircraft_documents_path(@aircraft), multipart: true, class: "card card-body max-w-3xl space-y-4", data: { controller: "bulk-upload", bulk_upload_url_value: rails_direct_uploads_path, - bulk_upload_max_bytes_value: UploadValidation::MAX_SIZE, action: "submit->bulk-upload#submit" } do %> + bulk_upload_max_bytes_value: UploadValidation.max_size, action: "submit->bulk-upload#submit" } do %>

Bulk upload

Each file becomes its own document, named after the file. Give files the same name to combine them, in order, as the pages of one document.

@@ -44,7 +44,7 @@ data-bulk-upload-target="folder" data-action="change->bulk-upload#add">
-

Images, PDFs or text, up to <%= UploadValidation::MAX_SIZE / 1.megabyte %> MB each and <%= DocumentsController::MAX_BULK_FILES %> at a time.

+

Images, PDFs or text, up to <%= Setting.max_upload_mb %> MB each and <%= DocumentsController::MAX_BULK_FILES %> at a time.