Ejemplo n.º 1
0
        public async Task <UrlEntity> GetEntity(string id)
        {
            UrlEntityInternal entityInternal = null;

            if (Cache != null && Cache.Contains(id))
            {
                entityInternal = Cache[id] as UrlEntityInternal;
            }
            else
            {
                var tableRef = GetTableRefFromId(id);
                if (tableRef == null)
                {
                    return(null);
                }

                entityInternal = await GetEntity(id, null, tableRef);
            }

            return(new UrlEntity()
            {
                Expires = entityInternal.Expires,
                BlockiFrame = entityInternal.BlockiFrame,
                AccessToken = entityInternal.AccessToken,
                Url = entityInternal.Url,
                Id = entityInternal.Id,
                Prefix = entityInternal.Prefix,
                PermanentRedirect = entityInternal.PermanentRedirect,
                UpdateLog = entityInternal.UpdateLog
            });
        }
Ejemplo n.º 2
0
        public async Task Update(string id, string AccessToken, string url, DateTime?Expires = null, bool BlockiFrame = false, string prefix = null)
        {
            var tableRef = GetTableRefFromId(id);

            UrlEntityInternal entity = await GetEntity(id, AccessToken, tableRef);

            if (Expires.HasValue)
            {
                entity.Expires = Expires;
            }
            entity.BlockiFrame = BlockiFrame;
            if (!string.IsNullOrWhiteSpace(prefix))
            {
                entity.Prefix = prefix;
            }
            entity.ETag = "*";
            if (!string.IsNullOrWhiteSpace(url))
            {
                if (!string.IsNullOrWhiteSpace(entity.Url))
                {
                    entity.UpdateLog += (!string.IsNullOrWhiteSpace(entity.UpdateLog) ? "," : "") + entity.Url;
                }

                if (Encoding.UTF8.GetByteCount(entity.UpdateLog) > 64000)
                {
                    entity.UpdateLog = entity.Url;
                }

                entity.Url = url;
            }

            if (Cache != null && Cache.Contains(id))
            {
                Cache[id] = entity;
            }

            await tableRef.ExecuteAsync(TableOperation.Merge(entity));
        }
Ejemplo n.º 3
0
        public async Task <UrlEntity> GetEntityUpdateLog(string id, string AccessToken)
        {
            var tableRef = GetTableRefFromId(id);

            if (tableRef == null)
            {
                return(null);
            }

            UrlEntityInternal entityInternal = await GetEntity(id, AccessToken, tableRef);

            return(new UrlEntity()
            {
                Expires = entityInternal.Expires,
                BlockiFrame = entityInternal.BlockiFrame,
                AccessToken = entityInternal.AccessToken,
                Url = entityInternal.Url,
                Id = entityInternal.Id,
                Prefix = entityInternal.Prefix,
                PermanentRedirect = entityInternal.PermanentRedirect,
                UpdateLog = entityInternal.UpdateLog
            });
        }
Ejemplo n.º 4
0
        public async Task <UrlEntityResponse> Create(string url, DateTime?Expires = null, bool BlockiFrame = false, bool permanentRedirect = false, string prefix = null, string accessToken = null)
        {
            UrlEntityInternal newEntityInternal = new UrlEntityInternal()
            {
                Url               = url,
                Expires           = Expires,
                BlockiFrame       = BlockiFrame,
                AccessToken       = string.IsNullOrWhiteSpace(accessToken) ? randomStringGenerator.GetRandomStringAlfaNumeric(12):accessToken,
                PartitionKey      = randomStringGenerator.GetRandomStringAlfa(3),
                RowKey            = randomStringGenerator.GetRandomStringAlfaNumeric(rnd.Next(3, 6)),
                PermanentRedirect = permanentRedirect,
                Prefix            = prefix,
            };
            TableOperation op = TableOperation.Insert(newEntityInternal);

            var randomTable = Table;

            string id       = string.Format("{0}{1}", randomTable.Name, newEntityInternal.Id);
            string shortUrl = string.IsNullOrWhiteSpace(prefix) ? string.Format("{0}{1}", baseUrl, id): string.Format("{0}{1}/{2}", baseUrl, prefix, id);

            if (url.Contains("http://"))
            {
                shortUrl = shortUrl.Replace("https://", "http://");
            }

            try
            {
                await randomTable.ExecuteAsync(op);

                if (Cache != null)
                {
                    DateTimeOffset cacheExpires = DateTimeOffset.UtcNow.AddDays(1);
                    if (Expires < DateTime.UtcNow.AddDays(1))
                    {
                        cacheExpires = Expires.Value;
                    }
                    Cache.Add(id, newEntityInternal, cacheExpires);
                }

                return(new UrlEntityResponse()
                {
                    AccessToken = newEntityInternal.AccessToken,
                    ShortUrl = shortUrl,
                    Id = id
                });
            }
            catch (Microsoft.WindowsAzure.Storage.StorageException e)
            {
                if (e.RequestInformation.ExtendedErrorInformation.ErrorCode.Contains(Microsoft.WindowsAzure.Storage.Table.Protocol.TableErrorCodeStrings.EntityAlreadyExists) ||
                    e.RequestInformation.ExtendedErrorInformation.ErrorMessage.ToLowerInvariant().Contains("already exists"))
                {
                    return(await Create(url, Expires, BlockiFrame));
                }
                else
                {
                    throw e;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Ejemplo n.º 5
0
        public async Task<UrlEntityResponse> Create(string url, DateTime? Expires = null, bool BlockiFrame = false, bool permanentRedirect = false, string prefix = null, string accessToken = null)
        {
            UrlEntityInternal newEntityInternal=new UrlEntityInternal()
            {
                Url = url,
                Expires = Expires,
                BlockiFrame = BlockiFrame,
                AccessToken =string.IsNullOrWhiteSpace( accessToken) ? randomStringGenerator.GetRandomStringAlfaNumeric(12):accessToken,
                PartitionKey = randomStringGenerator.GetRandomStringAlfa(3),
                RowKey = randomStringGenerator.GetRandomStringAlfaNumeric(rnd.Next(3,6)),
                PermanentRedirect = permanentRedirect,
                Prefix = prefix,
                
            };
            TableOperation op = TableOperation.Insert(newEntityInternal);

            var randomTable = Table;

            string id = string.Format("{0}{1}",  randomTable.Name, newEntityInternal.Id);
            string shortUrl =string.IsNullOrWhiteSpace(prefix) ? string.Format("{0}{1}", baseUrl,id): string.Format("{0}{1}/{2}", baseUrl,prefix, id);

            if (url.Contains("http://"))
                shortUrl = shortUrl.Replace("https://", "http://");

            try
            {
                await randomTable.ExecuteAsync(op);

                if (Cache != null)
                {
                    DateTimeOffset cacheExpires = DateTimeOffset.UtcNow.AddDays(1);
                    if (Expires < DateTime.UtcNow.AddDays(1))
                    {
                        cacheExpires = Expires.Value;
                    }
                    Cache.Add(id, newEntityInternal, cacheExpires);
                }

                return new UrlEntityResponse()
                {
                    AccessToken = newEntityInternal.AccessToken,
                    ShortUrl = shortUrl,
                    Id = id
                };
            }
            catch (Microsoft.WindowsAzure.Storage.StorageException e)
            {
                if (e.RequestInformation.ExtendedErrorInformation.ErrorCode.Contains(Microsoft.WindowsAzure.Storage.Table.Protocol.TableErrorCodeStrings.EntityAlreadyExists) 
                    || e.RequestInformation.ExtendedErrorInformation.ErrorMessage.ToLowerInvariant().Contains("already exists"))
                {
                    return await Create(url,Expires,BlockiFrame);
                }
                else
                {
                    throw e;
                }
            }
            catch (Exception e)
            {                
                throw e;
            }
 

        }