add.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Mutations
  3. class Form::UploadCache::Add < BaseMutation
  4. include Gql::Mutations::Form::UploadCache::Concerns::HandlesAuthorization
  5. description 'Upload files for a form'
  6. argument :form_id, Gql::Types::FormIdType, 'FormID for the uploads.'
  7. argument :files, [Gql::Types::Input::UploadFileInputType], 'Files to be uploaded.'
  8. field :uploaded_files, [Gql::Types::StoredFileType], null: false, description: 'Information about the uploaded files.'
  9. def resolve(form_id:, files:)
  10. cache = UploadCache.new(form_id)
  11. result = files.map { |elem| add_single_file(cache, elem) }
  12. { uploaded_files: result }
  13. end
  14. private
  15. def add_single_file(cache, file)
  16. preferences = { 'Content-Type' => file.type }
  17. if file.inline
  18. preferences['Content-Disposition'] = 'inline'
  19. end
  20. cache.add(
  21. data: file.content,
  22. filename: file.name,
  23. preferences: preferences,
  24. created_by_id: context.current_user.id
  25. )
  26. end
  27. end
  28. end