示例#1
0
        private Vanity GetVanityFromUri(TableServiceContext context, Uri uri)
        {
            var rowKey = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(uri.ToString().ToLower()));

            Trace.WriteLine(string.Format("Handling {0} ({1})", uri, rowKey));

            Vanity result = null;

            try
            {
                result = context.CreateQuery <Vanity>(Vanity.TableName)
                         .Where(v =>
                                v.PartitionKey == string.Empty &&
                                v.RowKey == rowKey)
                         .FirstOrDefault();
            }
            catch (System.Data.Services.Client.DataServiceQueryException ex)
            {
                Trace.WriteLine(string.Format("Url {0} Base64Ecoded {1} is invalid. {2}", uri.ToString(), rowKey, ex.Message));
            }

            if (result != null)
            {
                Trace.WriteLine(string.Format("Uri: {0} Vanity.RowKey: {1} Vanity.Destation: {2} Vanity.Options: {3}",
                                              uri.ToString(),
                                              rowKey,
                                              result.Destination,
                                              result.Options));
            }
            return(result);
        }
示例#2
0
        public string GetProperty(string propertyName, string format, System.Globalization.CultureInfo formatProvider, Entities.Users.UserInfo accessingUser, Scope accessLevel, ref bool propertyNotFound)
        {
            string OutputFormat = string.Empty;

            if (format == string.Empty)
            {
                OutputFormat = "g";
            }
            else
            {
                OutputFormat = format;
            }
            propertyName = propertyName.ToLowerInvariant();
            switch (propertyName)
            {
            case "id":
                return(PropertyAccess.FormatString(Id.ToString(), format));

            case "name":
                return(PropertyAccess.FormatString(Name.ToString(), format));

            case "vanity":
                return(PropertyAccess.FormatString(Vanity.ToString(), format));

            case "avatar":
                return(PropertyAccess.FormatString(Avatar.ToString(), format));
            }

            propertyNotFound = true;
            return(string.Empty);
        }
示例#3
0
        public IActionResult Vanity(Vanity vanity)
        {
            ViewBag.Features = _featuresSettings;
            ViewBag.Setup    = _setupSettings;

            if (ModelState.IsValid)
            {
                _ask.NewVanity(vanity);
                ViewBag.Succeed = true;
            }
            return(View());
        }
        public IActionResult Vanity(Vanity vanity)
        {
            ViewBag.Features = _featuresSettings;
            ViewBag.Setup    = _setupSettings;

            vanity.Prefix = $"s{vanity.Prefix}";

            if (ModelState.IsValid)
            {
                _ask.NewVanity(vanity);
                return(RedirectToAction(nameof(VanityProcess), new { prefix = vanity.Prefix }));
            }
            return(View(vanity));
        }
示例#5
0
        protected void Application_Start(object sender, EventArgs e)
        {
            // Create the table if it doesn't exist
            var client = GetTableClient();

            if (client.CreateTableIfNotExist(Vanity.TableName))
            {
                //
                // Must do this voodoo: http://deeperdesign.wordpress.com/2010/03/10/azure-table-storage-what-a-pain-in-the-ass/

                Vanity temp = new Vanity(Guid.NewGuid().ToString())
                {
                    Destination  = CloudConfigurationManager.GetSetting("DefaultDestination") ?? "https://github.com/jamestharpe/VanMan",
                    Options      = (int)RedirectOptions.Default,
                    PartitionKey = string.Empty
                };
                var context = client.GetDataServiceContext();
                context.AddObject(Vanity.TableName, temp);
                context.SaveChanges();
                context.DeleteObject(temp);
                context.SaveChanges(SaveChangesOptions.ContinueOnError);
            }
        }
示例#6
0
 public void NewVanity(Vanity vanity)
 {
     _vanities.Enqueue(vanity);
 }
示例#7
0
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            Trace.WriteLine("Application_BeginRequest");

            var context = GetTableClient().GetDataServiceContext();

            context.IgnoreResourceNotFoundException = true;

            var    uri    = Request.Url;
            Vanity vanity = null;

            do
            {
                vanity = GetVanityFromUri(context, uri);

                var unChoppedUri = new Uri(uri.ToString());
                uri = Chop(uri);

                if (uri == unChoppedUri)
                {
                    break;
                }
            }while (vanity == null);

            if (vanity == null) // Default
            {
                vanity = new Vanity(uri.ToString())
                {
                    Destination  = CloudConfigurationManager.GetSetting("DefaultDestination") ?? "https://github.com/jamestharpe/VanMan",
                    Options      = (int)RedirectOptions.Default,
                    PartitionKey = string.Empty
                };

                //Task.Run(() =>
                {
                    context.AddObject(Vanity.TableName, vanity);
                    context.SaveChanges();
                    //Trace.WriteLine(string.Format("Added record for {0} ({1})", uri, rowKey));
                }//);
            }

            var destination = (((vanity.GetOptions() & RedirectOptions.PreservePath) == RedirectOptions.PreservePath)
                ? vanity.Destination + Request.Url.AbsolutePath
                : vanity.Destination);

            if ((vanity.GetOptions() & RedirectOptions.PreserveQueryString) == RedirectOptions.PreserveQueryString)
            {
                destination += Request.Url.Query;
            }

            if ((vanity.GetOptions() & RedirectOptions.Permanent) == RedirectOptions.Permanent)
            {
                Trace.WriteLine(string.Format("301 Redirecting from {0} to {1}", uri, destination));
                Response.RedirectPermanent(destination, true);
            }
            else
            {
                Trace.WriteLine(string.Format("302 Redirecting from {0} to {1}", uri, destination));
                Response.Redirect(destination, true);
            }
        }