Ejemplo n.º 1
0
        public async Task <ActionResult <ShortUrl> > ShortenUrlAsync([FromForm] string targetUrl, [FromForm] string name = null, [FromForm] bool generateName = false)
        {
            if (!Uri.TryCreate(targetUrl, UriKind.Absolute, out _))
            {
                return(UnprocessableEntity("Invalid url!"));
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                name = GeneratorService.GenerateNextName();
            }
            else
            {
                bool nameUsed = await DbContext.ShortUrls.AnyAsync(x => x.Name == name);

                if (nameUsed)
                {
                    if (!generateName)
                    {
                        return(Conflict("this name is already being used"));
                    }

                    name = GeneratorService.GenerateNextName();
                }
            }

            var target = await DbContext.RedirectTargets.Where(x => x.TargetUrl == targetUrl).FirstOrDefaultAsync();

            if (target == null)
            {
                target = new RedirectTarget(targetUrl);
                DbContext.RedirectTargets.Add(target);
            }

            var userId   = Guid.Parse(User.FindFirstValue("UserId"));
            var shortUrl = new ShortUrl(userId, target.Id, name);

            DbContext.ShortUrls.Add(shortUrl);
            await DbContext.SaveChangesAsync();

            WebhookService.QueueShortUrl(shortUrl);

            return(Ok(shortUrl));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <User> > CreateUserAsync([FromForm] string username, [FromForm] string password)
        {
            bool nameUsed = await DbContext.Users.AnyAsync(x => x.Username == username);

            if (nameUsed)
            {
                Logger.LogInformation($"Someone tried to create a user but the name is already being used: {username}");
                return(Conflict("Name already used"));
            }

            string token          = Random.NextString(20);
            var    user           = new User(username, password, token);
            var    previewOptions = new PreviewOptions(user.Id);

            DbContext.Add(user);
            DbContext.Add(previewOptions);
            await DbContext.SaveChangesAsync();

            Logger.LogInformation($"Someone created a user: {username} [{user.Id}]");

            return(user);
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <Upload> > ReceiveUploadAsync([FromHeader] string filename = null, [FromHeader] bool generateName = false)
        {
            if (string.IsNullOrWhiteSpace(filename) && string.IsNullOrWhiteSpace(Request.ContentType))
            {
                return(BadRequest());
            }

            string extension = null;
            string name      = null;

            if (string.IsNullOrWhiteSpace(filename))
            {
                if (!TryGetExtension(Request.ContentType, out extension))
                {
                    return(BadRequest());
                }

                name = GeneratorService.GenerateNextName();
            }
            else
            {
                if (!TryGetContentType(filename, out string ctype, out name, out extension))
                {
                    return(BadRequest());
                }

                if (string.IsNullOrWhiteSpace(Request.ContentType))
                {
                    Request.ContentType = ctype;
                }

                bool nameUsed = await DbContext.Uploads.AnyAsync(x => x.Name == name);

                if (nameUsed)
                {
                    if (!generateName)
                    {
                        return(Conflict("The fileName is already being used!"));
                    }

                    name = GeneratorService.GenerateNextName();
                }
            }

            var content = new MemoryStream();
            await Request.Body.CopyToAsync(content);

            string checksum  = content.GenerateChecksum();
            var    localFile = await DbContext.LocalFiles.Where(x => x.Checksum == checksum)
                               .FirstOrDefaultAsync();

            if (localFile == null)
            {
                localFile = await DataService.StoreUploadDataAsync(content);

                DbContext.Add(localFile);
            }

            var userId = Guid.Parse(User.FindFirstValue("UserId"));
            var upload = new Upload(userId, localFile.Id, name, extension, Request.ContentType);

            DbContext.Add(upload);

            await DbContext.SaveChangesAsync();

            WebhookService.QueueUpload(upload);

            return(upload);
        }