DocSpace-client/products/ASC.Calendar/Server/iCalParser/iCalendar.cs

190 lines
6.4 KiB
C#
Raw Normal View History

2020-02-13 09:18:13 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
using System.Collections.Generic;
2021-11-24 19:34:39 +00:00
using System.IO;
2020-02-13 09:18:13 +00:00
using System.Linq;
2021-11-24 19:34:39 +00:00
using System.Net.Http;
2020-02-13 09:18:13 +00:00
using System.Text.RegularExpressions;
2021-11-24 19:34:39 +00:00
2020-02-13 09:18:13 +00:00
using ASC.Calendar.Resources;
using ASC.Common.Utils;
using ASC.Core;
2021-11-24 19:34:39 +00:00
using ASC.Web.Core.Calendars;
2020-02-13 09:18:13 +00:00
namespace ASC.Calendar.iCalParser
{
public class iCalendar : BaseCalendar
{
2021-11-24 19:34:39 +00:00
private TenantManager TenantManager { get; }
2022-01-13 11:19:39 +00:00
private IHttpClientFactory ClientFactory { get; }
2021-11-24 19:34:39 +00:00
2020-03-13 08:53:32 +00:00
public iCalendar GetFromStream(TextReader reader)
2020-02-13 09:18:13 +00:00
{
2022-01-13 11:19:39 +00:00
var emitter = new iCalendarEmitter(AuthContext, TimeZoneConverter, TenantManager, ClientFactory);
2020-02-13 09:18:13 +00:00
var parser = new Parser(reader, emitter);
parser.Parse();
return emitter.GetCalendar();
}
2020-03-13 08:53:32 +00:00
public iCalendar GetFromUrl(string url)
2020-02-13 09:18:13 +00:00
{
return GetFromUrl(url, null);
}
2020-03-13 08:53:32 +00:00
public iCalendar GetFromUrl(string url, string calendarId)
2020-02-13 09:18:13 +00:00
{
2022-01-13 11:19:39 +00:00
var cache = new iCalendarCache(AuthContext, TimeZoneConverter, TenantManager, ClientFactory);
2020-02-13 09:18:13 +00:00
iCalendar calendar = null;
if (calendarId != null)
calendar = cache.GetCalendarFromCache(calendarId);
if (calendar == null)
{
if (url.StartsWith("webcal"))
{
url = new Regex("webcal").Replace(url, "http", 1);
}
2021-10-12 10:14:33 +00:00
var request = new HttpRequestMessage();
request.RequestUri = new Uri(url);
2022-01-13 11:19:39 +00:00
var httpClient = ClientFactory.CreateClient();
2021-11-24 19:34:39 +00:00
using (var response = httpClient.Send(request))
2021-10-12 10:14:33 +00:00
using (var stream = response.Content.ReadAsStream())
2020-02-13 09:18:13 +00:00
{
var ms = new MemoryStream();
2021-04-13 08:40:31 +00:00
stream.CopyTo(ms);
2020-02-13 09:18:13 +00:00
ms.Seek(0, SeekOrigin.Begin);
using (var tempReader = new StreamReader(ms))
{
var reader = new StringReader(tempReader.ReadToEnd());
calendar = GetFromStream(reader);
if (calendar != null && calendarId != null)
{
tempReader.BaseStream.Seek(0, SeekOrigin.Begin);
cache.UpdateCalendarCache(calendarId, tempReader);
}
}
}
}
if (calendar == null)
throw new Exception(CalendarApiResource.WrongiCalFeedLink);
return calendar;
}
2021-11-24 19:34:39 +00:00
public List<iCalEvent> Events { get; set; }
public iCalendar(
2020-03-13 08:53:32 +00:00
AuthContext authContext,
TimeZoneConverter timeZoneConverter,
2022-01-13 11:19:39 +00:00
TenantManager tenantManager,
IHttpClientFactory clientFactory)
2020-03-13 08:53:32 +00:00
: base(authContext, timeZoneConverter)
2020-02-13 09:18:13 +00:00
{
2022-01-13 11:19:39 +00:00
TenantManager = tenantManager;
ClientFactory = clientFactory;
2020-02-13 09:18:13 +00:00
this.Context.CanChangeAlertType = false;
this.Context.CanChangeTimeZone = false;
2020-03-13 08:53:32 +00:00
this.Context.GetGroupMethod = delegate () { return Resources.CalendarApiResource.iCalCalendarsGroup; };
2020-02-13 09:18:13 +00:00
this.EventAlertType = EventAlertType.Never;
2020-03-13 08:53:32 +00:00
this.Events = new List<iCalEvent>();
2020-02-13 09:18:13 +00:00
}
public bool isEmptyName
{
2020-03-13 08:53:32 +00:00
get { return String.IsNullOrEmpty(_name); }
2020-02-13 09:18:13 +00:00
}
private string _name;
public override string Name
{
get
{
if (String.IsNullOrEmpty(_name))
return Resources.CalendarApiResource.NoNameCalendar;
return _name;
}
set
{
_name = value;
}
}
private TimeZoneInfo _timeZone;
public override TimeZoneInfo TimeZone
{
get
{
if (_timeZone != null)
return _timeZone;
if (!String.IsNullOrEmpty(xTimeZone))
{
_timeZone = TimeZoneConverter.GetTimeZone(xTimeZone);
return _timeZone;
}
if (String.IsNullOrEmpty(TZID))
{
2020-03-13 08:53:32 +00:00
_timeZone = TimeZoneConverter.GetTimeZone(TenantManager.GetCurrentTenant().TimeZone);
2020-02-13 09:18:13 +00:00
return _timeZone;
}
_timeZone = TimeZoneConverter.GetTimeZone(TZID);
return _timeZone;
}
set
{
_timeZone = value;
}
}
2020-03-13 08:53:32 +00:00
2020-02-13 09:18:13 +00:00
public string TZID { get; set; }
public string xTimeZone { get; set; }
2020-03-13 08:53:32 +00:00
2020-02-13 09:18:13 +00:00
public override List<IEvent> LoadEvents(Guid userId, DateTime utcStartDate, DateTime utcEndDate)
{
return Events.Cast<IEvent>().ToList();
2020-03-13 08:53:32 +00:00
}
2021-06-30 06:37:17 +00:00
public override List<ITodo> LoadTodos(Guid userId, DateTime utcStartDate, DateTime utcEndDate)
{
return new List<ITodo>();
}
2020-02-13 09:18:13 +00:00
}
}