Ejemplo n.º 1
0
        public iCalendar GetFromUrl(string url, string calendarId)
        {
            var       cache    = new iCalendarCache(AuthContext, TimeZoneConverter, TenantManager);
            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);
                }

                var req = (HttpWebRequest)WebRequest.Create(url);
                using (var resp = req.GetResponse())
                    using (var stream = resp.GetResponseStream())
                    {
                        var ms = new MemoryStream();
                        stream.CopyTo(ms);
                        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);
        }
Ejemplo n.º 2
0
        public void doComponentBegin(Token t)
        {
            _component.Push(t);

            switch (t.TokenVal)
            {
            case TokenValue.Tvcalendar:
                _curCalendar = new iCalendar(AuthContext, TimeZoneConverter, TenantManager);
                break;

            case TokenValue.Tvevent:
            case TokenValue.Tvjournal:
                _curEvent = new iCalEvent();
                _curCalendar.Events.Add(_curEvent);
                _curEvent.CalendarId = _curCalendar.Id;
                break;
            }
        }
Ejemplo n.º 3
0
        public iCalendar GetCalendarFromCache(string calendarId)
        {
            var filePath = _cacheParams.FolderCachePath + calendarId + ".ics";

            if (File.Exists(filePath))
            {
                var fi = new FileInfo(filePath);
                if ((DateTime.UtcNow - fi.LastWriteTimeUtc).TotalMinutes > _cacheParams.ExpiredPeriod)
                {
                    return(null);
                }

                using (var tr = new StreamReader(File.OpenRead(filePath)))
                {
                    var icalendar = new iCalendar(AuthContext, TimeZoneConverter, TenantManager);
                    return(icalendar.GetFromStream(tr));
                }
            }

            return(null);
        }