Пример #1
0
        // add new submission
        public static async Task <string> AddNewSubmission(Message submissionModel, Subverse targetSubverse, string userName)
        {
            using (var db = new voatEntities())
            {
                // LINK TYPE SUBMISSION
                if (submissionModel.Type == 2)
                {
                    // strip unicode if title contains unicode
                    if (ContainsUnicode(submissionModel.Linkdescription))
                    {
                        submissionModel.Linkdescription = StripUnicode(submissionModel.Linkdescription);
                    }

                    // reject if title is whitespace or < than 5 characters
                    if (submissionModel.Linkdescription.Length < 5 || String.IsNullOrWhiteSpace(submissionModel.Linkdescription))
                    {
                        return("The title may not be less than 5 characters.");
                    }

                    // make sure the input URI is valid
                    if (!UrlUtility.IsUriValid(submissionModel.MessageContent))
                    {
                        // ABORT
                        return("The URI you are trying to submit is invalid.");
                    }

                    // check if target subvere allows submissions from globally banned hostnames
                    if (!targetSubverse.exclude_sitewide_bans)
                    {
                        // check if hostname is banned before accepting submission
                        var domain = UrlUtility.GetDomainFromUri(submissionModel.MessageContent);
                        if (BanningUtility.IsHostnameBanned(domain))
                        {
                            // ABORT
                            return("The hostname you are trying to submit is banned.");
                        }
                    }

                    // check if user has reached daily crossposting quota
                    if (UserHelper.DailyCrossPostingQuotaUsed(userName, submissionModel.MessageContent))
                    {
                        // ABORT
                        return("You have reached your daily crossposting quota for this URL.");
                    }

                    // check if target subverse has thumbnails setting enabled before generating a thumbnail
                    if (targetSubverse.enable_thumbnails)
                    {
                        // try to generate and assign a thumbnail to submission model
                        submissionModel.Thumbnail = await ThumbGenerator.ThumbnailFromSubmissionModel(submissionModel);
                    }

                    // flag the submission as anonymized if it was submitted to a subverse with active anonymized_mode
                    if (targetSubverse.anonymized_mode)
                    {
                        submissionModel.Anonymized = true;
                    }
                    else
                    {
                        submissionModel.Name = userName;
                    }

                    // accept submission and save it to the database
                    submissionModel.Subverse = targetSubverse.name;
                    submissionModel.Likes    = 1;
                    db.Messages.Add(submissionModel);

                    await db.SaveChangesAsync();
                }
                else
                // MESSAGE TYPE SUBMISSION
                {
                    // strip unicode if submission contains unicode
                    if (ContainsUnicode(submissionModel.Title))
                    {
                        submissionModel.Title = StripUnicode(submissionModel.Title);
                    }

                    // reject if title is whitespace or less than 5 characters
                    if (submissionModel.Title.Length < 5 || String.IsNullOrWhiteSpace(submissionModel.Title))
                    {
                        return("Sorry, submission title may not be less than 5 characters.");
                    }

                    // flag the submission as anonymized if it was submitted to a subverse with active anonymized_mode
                    if (targetSubverse.anonymized_mode)
                    {
                        submissionModel.Anonymized = true;
                    }
                    else
                    {
                        submissionModel.Name = userName;
                    }

                    // grab server timestamp and modify submission timestamp to have posting time instead of "started writing submission" time
                    submissionModel.Subverse = targetSubverse.name;
                    submissionModel.Date     = DateTime.Now;
                    submissionModel.Likes    = 1;
                    db.Messages.Add(submissionModel);

                    if (ContentProcessor.Instance.HasStage(ProcessingStage.InboundPreSave))
                    {
                        submissionModel.MessageContent = ContentProcessor.Instance.Process(submissionModel.MessageContent, ProcessingStage.InboundPreSave, submissionModel);
                    }

                    await db.SaveChangesAsync();

                    if (ContentProcessor.Instance.HasStage(ProcessingStage.InboundPostSave))
                    {
                        ContentProcessor.Instance.Process(submissionModel.MessageContent, ProcessingStage.InboundPostSave, submissionModel);
                    }
                }
            }

            // null is returned if no errors were raised
            return(null);
        }
Пример #2
0
        public static async Task <CommandResponse <string> > GenerateThumbnail(Uri uri, bool purgeTempFile = true)
        {
            if (VoatSettings.Instance.OutgoingTraffic.Enabled)
            {
                var url = uri.ToString();
                if (!String.IsNullOrEmpty(url) && UrlUtility.IsUriValid(url))
                {
                    try
                    {
                        //Ok this all needs to be centralized, we should only make 1 request to a remote resource
                        using (var httpResource = new HttpResource(
                                   new Uri(url),
                                   new HttpResourceOptions()
                        {
                            AllowAutoRedirect = true
                        },
                                   VoatSettings.Instance.OutgoingTraffic.Proxy.ToWebProxy()))
                        {
                            var result = await httpResource.GiddyUp();

                            if (httpResource.IsImage)
                            {
                                var fileManager = FileManager.Instance;
                                var fileCheck   = fileManager.IsUploadPermitted(url, FileType.Thumbnail, httpResource.Response.Content.Headers.ContentType.MediaType, httpResource.Stream.Length);

                                if (fileCheck.Success)
                                {
                                    var key = new FileKey();
                                    key.FileType = FileType.Thumbnail;
                                    key.ID       = await GenerateRandomFilename(".jpg", FileType.Thumbnail);

                                    var stream = httpResource.Stream;

                                    await GenerateImageThumbnail(fileManager, key, stream, VoatSettings.Instance.ThumbnailSize, true);

                                    return(CommandResponse.Successful(key.ID));
                                }
                            }
                            else if (httpResource.Image != null)
                            {
                                //just do it. again.
                                return(await GenerateThumbnail(httpResource.Image));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        EventLogger.Instance.Log(ex, new { url = url, type = FileType.Thumbnail });
                        var response = CommandResponse.Error <CommandResponse <string> >(ex);
                        response.Response = ""; //Make sure this returns string.empty as other failures do.
                        return(response);
                    }
                }
                EventLogger.Instance.Log(new LogInformation()
                {
                    Type     = LogType.Debug,
                    Category = "Thumbnail Diag",
                    Message  = "Default Response",
                    Data     = new { url = url },
                    Origin   = VoatSettings.Instance.Origin
                });
            }
            return(CommandResponse.FromStatus <string>("", Status.Invalid));
        }