Пример #1
0
        public IActionResult ShortenUrl(string url)
        {
            if (url.IsValidUrl())
            {
                ShortenedUrl newUrl = ShortenerHelper.ShortenUrl(_dbContext, url, _config.ShortenerConfig.UrlLength);

                if (User.Identity.IsAuthenticated)
                {
                    Users.Models.User foundUser = UserHelper.GetUser(_dbContext, User.Identity.Name);
                    if (foundUser != null)
                    {
                        newUrl.UserId = foundUser.UserId;
                    }
                }

                _dbContext.ShortenedUrls.Add(newUrl);
                _dbContext.SaveChanges();

                string shortUrl = string.Format("{0}://{1}/{2}", Request.Scheme, _config.ShortenerConfig.ShortenerHost, newUrl.ShortUrl);
                if (_config.DevEnvironment)
                {
                    shortUrl = Url.SubRouteUrl("shortened", "Shortener.View", new { url = newUrl.ShortUrl });
                }

                return(Json(new { result = new { shortUrl = shortUrl, originalUrl = url } }));
            }
            return(Json(new { error = "Must be a valid Url" }));
        }
Пример #2
0
        public IActionResult Paste([Bind("Content, Title, Syntax, ExpireLength, ExpireUnit, Password")] PasteCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (_config.PasteConfig.Enabled)
                {
                    try
                    {
                        Models.Paste paste = PasteHelper.CreatePaste(_config, _dbContext, model.Content, model.Title, model.Syntax, model.ExpireUnit, model.ExpireLength ?? 1, model.Password);

                        if (model.ExpireUnit == ExpirationUnit.Views)
                        {
                            paste.Views = -1;
                        }

                        if (User.Identity.IsAuthenticated)
                        {
                            Users.Models.User user = UserHelper.GetUser(_dbContext, User.Identity.Name);
                            if (user != null)
                            {
                                paste.UserId = user.UserId;
                            }
                        }

                        _dbContext.Pastes.Add(paste);
                        _dbContext.SaveChanges();

                        // Cache the password
                        CachePassword(paste.Url, model.Password);

                        return(Redirect(Url.SubRouteUrl("p", "Paste.View", new { type = "Full", url = paste.Url })));
                    }
                    catch (Exception ex)
                    {
                        return(Redirect(Url.SubRouteUrl("error", "Error.500", new { exception = ex })));
                    }
                }
                return(new StatusCodeResult(StatusCodes.Status403Forbidden));
            }
            return(View("~/Areas/Paste/Views/Paste/Index.cshtml", model));
        }
Пример #3
0
        public async Task <IActionResult> Upload([FromForm] UploadFileViewModel uploadFile)
        {
            try
            {
                if (_config.UploadConfig.UploadEnabled)
                {
                    long maxUploadSize = _config.UploadConfig.MaxUploadSize;
                    if (User.Identity.IsAuthenticated)
                    {
                        maxUploadSize = _config.UploadConfig.MaxUploadSizeBasic;
                        IdentityUserInfo userInfo = await IdentityHelper.GetIdentityUserInfo(_config, User.Identity.Name);

                        if (userInfo.AccountType == AccountType.Premium)
                        {
                            maxUploadSize = _config.UploadConfig.MaxUploadSizePremium;
                        }
                    }
                    else
                    {
                        // Non-logged in users are defaulted to 1 day expiration
                        uploadFile.options.ExpirationUnit   = ExpirationUnit.Days;
                        uploadFile.options.ExpirationLength = 1;
                    }
                    if (uploadFile.file.Length <= maxUploadSize)
                    {
                        // convert file to bytes
                        long contentLength = uploadFile.file.Length;

                        // Scan the file to detect a virus
                        if (_config.UploadConfig.VirusScanEnable)
                        {
                            using (Stream fs = uploadFile.file.OpenReadStream())
                            {
                                ClamClient clam = new ClamClient(_config.UploadConfig.ClamServer, _config.UploadConfig.ClamPort);
                                clam.MaxStreamSize = maxUploadSize;
                                ClamScanResult scanResult = await clam.SendAndScanFileAsync(fs);

                                switch (scanResult.Result)
                                {
                                case ClamScanResults.Clean:
                                    break;

                                case ClamScanResults.VirusDetected:
                                    return(Json(new { error = new { message = string.Format("Virus Detected: {0}. As per our <a href=\"{1}\">Terms of Service</a>, Viruses are not permited.", scanResult.InfectedFiles.First().VirusName, Url.SubRouteUrl("tos", "TOS.Index")) } }));

                                case ClamScanResults.Error:
                                    return(Json(new { error = new { message = string.Format("Error scanning the file upload for viruses.  {0}", scanResult.RawResult) } }));

                                case ClamScanResults.Unknown:
                                    return(Json(new { error = new { message = string.Format("Unknown result while scanning the file upload for viruses.  {0}", scanResult.RawResult) } }));
                                }
                            }
                        }

                        // Check content type restrictions (Only for encrypting server side
                        if (!uploadFile.options.Encrypt)
                        {
                            if (_config.UploadConfig.RestrictedContentTypes.Contains(uploadFile.fileType) || _config.UploadConfig.RestrictedExtensions.Contains(uploadFile.fileExt))
                            {
                                return(Json(new { error = new { message = "File Type Not Allowed" } }));
                            }
                        }

                        using (Stream fs = uploadFile.file.OpenReadStream())
                        {
                            Models.Upload upload = UploadHelper.SaveFile(_dbContext,
                                                                         _config,
                                                                         fs,
                                                                         uploadFile.fileType,
                                                                         contentLength,
                                                                         !uploadFile.options.Encrypt,
                                                                         uploadFile.options.ExpirationUnit,
                                                                         uploadFile.options.ExpirationLength,
                                                                         uploadFile.fileExt,
                                                                         uploadFile.iv, null,
                                                                         uploadFile.keySize,
                                                                         uploadFile.blockSize);
                            if (upload != null)
                            {
                                if (User.Identity.IsAuthenticated)
                                {
                                    Users.Models.User user = UserHelper.GetUser(_dbContext, User.Identity.Name);
                                    if (user != null)
                                    {
                                        upload.UserId = user.UserId;
                                        _dbContext.Entry(upload).State = EntityState.Modified;
                                        _dbContext.SaveChanges();
                                    }
                                }
                                return(Json(new { result = new
                                                  {
                                                      name = upload.Url,
                                                      url = Url.SubRouteUrl("u", "Upload.Download", new { file = upload.Url }),
                                                      contentType = upload.ContentType,
                                                      contentLength = StringHelper.GetBytesReadable(upload.ContentLength),
                                                      deleteUrl = Url.SubRouteUrl("u", "Upload.DeleteByKey", new { file = upload.Url, key = upload.DeleteKey }),
                                                      expirationUnit = uploadFile.options.ExpirationUnit.ToString(),
                                                      expirationLength = uploadFile.options.ExpirationLength
                                                  } }));
                            }
                        }
                        return(Json(new { error = new { message = "Unable to upload file" } }));
                    }
                    else
                    {
                        return(Json(new { error = new { message = "File Too Large" } }));
                    }
                }
                return(Json(new { error = new { message = "Uploads are disabled" } }));
            }
            catch (Exception ex)
            {
                return(Json(new { error = new { message = "Exception while uploading file: " + ex.GetFullMessage(true) } }));
            }
        }
Пример #4
0
        public async Task Blog(string username)
        {
            Response.ContentType = "application/rss+xml";

            // If empty, grab the main blog
            List <BlogPost> posts = new List <BlogPost>();

            string blogUrl     = Url.SubRouteUrl("blog", "Blog.Blog");
            string title       = string.Empty;
            string description = string.Empty;
            bool   isSystem    = string.IsNullOrEmpty(username);
            bool   userExists  = false;

            if (isSystem)
            {
                posts   = _dbContext.BlogPosts.Where(p => (p.System && p.Published)).ToList();
                blogUrl = Url.SubRouteUrl("blog", "Blog.Blog");
            }
            else
            {
                Blog.Models.Blog blog = _dbContext.Blogs.Where(p => p.User.Username == username && p.BlogId != _config.BlogConfig.ServerBlogId).FirstOrDefault();
                posts   = _dbContext.BlogPosts.Where(p => (p.BlogId == blog.BlogId && !p.System) && p.Published).ToList();
                blogUrl = Url.SubRouteUrl("blog", "Blog.Blog", new { username = username });
            }
            if (posts.Any())
            {
                if (isSystem)
                {
                    userExists  = true;
                    title       = _config.BlogConfig.Title;
                    description = _config.BlogConfig.Description;
                }
                else
                {
                    Users.Models.User user = UserHelper.GetUser(_dbContext, username);
                    if (user != null)
                    {
                        userExists  = true;
                        title       = user.BlogSettings.Title;
                        description = user.BlogSettings.Description;
                    }
                    else
                    {
                        userExists  = false;
                        title       = "No Blog Available";
                        description = "The specified user does not exist";
                    }
                }

                List <SyndicationItem> items = new List <SyndicationItem>();

                if (userExists)
                {
                    foreach (BlogPost post in posts.OrderByDescending(p => p.BlogPostId))
                    {
                        if (post.Published && post.System == isSystem)
                        {
                            SyndicationItem item = new SyndicationItem()
                            {
                                Id          = post.BlogPostId.ToString(),
                                Title       = post.Title,
                                Description = MarkdownHelper.Markdown(post.Article).Value,
                                Published   = post.DatePublished
                            };

                            item.AddLink(new SyndicationLink(new Uri(Url.SubRouteUrl("blog", "Blog.Post", new { username = post.Blog.User.Username, id = post.BlogPostId }))));
                            item.AddContributor(new SyndicationPerson(post.Blog.User.Username, UserHelper.GetUserEmailAddress(_config, post.Blog.User.Username)));

                            items.Add(item);
                        }
                    }
                }

                using (var xmlWriter = CreateXmlWriter())
                {
                    var feedWriter = new RssFeedWriter(xmlWriter);

                    await feedWriter.WriteTitle(title);

                    await feedWriter.WriteDescription(description);

                    await feedWriter.Write(new SyndicationLink(new Uri(blogUrl)));

                    foreach (SyndicationItem item in items)
                    {
                        await feedWriter.Write(item);
                    }

                    await xmlWriter.FlushAsync();
                }
            }
            else
            {
                using (var xmlWriter = CreateXmlWriter())
                {
                    var feedWriter = new RssFeedWriter(xmlWriter);

                    await feedWriter.WriteTitle("No Blog Available");

                    await feedWriter.WriteDescription("The specified blog does not exist");

                    await feedWriter.Write(new SyndicationLink(new Uri(blogUrl)));

                    await xmlWriter.FlushAsync();
                }
            }
        }