When a profile has no usable picture, the account avatar is now cropped and uploaded to the configured Blossom server on save, and the returned URL is written to the profile, instead of publishing the account's own avatar URL. A failed upload fails the save.
117 lines
4.3 KiB
Ruby
117 lines
4.3 KiB
Ruby
require "marcel"
|
|
|
|
module Settings
|
|
class NostrProfileImagesController < ApplicationController
|
|
before_action :authenticate_user!
|
|
|
|
FIELDS = BlossomManagerService::MAX_UPLOAD_BYTES.keys.freeze
|
|
ALLOWED_CONTENT_TYPES = %w[image/jpeg image/png image/webp image/gif].freeze
|
|
EXTENSIONS = {
|
|
"image/jpeg" => "jpg", "image/png" => "png", "image/webp" => "webp", "image/gif" => "gif"
|
|
}.freeze
|
|
REQUEST_SLACK = 64.kilobytes
|
|
MAX_CAP = BlossomManagerService::MAX_UPLOAD_BYTES.values.max
|
|
RATE_LIMIT = 10
|
|
RATE_WINDOW = 5.minutes
|
|
|
|
def create
|
|
return render_error("Media server is not configured", :unprocessable_entity) if BlossomManagerService.server_url.blank?
|
|
return render_error("Too many upload attempts. Please try again later.", :too_many_requests) if rate_limited?
|
|
|
|
field = upload_field
|
|
return render_error("Invalid upload", :unprocessable_entity) if field.nil?
|
|
|
|
return create_from_avatar(field) if params[:source] == "avatar"
|
|
|
|
return render_error("File is too large", :payload_too_large) if request.content_length.to_i > MAX_CAP + REQUEST_SLACK
|
|
|
|
file = params.require(:file)
|
|
return render_error("Invalid upload", :unprocessable_entity) unless file.is_a?(ActionDispatch::Http::UploadedFile)
|
|
return render_error("File is too large", :payload_too_large) if file.size > BlossomManagerService.max_upload_bytes(field)
|
|
|
|
content_type = detect_content_type(file)
|
|
return render_error("Unsupported file type", :unprocessable_entity) unless ALLOWED_CONTENT_TYPES.include?(content_type)
|
|
|
|
bytes = NostrManager::ProcessProfileImage.call(field: field, io: file, content_type: content_type)
|
|
return render_error("Could not process image", :unprocessable_entity) if bytes.blank?
|
|
|
|
store_and_respond(field, bytes)
|
|
rescue ActionController::ParameterMissing, ArgumentError, TypeError => e
|
|
Rails.logger.warn("[nostr] Malformed profile image upload: #{e.class}: #{e.message}")
|
|
render_error("Invalid upload", :unprocessable_entity)
|
|
end
|
|
|
|
def show
|
|
field = upload_field
|
|
attachment = field && current_user.public_send(attachment_name(field))
|
|
http_status :not_found and return unless attachment&.attached?
|
|
|
|
send_data attachment.blob.download, type: attachment.blob.content_type, disposition: "inline"
|
|
end
|
|
|
|
private
|
|
|
|
def upload_field
|
|
field = params[:field].to_s
|
|
FIELDS.include?(field) ? field : nil
|
|
end
|
|
|
|
def attachment_name(field)
|
|
field == "banner" ? :nostr_banner : :nostr_picture
|
|
end
|
|
|
|
def detect_content_type(file)
|
|
file.rewind
|
|
Marcel::MimeType.for(file.tempfile, name: file.original_filename.to_s, declared_type: file.content_type)
|
|
ensure
|
|
file.rewind
|
|
end
|
|
|
|
def store(field, bytes)
|
|
content_type = Marcel::MimeType.for(StringIO.new(bytes))
|
|
current_user.public_send(attachment_name(field)).attach(
|
|
io: StringIO.new(bytes),
|
|
filename: "#{field}.#{EXTENSIONS.fetch(content_type, "jpg")}",
|
|
content_type: content_type
|
|
)
|
|
current_user.save
|
|
end
|
|
|
|
def create_from_avatar(field)
|
|
return render_error("Invalid upload", :unprocessable_entity) unless field == "picture"
|
|
return render_error("No account avatar", :unprocessable_entity) unless current_user.avatar.attached?
|
|
|
|
blob = current_user.avatar.blob
|
|
content_type = blob.content_type
|
|
return render_error("Unsupported file type", :unprocessable_entity) unless ALLOWED_CONTENT_TYPES.include?(content_type)
|
|
|
|
bytes = NostrManager::ProcessProfileImage.call(
|
|
field: field, io: StringIO.new(blob.download), content_type: content_type
|
|
)
|
|
return render_error("Could not process image", :unprocessable_entity) if bytes.blank?
|
|
|
|
store_and_respond(field, bytes)
|
|
end
|
|
|
|
def store_and_respond(field, bytes)
|
|
store(field, bytes)
|
|
|
|
sha256 = Digest::SHA256.hexdigest(bytes)
|
|
render json: { sha256: sha256, previewUrl: nostr_profile_image_settings_path(field, v: sha256) }
|
|
end
|
|
|
|
def rate_limited?
|
|
key = "nostr_profile_image:#{current_user.id}"
|
|
count = Rails.cache.read(key).to_i
|
|
return true if count >= RATE_LIMIT
|
|
|
|
Rails.cache.write(key, count + 1, expires_in: RATE_WINDOW)
|
|
false
|
|
end
|
|
|
|
def render_error(message, status)
|
|
render json: { error: message }, status: status
|
|
end
|
|
end
|
|
end
|