Beispiel #1
0
        public async Task <IActionResult> Publish()
        {
            IQueryable <CreativeCommons> creativeCommons = _context.CreativeCommons;

            ArtworkPublishViewModel viewModel = new ArtworkPublishViewModel
            {
                CreativeCommons = await creativeCommons.ToListAsync()
            };

            return(View(viewModel));
        }
Beispiel #2
0
        public async Task <IActionResult> Publish([Bind("Artwork, Document, Tags, ArtworkTag, File, CreativeCommonsId")] ArtworkPublishViewModel viewModel)
        {
            // Reinitialisation
            IQueryable <CreativeCommons> creativeCommons = _context.CreativeCommons;

            viewModel.CreativeCommons = await creativeCommons.ToListAsync();

            if (ModelState.IsValid)
            {
                // FILE UPLOAD
                string uniqueFileName = null;
                if (viewModel.File != null)
                {
                    // Verifications content-type, mime-type, scripting etc
                    bool isPicture = FormFileExtensions.IsPicture(viewModel.File, out string errorImage);
                    bool isAudio   = FormFileExtensions.IsAudio(viewModel.File, out string errorAudio);
                    bool isPdf     = FormFileExtensions.IsPDF(viewModel.File, out string errorPdf);

                    string folder = null;
                    if (isPicture)
                    {
                        folder = "artworks/picture/";
                    }
                    else if (isAudio)
                    {
                        folder = "artworks/audio/";
                    }
                    else if (isPdf)
                    {
                        folder = "artworks/pdf/";
                    }

                    string uploadsFolder = Path.Combine(_hostingEnv.WebRootPath, folder);
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + viewModel.File.FileName;
                    string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                    try
                    {
                        viewModel.File.CopyTo(new FileStream(filePath, FileMode.Create));
                    }
                    catch (Exception e)
                    {
                        ModelState.AddModelError("error", $"An unexpected error occurred : + {e.Message}");
                    }

                    // Creating thumbnails for pic
                    if (isPicture)
                    {
                        string uploadsThumbnailAvatarFolder = Path.Combine(_hostingEnv.WebRootPath, "artworks/picture/thumbnails");
                        string thumbnailFilePath            = Path.Combine(uploadsThumbnailAvatarFolder, uniqueFileName);

                        Image image = Image.FromStream(viewModel.File.OpenReadStream(), true, true);

                        double ratio     = 200 * 1.0 / image.Width;
                        int    newHeight = (int)Math.Floor(image.Height * ratio);

                        var newImage = new Bitmap(200, newHeight);

                        using var thumbnail          = Graphics.FromImage(newImage);
                        thumbnail.CompositingQuality = CompositingQuality.HighSpeed;
                        thumbnail.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                        thumbnail.CompositingMode    = CompositingMode.SourceCopy;
                        thumbnail.DrawImage(image, 0, 0, 200, newHeight);

                        newImage.Save(thumbnailFilePath);
                    }
                }

                // SEO-friendly URL
                SlugHelper helper          = new SlugHelper();
                string     normalizedTitle = helper.GenerateSlug(viewModel.Artwork.Title);

                Artwork artworkToAdd = new Artwork
                {
                    Title             = viewModel.Artwork.Title,
                    NormalizedTitle   = normalizedTitle,
                    Description       = viewModel.Artwork.Description,
                    CreationDate      = DateTime.Now,
                    ReleaseDate       = viewModel.Artwork.ReleaseDate,
                    Privacy           = viewModel.Artwork.Privacy,
                    CCLicense         = await _context.CreativeCommons.FirstAsync(x => x.CreativeCommonsId == viewModel.CreativeCommonsId),
                    Category          = viewModel.Artwork.Category,
                    IsDerivating      = viewModel.Artwork.IsDerivating,
                    LinkDerivating    = viewModel.Artwork.LinkDerivating,
                    LicenseDerivating = viewModel.Artwork.LicenseDerivating,
                    AuthorDerivating  = viewModel.Artwork.AuthorDerivating,
                    Artist            = await _userManager.GetUserAsync(User)
                };

                Document documentToAdd = new Document
                {
                    Media       = viewModel.Document.Media,
                    FilePath    = uniqueFileName,
                    ContentType = viewModel.File.ContentType.ToLower(),
                    Artwork     = artworkToAdd
                };

                _context.Add(artworkToAdd);
                _context.Add(documentToAdd);

                // Tag and linking many-to-many
                char[] delimiterChars = { ',', '.', ';' };

                string[] tags = viewModel.Tags.Name.Split(delimiterChars);

                foreach (string tag in tags)
                {
                    Tag tagToAdd = new Tag {
                        Name = tag
                    };
                    ArtworkTag artworkTagToAdd = new ArtworkTag {
                        Artwork = artworkToAdd, Tag = tagToAdd
                    };

                    _context.Add(tagToAdd);
                    _context.Add(artworkTagToAdd);
                }

                await _context.SaveChangesAsync();

                // We redirect on the new artwork page then
                return(RedirectToAction(nameof(Index), new { userName = _userManager.GetUserName(User), title = normalizedTitle }));
            }
            return(View(viewModel));
        }