コード例 #1
0
        public static async Task <CommandResponse <string> > GenerateAvatar(Stream imageStream, string fileName, string mimetype, bool purgeTempFile = true)
        {
            var fileManager = FileManager.Instance;
            var fileCheck   = fileManager.IsUploadPermitted(fileName, FileType.Avatar, mimetype, imageStream.Length);

            if (fileCheck.Success)
            {
                var key = new FileKey();
                key.FileType = FileType.Avatar;
                key.ID       = await GenerateRandomFilename(Path.GetExtension(fileName), FileType.Avatar);

                await GenerateImageThumbnail(fileManager, key, imageStream, VoatSettings.Instance.AvatarSize, false, false);

                return(CommandResponse.Successful(key.ID));
            }

            return(CommandResponse.FromStatus <string>(null, fileCheck.Status, fileCheck.Message));
        }
コード例 #2
0
        public async Task <CommandResponse <Domain.Models.Set> > CreateOrUpdateSet(Set set)
        {
            DemandAuthentication();

            set.Name = set.Name.TrimSafe();
            //Evaulate Rules
            var context = new VoatRuleContext(User);

            context.PropertyBag.Set = set;
            var outcome = VoatRulesEngine.Instance.EvaluateRuleSet(context, RuleScope.CreateSet);

            if (!outcome.IsAllowed)
            {
                return(MapRuleOutCome <Set>(outcome, null));
            }

            var existingSet = _db.SubverseSet.FirstOrDefault(x => x.ID == set.ID);

            if (existingSet != null)
            {
                var perms = SetPermission.GetPermissions(existingSet.Map(), User.Identity);

                if (!perms.EditProperties)
                {
                    return(CommandResponse.FromStatus <Set>(null, Status.Denied, "User does not have permission to edit this set"));
                }

                //HACK: Need to clear this entry out of cache if name changes and check name
                if (!existingSet.Name.IsEqual(set.Name))
                {
                    if (_db.SubverseSet.Any(x => x.Name.ToLower() == set.Name.ToLower() && x.UserName.ToLower() == UserName.ToLower()))
                    {
                        return(CommandResponse.FromStatus <Set>(null, Status.Denied, "A set with this name already exists"));
                    }
                    CacheHandler.Instance.Remove(CachingKey.Set(existingSet.Name, existingSet.UserName));
                }

                existingSet.Name        = set.Name;
                existingSet.Title       = set.Title;
                existingSet.Description = set.Description;
                existingSet.IsPublic    = set.IsPublic;

                await _db.SaveChangesAsync().ConfigureAwait(CONSTANTS.AWAIT_CAPTURE_CONTEXT);

                return(CommandResponse.FromStatus <Set>(existingSet.Map(), Status.Success));
            }
            else
            {
                //Validation - MOVE TO RULES SYSTEM MAYBE
                if (!VoatSettings.Instance.SetCreationEnabled || VoatSettings.Instance.MaximumOwnedSets <= 0)
                {
                    return(CommandResponse.FromStatus <Set>(null, Status.Denied, "Set creation is currently disabled"));
                }

                if (VoatSettings.Instance.MaximumOwnedSets > 0)
                {
                    var d = new DapperQuery();
                    d.Select = $"SELECT COUNT(*) FROM {SqlFormatter.Table("SubverseSet", "subSet")}";
                    d.Where  = "subSet.\"Type\" = @Type AND subSet.\"UserName\" = @UserName";
                    d.Parameters.Add("Type", (int)SetType.Normal);
                    d.Parameters.Add("UserName", UserName);

                    var setCount = _db.Connection.ExecuteScalar <int>(d.ToString(), d.Parameters);
                    if (setCount >= VoatSettings.Instance.MaximumOwnedSets)
                    {
                        return(CommandResponse.FromStatus <Set>(null, Status.Denied, $"Sorry, Users are limited to {VoatSettings.Instance.MaximumOwnedSets} sets and you currently have {setCount}"));
                    }
                }


                //Create new set
                try
                {
                    var setCheck = GetSet(set.Name, UserName);
                    if (setCheck != null)
                    {
                        return(CommandResponse.FromStatus <Set>(null, Status.Denied, "A set with same name and owner already exists"));
                    }

                    var newSet = new SubverseSet
                    {
                        Name            = set.Name,
                        Title           = set.Title,
                        Description     = set.Description,
                        UserName        = UserName,
                        Type            = (int)SetType.Normal,
                        IsPublic        = set.IsPublic,
                        CreationDate    = Repository.CurrentDate,
                        SubscriberCount = 1, //Owner is a subscriber. Reminds me of that hair club commercial: I"m not only the Set Owner, I'm also a subscriber.
                    };

                    _db.SubverseSet.Add(newSet);
                    await _db.SaveChangesAsync().ConfigureAwait(CONSTANTS.AWAIT_CAPTURE_CONTEXT);

                    _db.SubverseSetSubscription.Add(new SubverseSetSubscription()
                    {
                        SubverseSetID = newSet.ID, UserName = UserName, CreationDate = CurrentDate
                    });
                    await _db.SaveChangesAsync().ConfigureAwait(CONSTANTS.AWAIT_CAPTURE_CONTEXT);

                    return(CommandResponse.Successful(newSet.Map()));
                }
                catch (Exception ex)
                {
                    return(CommandResponse.Error <CommandResponse <Set> >(ex));
                }
            }
        }
コード例 #3
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));
        }