async Task <Asset> SaveFile(IFormFile file)
        {
            var profile = GetProfile();
            var storage = new BlogStorage(profile.Slug);
            var path    = string.Format("{0}/{1}", DateTime.Now.Year, DateTime.Now.Month);

            var asset = await storage.UploadFormFile(file, Url.Content("~/"), path);

            asset.ProfileId   = profile.Id;
            asset.LastUpdated = SystemClock.Now();

            if (IsImageFile(asset.Url))
            {
                asset.AssetType = AssetType.Image;
            }
            else
            {
                asset.AssetType = AssetType.Attachment;
            }

            _db.Assets.Add(asset);
            _db.Complete();

            return(asset);
        }
Beispiel #2
0
        private async Task <Asset> SaveFile(IFormFile file)
        {
            var profile = GetProfile();
            var storage = new BlogStorage(profile.Slug);
            var path    = string.Format("{0}/{1}", DateTime.Now.Year, DateTime.Now.Month);

            var asset = await storage.UploadFormFile(file, Url.Content("~/"), path);

            // sometimes we just want to override uploaded file
            // only add DB record if asset does not exist yet
            var existingAsset = this.db.Assets.Find(a => a.Path == asset.Path).FirstOrDefault();

            if (existingAsset == null)
            {
                asset.ProfileId   = profile.Id;
                asset.LastUpdated = SystemClock.Now();

                if (IsImageFile(asset.Url))
                {
                    asset.AssetType = AssetType.Image;
                }
                else
                {
                    asset.AssetType = AssetType.Attachment;
                }
                this.db.Assets.Add(asset);
            }
            else
            {
                existingAsset.LastUpdated = SystemClock.Now();
            }
            this.db.Complete();
            return(asset);
        }
        public IActionResult Personal()
        {
            if (ApplicationSettings.SingleBlog)
            {
                return(NotFound());
            }

            var profile = GetProfile();
            var storage = new BlogStorage("");

            var model = new SettingsPersonal
            {
                Profile    = profile,
                BlogThemes = storage.GetThemes(ThemeType.Blog)
            };

            if (profile != null)
            {
                model.Title       = profile.Title;
                model.Description = profile.Description;
                model.BlogTheme   = profile.BlogTheme;
                model.Image       = profile.Image;
                model.Logo        = profile.Logo;
            }
            return(View(_theme + "Personal.cshtml", model));
        }
        public IActionResult General(SettingsGeneral model)
        {
            var storage = new BlogStorage("");

            model.BlogThemes = BlogSettings.BlogThemes;
            model.Profile    = GetProfile();

            if (ModelState.IsValid)
            {
                BlogSettings.Title                = model.Title;
                BlogSettings.Description          = model.Description;
                BlogSettings.Logo                 = model.Logo;
                ApplicationSettings.ProfileAvatar = model.Avatar;
                BlogSettings.Cover                = model.Image;
                BlogSettings.Theme                = model.BlogTheme;

                _db.CustomFields.SetCustomField(CustomType.Application, 0, Constants.Title, model.Title);
                _db.CustomFields.SetCustomField(CustomType.Application, 0, Constants.Description, model.Description);
                _db.CustomFields.SetCustomField(CustomType.Application, 0, Constants.ProfileLogo, model.Logo);
                _db.CustomFields.SetCustomField(CustomType.Application, 0, Constants.ProfileAvatar, model.Avatar);
                _db.CustomFields.SetCustomField(CustomType.Application, 0, Constants.ProfileImage, model.Image);
                _db.CustomFields.SetCustomField(CustomType.Application, 0, Constants.BlogTheme, model.BlogTheme);
                _db.CustomFields.SetCustomField(CustomType.Application, 0, Constants.SendGridApiKey, model.EmailKey);
                _db.CustomFields.SetCustomField(CustomType.Application, 0, Constants.HeadCode, model.BlogHead);
                _db.CustomFields.SetCustomField(CustomType.Application, 0, Constants.FooterCode, model.BlogFooter);

                model.Profile.BlogTheme = model.BlogTheme;

                _db.Complete();

                ViewBag.Message = "Updated";
            }
            return(View(_theme + "General.cshtml", model));
        }
        public IActionResult Delete(int id)
        {
            var asset = _db.Assets.Single(a => a.Id == id);

            if (asset == null)
            {
                return(NotFound());
            }

            var blog = GetProfile();

            try
            {
                var storage = new BlogStorage(blog.Slug);
                storage.DeleteFile(asset.Path);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
            }

            _db.Assets.Remove(asset);
            _db.Complete();

            // reset profile image to default
            // if asset was removed
            var profiles = _db.Profiles.Find(p => p.Image == asset.Url || p.Avatar == asset.Url || p.Logo == asset.Url).ToList();

            if (profiles != null)
            {
                foreach (var item in profiles)
                {
                    if (item.Image == asset.Url)
                    {
                        item.Image = null;
                    }
                    if (item.Avatar == asset.Url)
                    {
                        item.Avatar = null;
                    }
                    if (item.Logo == asset.Url)
                    {
                        item.Logo = null;
                    }
                    _db.Complete();
                }
            }
            return(new NoContentResult());
        }
Beispiel #6
0
        public IActionResult Delete(int id)
        {
            var profile = GetProfile();

            if (!profile.IsAdmin || profile.Id == id)
            {
                return(NotFound());
            }

            this.logger.LogInformation(string.Format("Delete blog {0} by {1}", id, profile.AuthorName));

            var assets = this.db.Assets.Find(a => a.ProfileId == id);

            this.db.Assets.RemoveRange(assets);
            this.db.Complete();
            this.logger.LogInformation("Assets deleted");

            var categories = this.db.Categories.Find(c => c.ProfileId == id);

            this.db.Categories.RemoveRange(categories);
            this.db.Complete();
            this.logger.LogInformation("Categories deleted");

            var posts = this.db.BlogPosts.Find(p => p.ProfileId == id);

            this.db.BlogPosts.RemoveRange(posts);
            this.db.Complete();
            this.logger.LogInformation("Posts deleted");

            var fields = this.db.CustomFields.Find(f => f.CustomType == CustomType.Profile && f.ParentId == id);

            this.db.CustomFields.RemoveRange(fields);
            this.db.Complete();
            this.logger.LogInformation("Custom fields deleted");

            var profileToDelete = this.db.Profiles.Single(b => b.Id == id);

            var storage = new BlogStorage(profileToDelete.Slug);

            storage.DeleteFolder("");
            this.logger.LogInformation("Storage deleted");

            this.db.Profiles.Remove(profileToDelete);
            this.db.Complete();
            this.logger.LogInformation("Profile deleted");

            return(new NoContentResult());
        }
        public IActionResult Personal(SettingsPersonal model)
        {
            var storage = new BlogStorage("");

            model.BlogThemes = storage.GetThemes(ThemeType.Blog);
            model.Profile    = GetProfile();

            if (ModelState.IsValid)
            {
                model.Profile.Title       = model.Title;
                model.Profile.Description = model.Description;
                model.Profile.BlogTheme   = model.BlogTheme;
                model.Profile.Logo        = model.Logo;
                model.Profile.Image       = model.Image;

                _db.Complete();
                ViewBag.Message = "Updated";
            }
            return(View(_theme + "Personal.cshtml", model));
        }
        public void CanParseRssFeed(string feed)
        {
            using (var context = new BlogifierDbContext(_options))
            {
                var storage = new BlogStorage("test");
                var path = Path.Combine(GetRoot(), feed);

                var uow = new UnitOfWork(context);

                var profile = uow.Profiles.Single(p => p.IdentityName == "test");

                if(profile == null)
                {
                    profile = new Profile();
                    profile.IdentityName = "test";
                    profile.AuthorName = "test";
                    profile.AuthorEmail = "*****@*****.**";
                    profile.Title = "test";
                    profile.Slug = "test";
                    profile.Description = "the test";
                    profile.BlogTheme = "Standard";

                    uow.Profiles.Add(profile);
                    uow.Complete();
                }

                Assert.True(context.Profiles.ToList().Count > 0);

                var service = new RssService(uow, null);

                var model = new RssImportModel();
                model.FeedUrl = path;
                model.ProfileId = profile.Id;

                var result = service.Import(model);

                Assert.True(context.BlogPosts.ToList().Count > 1);
            }

            Assert.NotNull(feed);
        }
        public IActionResult General()
        {
            var profile = GetProfile();
            var storage = new BlogStorage("");

            var model = new SettingsGeneral
            {
                Profile     = profile,
                BlogThemes  = BlogSettings.BlogThemes,
                Title       = BlogSettings.Title,
                Description = BlogSettings.Description,
                BlogTheme   = BlogSettings.Theme,
                Logo        = BlogSettings.Logo,
                Avatar      = ApplicationSettings.ProfileAvatar,
                Image       = BlogSettings.Cover,
                EmailKey    = _db.CustomFields.GetValue(CustomType.Application, 0, Constants.SendGridApiKey),
                BlogHead    = _db.CustomFields.GetValue(CustomType.Application, 0, Constants.HeadCode),
                BlogFooter  = _db.CustomFields.GetValue(CustomType.Application, 0, Constants.FooterCode)
            };

            return(View(_theme + "General.cshtml", model));
        }
Beispiel #10
0
        public IActionResult AppSettings()
        {
            var profile = GetProfile();
            var storage = new BlogStorage("");

            var model = new SettingsApplication
            {
                Profile      = profile,
                BlogThemes   = storage.GetThemes(),
                Title        = ApplicationSettings.Title,
                Description  = ApplicationSettings.Description,
                BlogTheme    = ApplicationSettings.BlogTheme,
                ItemsPerPage = ApplicationSettings.ItemsPerPage,
                Logo         = ApplicationSettings.ProfileLogo,
                Avatar       = ApplicationSettings.ProfileAvatar,
                Image        = ApplicationSettings.ProfileImage,
                PostImage    = ApplicationSettings.PostImage,
                CustomFields = _db.CustomFields.GetCustomFields(CustomType.Application, 0).Result
            };

            return(View(_theme + "AppSettings.cshtml", model));
        }
Beispiel #11
0
        public IActionResult AppSettings(SettingsApplication model)
        {
            var storage = new BlogStorage("");

            model.BlogThemes = storage.GetThemes();
            model.Profile    = GetProfile();

            if (ModelState.IsValid)
            {
                _db.CustomFields.SetCustomField(CustomType.Application, 0, "Title", model.Title);
                ApplicationSettings.Title = model.Title;
                _db.CustomFields.SetCustomField(CustomType.Application, 0, "Description", model.Description);
                ApplicationSettings.Description = model.Description;
                _db.CustomFields.SetCustomField(CustomType.Application, 0, "ItemsPerPage", model.ItemsPerPage.ToString());
                ApplicationSettings.ItemsPerPage = model.ItemsPerPage;

                _db.CustomFields.SetCustomField(CustomType.Application, 0, "ProfileLogo", model.Logo);
                ApplicationSettings.ProfileLogo = model.Logo;
                _db.CustomFields.SetCustomField(CustomType.Application, 0, "ProfileAvatar", model.Avatar);
                ApplicationSettings.ProfileAvatar = model.Avatar;
                _db.CustomFields.SetCustomField(CustomType.Application, 0, "ProfileImage", model.Image);
                ApplicationSettings.ProfileImage = model.Image;
                _db.CustomFields.SetCustomField(CustomType.Application, 0, "PostImage", model.PostImage);
                ApplicationSettings.PostImage = model.PostImage;

                _db.CustomFields.SetCustomField(CustomType.Application, 0, "BlogTheme", model.BlogTheme);

                _db.CustomFields.SetCustomField(CustomType.Application, 0, Constants.SendGridApiKey, model.CustomFields[Constants.SendGridApiKey]);

                model.Profile.BlogTheme       = model.BlogTheme;
                ApplicationSettings.BlogTheme = model.BlogTheme;

                _db.Complete();

                ViewBag.Message = "Updated";
            }
            return(View(_theme + "AppSettings.cshtml", model));
        }
Beispiel #12
0
        public async Task <HttpResponseMessage> Import(RssImportModel model)
        {
            var response = new HttpResponseMessage(HttpStatusCode.OK);

            _model = model;

            if (model == null || string.IsNullOrEmpty(model.FeedUrl))
            {
                response.StatusCode   = HttpStatusCode.NotFound;
                response.ReasonPhrase = "RSS feed URL is required";
                return(response);
            }

            var blog = _db.Profiles.Single(b => b.Id == model.ProfileId);

            if (blog == null)
            {
                response.StatusCode   = HttpStatusCode.NotFound;
                response.ReasonPhrase = Constants.ProfileNotFound;
                return(response);
            }

            try
            {
                var storage = new BlogStorage(blog.Slug);
                var items   = GetFeedItems(model.FeedUrl);

                _logger.LogInformation(string.Format("Start importing {0} posts", items.Count));

                foreach (var item in items)
                {
                    var content = item.Body.Length > item.Description.Length ? item.Body : item.Description;

                    var desc = content.StripHtml();
                    if (desc.Length > 300)
                    {
                        desc = desc.Substring(0, 300);
                    }

                    var post = new BlogPost
                    {
                        ProfileId   = model.ProfileId,
                        Title       = item.Title,
                        Slug        = item.Title.ToSlug(),
                        Description = desc,
                        Content     = content,
                        Published   = item.PublishDate
                    };
                    if (model.ImportImages)
                    {
                        await ImportImages(post, storage);
                    }
                    if (model.ImportAttachements)
                    {
                        await ImportAttachements(post, storage);
                    }
                    _db.BlogPosts.Add(post);
                    _db.Complete();
                    _logger.LogInformation(string.Format("RSS item added : {0}", item.Title));

                    await AddCategories(item, model.ProfileId);
                }
                response.ReasonPhrase = string.Format("Imported {0} blog posts", items.Count);
                return(response);
            }
            catch (Exception ex)
            {
                _logger.LogError(string.Format("Error importing RSS : {0}", ex.Message));

                response.StatusCode   = HttpStatusCode.BadRequest;
                response.ReasonPhrase = ex.Message;
                return(response);
            }
        }
 public BlogStorageTests()
 {
     _storage = new BlogStorage("test");
 }
Beispiel #14
0
        public async Task <IActionResult> Delete(int id)
        {
            var admin = GetProfile();

            if (!admin.IsAdmin || admin.Id == id)
            {
                return(NotFound());
            }

            var profile = _db.Profiles.Single(p => p.Id == id);

            _logger.LogInformation(string.Format("Delete blog {0} by {1}", profile.Title, profile.AuthorName));

            var assets = _db.Assets.Find(a => a.ProfileId == id);

            _db.Assets.RemoveRange(assets);
            _db.Complete();
            _logger.LogInformation("Assets deleted");

            var categories = _db.Categories.Find(c => c.ProfileId == id);

            _db.Categories.RemoveRange(categories);
            _db.Complete();
            _logger.LogInformation("Categories deleted");

            var posts = _db.BlogPosts.Find(p => p.ProfileId == id);

            _db.BlogPosts.RemoveRange(posts);
            _db.Complete();
            _logger.LogInformation("Posts deleted");

            var fields = _db.CustomFields.Find(f => f.CustomType == CustomType.Profile && f.ParentId == id);

            _db.CustomFields.RemoveRange(fields);
            _db.Complete();
            _logger.LogInformation("Custom fields deleted");

            var profileToDelete = _db.Profiles.Single(b => b.Id == id);

            var storage = new BlogStorage(profileToDelete.Slug);

            storage.DeleteFolder("");
            _logger.LogInformation("Storage deleted");

            _db.Profiles.Remove(profileToDelete);
            _db.Complete();
            _logger.LogInformation("Profile deleted");

            // remove login

            var user = await _userManager.FindByNameAsync(profile.IdentityName);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }
            var result = await _userManager.DeleteAsync(user);

            if (!result.Succeeded)
            {
                throw new ApplicationException($"Unexpected error occurred removing login for user with ID '{user.Id}'.");
            }
            return(new NoContentResult());
        }