Ejemplo n.º 1
0
        /// <summary>
        /// Returns list of the files...
        /// </summary>
        void GetList()
        {
            long   siteId      = _context.Request["siteId"] == null ? 0 : Convert.ToInt64(_context.Request["siteId"]);
            long   userid      = _context.Request["userid"] == null ? 0 : Convert.ToInt64(_context.Request["userid"]);
            long   fileGroupid = _context.Request["fileGroupid"] == null ? 0 : Convert.ToInt64(_context.Request["fileGroupid"]);
            string contentType = _context.Request["contenttype"] == null ? "" : _context.Request["contenttype"];
            string returnType  = _context.Request["type"] == null ? "rss" : _context.Request["type"];


            //Chheck if the site exists and enabled
            AWAPI_Data.Data.awSite site = new AWAPI_BusinessLibrary.library.SiteLibrary().Get(siteId);
            if (site == null || !site.isEnabled)
            {
                return;
            }

            //SET THE FEED HEADER ----------------------------------
            string          link = String.IsNullOrEmpty(site.link) ? "http://awapi.com" : site.link;
            SyndicationFeed feed = new SyndicationFeed(site.title + " - Blog Feed", "", new Uri(link));

            feed.Authors.Add(new SyndicationPerson("*****@*****.**"));
            feed.AttributeExtensions.Add(new XmlQualifiedName("site"), site.title);
            feed.AttributeExtensions.Add(new XmlQualifiedName("sitelink"), site.link);
            feed.AttributeExtensions.Add(new XmlQualifiedName("servertime"), DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"));

            if (userid > 0)
            {
                feed.AttributeExtensions.Add(new XmlQualifiedName("userid"), userid.ToString());
            }
            if (fileGroupid > 0)
            {
                feed.AttributeExtensions.Add(new XmlQualifiedName("filegroupid"), fileGroupid.ToString());
            }
            if (contentType != "")
            {
                feed.AttributeExtensions.Add(new XmlQualifiedName("contenttype"), contentType);
            }

            //GET FILE LIST
            IList <AWAPI_Data.Data.awFile> fileList = _fileLib.GetList(siteId, "");

            if (fileList != null && fileList.Count > 0)
            {
                //get the list if the groupid and userid are set
                var list2 = from l in fileList
                            where l.isEnabled &&
                            (userid == 0 || userid > 0 && l.userId.Equals(userid)) &&
                            (fileGroupid == 0 ||
                             fileGroupid > 0 && l.awFileInGroups.FirstOrDefault(a => a.fileGroupId.Equals(fileGroupid)) != null)
                            select l;

                if (list2 != null && list2.Count() > 0)
                {
                    //fill the syndication feed
                    List <SyndicationItem> items = new List <SyndicationItem>();
                    fileList = list2.ToArray();
                    foreach (AWAPI_Data.Data.awFile file in fileList)
                    {
                        Uri uri = null;

                        SyndicationItem item = new SyndicationItem(
                            file.title,
                            file.description,
                            uri,
                            file.fileId.ToString(),
                            file.createDate);

                        item.ElementExtensions.Add("contenttype", null, file.contentType);
                        if (!String.IsNullOrEmpty(file.thumbnail))
                        {
                            item.ElementExtensions.Add("videopreview", null, file.thumbnail);
                        }

                        item.ElementExtensions.Add("userid", null, file.userId);
                        item.ElementExtensions.Add("username", null, file.awUser_File.username);
                        item.ElementExtensions.Add("userfirstname", null, file.awUser_File.firstName);
                        item.ElementExtensions.Add("userlastname", null, file.awUser_File.lastName);


                        items.Add(item);
                    }
                    feed.Items = items;
                }
            }


            string    output = "";
            XmlWriter writer = XmlWriter.Create(_context.Response.Output);

            if (feed != null)
            {
                switch (returnType)
                {
                case "atom":
                    _context.Response.ContentType = "application/atom+xml";
                    feed.Description = new TextSyndicationContent("AWAPI Content in Atom 1.0 Feed Format");
                    Atom10FeedFormatter atom = new Atom10FeedFormatter(feed);
                    atom.WriteTo(writer);
                    break;

                case "json":
                    _context.Response.ContentType = "application/json";
                    Rss20FeedFormatter rssFeed = new Rss20FeedFormatter(feed);
                    if (rssFeed != null)
                    {
                        output = Newtonsoft.Json.JsonConvert.SerializeObject(rssFeed);
                        //JavaScriptSerializer ser = new JavaScriptSerializer();
                        //output = ser.Serialize(rssFeed);
                    }
                    break;

                default:        //rss
                    feed.Description = new TextSyndicationContent("AWAPI Content in RSS 2.0 Feed Format");
                    _context.Response.ContentType = "application/rss+xml";
                    Rss20FeedFormatter rss = new Rss20FeedFormatter(feed);
                    rss.WriteTo(writer);
                    break;
                }
            }

            if (output != "")
            {
                _context.Response.Write(output);
            }

            writer.Close();
        }