Exemplo n.º 1
0
        public IActionResult UploadCoverPhoto(int playlistId, [FromForm] CoverFile file)
        {
            var photo  = file.Photo;
            var sizeMB = photo.Length / 1024 / 1000;

            if (sizeMB > 10)
            {
                return(StatusCode(400, new ErrorDetails()
                {
                    errorId = ErrorList.WrongSize.Id,
                    errorMessage = ErrorList.WrongSize.Description
                }));
            }

            int userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(c => c.Type == claimTypes.Id.ToString()).Value);

            if (!db.CheckPlaylistExists(playlistId, userId))
            {
                return(StatusCode(403, new ErrorDetails()
                {
                    errorId = ErrorList.UnauthorizedAction.Id, errorMessage = ErrorList.UnauthorizedAction.Description
                }.ToString()));
            }

            var extension = Path.GetExtension(photo.FileName).Replace(".", "");

            if (!AllowedExtensions.Contains(extension))
            {
                return(BadRequest(new ErrorDetails()
                {
                    errorId = ErrorList.InputDataError.Id, errorMessage = ErrorList.InputDataError.Description
                }.ToString()));
            }
            ;

            var imageName = Guid.NewGuid().ToString().Replace("-", "");
            var thumbName = $"{imageName}_cover.{extension}";

            var additionalPath = $"/{userId}";
            var newThumbPath   = $"{additionalPath}/{thumbName}";

            db.AddPlaylistCover(playlistId, userId, newThumbPath);

            var thumbPath = COVER_STORAGE + newThumbPath;

            var resizedThumb = helper.ResizeImage(Image.FromStream(photo.OpenReadStream()));

            if (!Directory.Exists(Path.GetDirectoryName(thumbPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(thumbPath));
            }

            helper.SaveImage(resizedThumb, thumbPath);

            var base64Photo = Convert.ToBase64String(System.IO.File.ReadAllBytes(thumbPath));

            return(Ok(new { response = new { coverPhoto = base64Photo } }));
        }
Exemplo n.º 2
0
        public async Task <BitmapImage> GetImageThumbnailAsync()
        {
            var thumbnail = await CoverFile.GetThumbnailAsync(ThumbnailMode.PicturesView);

            BitmapImage bitmapImage = new BitmapImage();

            bitmapImage.SetSource(thumbnail);
            thumbnail.Dispose();
            return(bitmapImage);
        }