Skip to content

Commit

Permalink
Update file type handling and validation for OSBL and Label logos
Browse files Browse the repository at this point in the history
- Updated content type validation for SVG files from 'image/svg' to 'image/svg+xml'
- Modified logo validation in Osbl and Label models to use consistent SVG MIME type
- Adjusted frontend file input and validation to match new SVG content type
- Simplified contributions controller error handling
- Updated related specs to use new SVG MIME type
  • Loading branch information
mpressen committed Feb 6, 2025
1 parent f9cb4ba commit d6f75f0
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 30 deletions.
7 changes: 2 additions & 5 deletions app/controllers/users/contributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ def create
osbl_data = OsblDataTransformer.new(osbl_params).transform
contribution.contributable = OsblCreation.new(osbl_data: osbl_data)

if contribution.save
redirect_to my_contributions_path, success: "Votre contribution a été enregistrée."
else
redirect_to my_new_contribution_path, inertia: {errors: @contribution.errors}
end
contribution.save!
redirect_to my_contributions_path, success: "Votre contribution a été enregistrée."
else
redirect_to my_new_contribution_path, inertia: {errors: osbl.errors}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function OsblHeader ({ data, setData, errors, clearErrors }: Omit

<MyFileInput
id='logo'
accept='image/png, image/svg, image/webp'
accept='image/png, image/svg+xml, image/webp'
labelText={
<p className='flex items-center gap-2'>
Logo
Expand Down
13 changes: 2 additions & 11 deletions app/frontend/pages/Contribution/New.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import { OsblCreationData, FormProps } from '@/pages/Contribution/types'
import z from 'zod'
import deepCleanData from '@/lib/deepCleanData'
import { toast } from 'sonner'
import { FormDataConvertible } from '@inertiajs/core'

const MAX_LOGO_SIZE = 1 * 1024 * 1024 // 1MB
const ALLOWED_LOGO_TYPES = ['image/svg', 'image/png', 'image/webp']
const ALLOWED_LOGO_TYPES = ['image/svg+xml', 'image/png', 'image/webp']

const validation = z.object({
contribution: z.object({
Expand All @@ -38,14 +37,6 @@ const validation = z.object({
})
})

type StrictForm<T> = {
[K in keyof T]: T[K] extends Record<string, any>
? StrictForm<T[K]>
: T[K] extends FormDataConvertible
? T[K]
: never
}

function createOsblProxy (
data: OsblCreationData,
setData: (key: string, value: any) => void,
Expand Down Expand Up @@ -80,7 +71,7 @@ function createOsblProxy (

export default function New ({ currentUser }: { currentUser: CurrentUserType }): ReactElement {
// inertia types don't handle nested data properly.
const { data, setData, post, processing, errors, clearErrors, setError, transform } = useForm<StrictForm<OsblCreationData>>({
const { data, setData, post, processing, errors, clearErrors, setError, transform } = useForm({
contribution: {
osbl: {
name: '',
Expand Down
8 changes: 8 additions & 0 deletions app/models/label.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
class Label < ApplicationRecord
include AttachableValidation

has_many :osbls_labels, dependent: :destroy
has_many :osbls, through: :osbls_labels

has_one_attached :logo

validates_attachment(
name: :logo,
max_size: 1.megabytes,
content_types: %w[image/svg+xml image/png image/webp]
)
end
2 changes: 1 addition & 1 deletion app/models/osbl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Osbl < ApplicationRecord
validates_attachment(
name: :logo,
max_size: 1.megabytes,
content_types: %w[image/png image/svg image/webp]
content_types: %w[image/png image/svg+xml image/webp]
)
validates :osbls_causes, presence: {message: "Au moins une cause est requise."}

Expand Down
2 changes: 1 addition & 1 deletion app/models/osbl_creation.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class OsblCreation < ApplicationRecord
has_one :contribution, as: :contributable
has_one :contribution, as: :contributable, touch: true

STATUS = {
"brouillon" => 0,
Expand Down
9 changes: 0 additions & 9 deletions app/services/OsblDataTransformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,11 @@ def transform_documents!
end

def process_file(uploaded_file)
# uploaded_file signature is :
# #<ActionDispatch::Http::UploadedFile:0x000074ebd23c15f8
# @content_type="image/png",
# @headers="content-disposition: form-data; name=\"logo\"; filename=\"valid_file.png\"\r\ncontent-type: image/png\r\ncontent-length: 1498\r\n",
# @original_filename="valid_file.png",
# @tempfile=#<File:/tmp/RackMultipart20250205-82360-bi2o4k.png>>

blob = ActiveStorage::Blob.create_and_upload!(
io: uploaded_file.tempfile,
filename: uploaded_file.original_filename,
content_type: uploaded_file.content_type
)
blob.signed_id

# Rails.application.routes.url_helpers.rails_blob_url(blob, only_path: true)
end
end
6 changes: 6 additions & 0 deletions spec/factories/osbls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@
after(:build) do |osbl|
osbl.causes << build(:cause) if osbl.causes.empty?
end

trait :with_document do
after(:build) do |osbl|
osbl.documents << build(:document)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/models/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
end

it "is invalid with non-PDF file" do
document = build(:document, file: fixture_file_upload("spec/fixtures/files/invalid_file_type.svg", "image/svg"))
document = build(:document, file: fixture_file_upload("spec/fixtures/files/invalid_file_type.svg", "image/svg+xml"))
expect(document).not_to be_valid
expect(document.errors[:file]).to be_present
end
Expand Down
15 changes: 15 additions & 0 deletions spec/models/osbl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@
expect(osbl).not_to be_valid
expect(osbl.errors[:osbls_causes]).to be_present
end

describe "associated documents" do
it "osbl is invalid with an invalid document" do
osbl = build(:osbl, document_attachments_attributes: [{
document_attributes: {
type: "Autre",
name: "Test document"
# file is missing, making document invalid
}
}])

expect(osbl).not_to be_valid
expect(osbl.errors[:"document_attachments.document.file"]).to be_present
end
end
end

# Association tests
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/contributions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
expect(contribution.body).to eq("Je suis le créateur de Benefactorum, vous trouverez toutes les informations sur le projet sur le site de Benefactorum et dans les documents associés.")
expect(contribution.files).to be_attached

osbl = Osbl.create!(contribution.contributable.osbl_data)
osbl = Osbl.create!(contribution.osbl_creation.osbl_data)
expect(osbl.name).to eq("Benefactorum")
expect(osbl.website).to eq("https://benefactorum.org/")
expect(osbl.logo).to be_attached
Expand Down

0 comments on commit d6f75f0

Please sign in to comment.