Esempio n. 1
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "track/{trackId}/follow")] HttpRequest req, string trackId, TraceWriter log)
        {
            try
            {
                // check postId and trackId provided
                if (!Tools.IsValidGuid(trackId))
                {
                    return(new UnauthorizedResult());
                }

                string authToken = req.Headers["Authorization"];

                if (authToken == null)
                {
                    return(new UnauthorizedResult());
                }

                // validate authKey
                AuthClaim authClaim = AuthRepository.ValidateAuthClaim(authToken);
                if (authClaim == null)
                {
                    return(new UnauthorizedResult());
                }

                // get the track
                TrackAuth track = await TrackRepository.GetTrack(trackId);

                if (track == null || (track.is_private && track.PartitionKey != authClaim.user_id))
                {
                    return(new UnauthorizedResult());
                }

                // trackFollow body
                string         requestBody = new StreamReader(req.Body).ReadToEnd();
                TrackFollowDTO dto         = JsonConvert.DeserializeObject <TrackFollowDTO>(requestBody);

                // insert or update the follow
                TrackFollow trackFollow = new TrackFollow();
                trackFollow.feed_follow_type          = dto.feed?.ToLower() == "all" || dto.feed?.ToLower() == "none" ? dto.feed.ToLower() : null;
                trackFollow.notifications_follow_type = dto.notifications?.ToLower() == "all" || dto.notifications?.ToLower() == "none" ? dto.notifications.ToLower() : null;
                trackFollow.criteria = dto.criteria;
                trackFollow.user_id  = authClaim.user_id;
                trackFollow.track_id = track.RowKey;

                FollowRepository.InsertOrReplaceTrackFollow(trackFollow);

                return(new OkResult());
            }
            catch (Exception e)
            {
                log.Error(e.Message);
                return(new UnauthorizedResult());
            }
        }
        public async Task <IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var track = new TrackAuth(user.Id, Guid.NewGuid().ToString())
            {
                description = Input.Description,
                is_private  = Input.IsPrivate,
                name        = Input.Name,
                tags        = string.IsNullOrWhiteSpace(Input.Tags) ? null : string.Join(",", Tools.ValidateTags(Input.Tags.Split(',').ToList()))
            };

            // generate small thumb
            if (Input.UploadTrackImage != null)
            {
                using (MemoryStream uploadStream = new MemoryStream())
                {
                    await Input.UploadTrackImage.CopyToAsync(uploadStream);

                    byte[] data = uploadStream.ToArray();

                    using (MemoryStream input = new MemoryStream(data))
                    {
                        using (MemoryStream output = new MemoryStream())
                        {
                            Images.CropSquare(150, input, output);

                            await BlobRepository.UploadFileAsync(BlobRepository.TracksContainer, output.ToArray(), track.RowKey + "/thumb");
                        }
                    }

                    using (MemoryStream input = new MemoryStream(data))
                    {
                        using (MemoryStream output = new MemoryStream())
                        {
                            Images.CropSquare(32, input, output);

                            await BlobRepository.UploadFileAsync(BlobRepository.TracksContainer, output.ToArray(), track.RowKey + "/thumb_mini");
                        }
                    }

                    track.has_image = true;
                }
            }

            // create track
            TrackAuth createdTrack = await TrackRepository.CreateTrack(track);

            FollowRepository.InsertOrReplaceTrackFollow(new TrackFollow()
            {
                track_id                  = createdTrack.RowKey,
                user_id                   = user.Id,
                feed_follow_type          = "none",
                notifications_follow_type = "none"
            });

            _logger.LogInformation($"Track with ID '{createdTrack.RowKey}' has been created by '{user.Id}'.");
            return(RedirectToPage("./Index"));
        }