parse.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::Calendar::IcsFile::Parse < Service::BaseWithCurrentUser
  3. def execute(file:)
  4. parse_calendar(file)
  5. end
  6. private
  7. def parse_calendar(file)
  8. file_content = file.is_a?(::ApplicationController::HasDownload::DownloadFile) ? file.content('preview') : file.content
  9. calendars = Icalendar::Calendar.parse(file_content)
  10. events = calendars.first&.events || []
  11. events = events.map { |event| build_event(event) }
  12. {
  13. filename: file.filename,
  14. events: events,
  15. type: file.preferences['Content-Type'] || file.preferences['Mime-Type'] || 'application/octet-stream',
  16. }
  17. end
  18. def build_event(event)
  19. description = event.description.to_utf8(fallback: :read_as_sanitized_binary)
  20. summary = event.summary.to_utf8(fallback: :read_as_sanitized_binary)
  21. {
  22. title: summary || description,
  23. location: event.location.to_utf8(fallback: :read_as_sanitized_binary),
  24. start_date: build_date(event.dtstart),
  25. end_date: build_date(event.dtend),
  26. attendees: event.attendee.map { |attendee| attendee.try(:to) },
  27. organizer: event.organizer&.try(:to),
  28. description: description&.strip,
  29. }
  30. end
  31. def build_date(date)
  32. return date if date.is_a?(Icalendar::Values::DateTime)
  33. date.to_time
  34. end
  35. end