Example #1
0
 private void CreateFileImageAlternate(File item, IImageFormat format, Image rawImage, Size size)
 {
     using (var resizedImage = rawImage.Clone(img =>
                                              img.Resize(new ResizeOptions {
         Size = size, Mode = ResizeMode.Max
     })
                                              ))
     {
         byte[] resizedBlob;
         using (var ms = new MemoryStream())
         {
             resizedImage.Save(ms, format);
             resizedBlob = ms.ToArray();
         }
         var alt = new FileImageAlternate
         {
             Metadata = new FileImageAlternateMetadata(item.Metadata)
             {
                 Width  = resizedImage.Width,
                 Height = resizedImage.Height
             },
             FileId   = item.FileId,
             FileBlob = resizedBlob
         };
         alt.Metadata.Size = alt.FileBlob.Length;
         _fileData.Add(alt);
     }
 }
Example #2
0
        public async Task <IActionResult> OnPostUploadAsync(List <IFormFile> model)
        {
            if (!ModelState.IsValid || model == null)
            {
                return(BadRequest());
            }

            var userIdClaim = User.Claims.Single(c => c.Type == "id");
            var user        = await _userManager.FindByIdAsync(userIdClaim.Value);

            // see other considerations and solutions in the docs:
            // https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.1
            long size = model.Sum(f => f.Length);

            var files = new List <File>();

            foreach (var formFile in model)
            {
                if (formFile.Length > 0)
                {
                    byte[] content;
                    using (var memoryStream = new System.IO.MemoryStream())
                    {
                        await formFile.CopyToAsync(memoryStream);

                        content = memoryStream.ToArray();
                    }
                    var filename = formFile.FileName;
                    var fileId   = Guid.NewGuid();
                    files.Add(new File
                    {
                        FileId   = fileId,
                        FileBlob = content,
                        Metadata = new FileMetadata
                        {
                            FileId      = fileId,
                            ContentType = formFile.ContentType,
                            Extension   = System.IO.Path.GetExtension(filename),
                            Size        = content.LongLength,
                            Audit       = new AuditCreate
                            {
                                CreatedUserId   = user.Id,
                                CreatedUserName = user.Name
                            }
                        }
                    });
                }
            }

            files.ForEach(f =>
            {
                _fileData.Add(f);
                _imageResizer.CreateFileImageAlternates(f);
            });

            return(Ok(files.Select(x => x.Metadata)));
        }
Example #3
0
        public async Task <IActionResult> Upload(UploadViewModel model, IList <IFormFile> files)
        {
            if ((model != null) && ModelState.IsValid)
            {
                var user = await GetLoggedInUser();

                var containerName = user.CompanyId.ToString().ToLower();
                await _blobService.CreateContainerIfNotExists(containerName);

                foreach (var formFile in files)
                {
                    if ((formFile != null) && (formFile.Length > 0))
                    {
                        using (var fileStream = formFile.OpenReadStream())
                        {
                            await
                            _blobService.UploadBlobFromStream(containerName, formFile.FileName, fileStream,
                                                              formFile.ContentType, false);
                        }
                        var file = new FileInfo
                        {
                            ContentType     = model.ContentType,
                            FileName        = formFile.FileName,
                            FileContentType = formFile.ContentType,
                            FileSizeInBytes = formFile.Length,
                            ContainerName   = containerName,
                            Description     = model.Description,
                            ReadOnly        = model.ReadOnly,
                            FileOwner       = user.UserName
                        };

                        _fileData.Add(file);
                        _fileData.Commit();

                        SendFileNotification(FileOperations.Uploaded, formFile.FileName, user.CompanyId.ToString());
                    }
                }

                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
Example #4
0
        public async Task <IActionResult> Upload(UploadViewModel model)
        {
            if ((model != null) && ModelState.IsValid)
            {
                var user = await GetLoggedInUser();

                var containerName = user.CompanyId.ToString().ToLower();
                await _blobService.CreateContainerIfNotExists(containerName);

                if ((model.UploadedFile != null) && (model.UploadedFile.Length > 0))
                {
                    using (var fileStream = model.UploadedFile.OpenReadStream())
                    {
                        await
                        _blobService.UploadBlobFromStream(containerName, model.FileName, fileStream,
                                                          model.UploadedFile.ContentType, false);
                    }

                    var file = new FileInfo
                    {
                        ContentType     = model.ContentType,
                        FileName        = model.FileName,
                        FileContentType = model.UploadedFile.ContentType,
                        FileSizeInBytes = model.UploadedFile.Length,
                        ContainerName   = containerName
                    };

                    _fileData.Add(file);
                    _fileData.Commit();

                    SendFileNotification(FileOperations.Uploaded, model.FileName, user.CompanyId.ToString());

                    return(RedirectToAction(nameof(Details), new { id = file.Id }));
                }
            }
            return(View(model));
        }