calendar_preview.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module CalendarPreview
  3. extend ActiveSupport::Concern
  4. private
  5. def parse_calendar(file)
  6. file_content = file.content('preview')
  7. calendars = Icalendar::Calendar.parse(file_content)
  8. events = calendars.first&.events || []
  9. events = events.map { |event| build_event(event) }
  10. {
  11. filename: file.filename,
  12. events: events,
  13. type: file.preferences['Content-Type'] || file.preferences['Mime-Type'] || 'application/octet-stream',
  14. }
  15. end
  16. def build_event(event)
  17. description = event.description.to_utf8(fallback: :read_as_sanitized_binary)
  18. summary = event.summary.to_utf8(fallback: :read_as_sanitized_binary)
  19. {
  20. title: summary || description,
  21. location: event.location.to_utf8(fallback: :read_as_sanitized_binary),
  22. start_date: event.dtstart,
  23. end_date: event.dtend,
  24. attendees: event.attendee.map { |attendee| attendee.try(:to) },
  25. organizer: event.organizer&.try(:to),
  26. description: description&.strip,
  27. }
  28. end
  29. end