Skip to content

Commit

Permalink
limit image upload to 4mb
Browse files Browse the repository at this point in the history
  • Loading branch information
digitaltom committed Jan 30, 2025
1 parent 41c216e commit 3ef764f
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ def destroy
private

def require_admin_user
not_found! unless @user&.admin?
redirect_to login_path unless @user&.admin?
end
end
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def set_user
end

def require_login
not_found! unless @user
redirect_to login_path unless @user
end

def disable_session_cookies
Expand Down
1 change: 1 addition & 0 deletions app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def upload
file = params[:image]
ext = file.content_type.split("/").last
filename = "#{SecureRandom.hex(4)}.#{ext}"
raise "Image size exceeds 4MB" if file.size / (1024 * 1024) > 4
uid = Dragonfly.app.store(file.tempfile, "name" => filename)
img = Image.create!(img_uid: uid)
render json: { icon: "/icon/#{img.public_id}", image: "/image/#{img.public_id}" }
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def create
# Make first user admin
user.update!(admin: true) if User.count == 1
session[:user_id] = user.id
redirect_to root_path
redirect_to my_path
end
end
6 changes: 6 additions & 0 deletions app/javascript/controllers/feature_edit_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ export default class extends Controller {
const feature = this.getFeature()
const image = document.querySelector('#marker-image').files[0]
const formData = new FormData() // send using multipart/form-data

if (image && image.size > (4 * 1024 * 1024)) {
alert('Image is too large. Maximum size is 4MB.')
}

formData.append('image', image)
fetch('/images', {
method: 'POST',
Expand All @@ -141,6 +146,7 @@ export default class extends Controller {
console.log('Setting icon: ' + data.icon)
feature.properties['marker-image-url'] = data.icon
draw.setFeatureProperty(this.featureIdValue, 'marker-image-url', data.icon)
draw.setFeatureProperty(this.featureIdValue, 'marker-size', 15)
setFeatureTitleImage(feature)
redrawGeojson(false)
this.saveFeature()
Expand Down

0 comments on commit 3ef764f

Please sign in to comment.