Example #1
0
        public Task <string> Compress(string url)
        {
            if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
            {
                Response.StatusCode = 400;                                // bad request
                return(Task.FromResult("Error: url should be absolute")); // don't allocate an async state machine if user provided invalid input
            }
            return(CompressInternal());

            async Task <string> CompressInternal()
            {
                string userKey = await _authTokenProvider.GetTokenAsync(HttpContext, createIfNotExists : true).ConfigureAwait(false);

                long     newlyGeneratedLinkId;
                Sequence seq = await _sequenceProvider.GetSequenceAsync("linksSequence_" + userKey).ConfigureAwait(false);

                using (CancellationTokenSource source = new CancellationTokenSource(_defaultTimeout))
                {
                    seq = await seq.GetNextSequenceValue(source.Token).ConfigureAwait(false);

                    newlyGeneratedLinkId = seq.Value;
                }
                string             key    = String.Concat(userKey, "~", _converter.GenerateKey(newlyGeneratedLinkId));
                long               userId = _converter.RegenerateId(userKey);
                ShortenedLinkModel model  = new ShortenedLinkModel {
                    Id = new ShortenedLinkModelId {
                        LinkId = newlyGeneratedLinkId, UserId = userId
                    }, Key = key, Value = url
                };

                using (CancellationTokenSource source = new CancellationTokenSource(_defaultTimeout))
                    await _links.InsertOneAsync(model, _insertOptions, source.Token).ConfigureAwait(false);

                return(model.Key);
            }
        }
 public AuthTokenProvider(ISequenceProvider provider, DecBase68Converter converter)
 {
     _authorizedUsersSequence = provider.GetSequenceAsync("authorizedUsersSequence").GetAwaiter().GetResult();
     _converter = converter;
 }