Ejemplo n.º 1
0
        /// <summary>
        ///     Uploads a file from a posted file
        /// </summary>
        /// <param name="file"></param>
        /// <param name="uploadFolderPath"></param>
        /// <param name="localizationService"></param>
        /// <param name="onlyImages"></param>
        /// <returns></returns>
        public static UploadFileResult UploadFile(this HttpPostedFileBase file, string uploadFolderPath,
                                                  ILocalizationService localizationService, bool onlyImages = false)
        {
            var upResult = new UploadFileResult {
                UploadSuccessful = true
            };
            var storageProvider = StorageProvider.Current;

            var fileOkResult = file.CanBeUploaded(localizationService);

            if (fileOkResult.IsOk)
            {
                if (!Directory.Exists(uploadFolderPath))
                {
                    Directory.CreateDirectory(uploadFolderPath);
                }

                var fileName      = fileOkResult.FileName;
                var fileExtension = fileOkResult.FileExtension;

                // Store these here as we may change the values within the image manipulation
                string newFileName;

                // See if this is an image, if so then do some extra checks
                if (fileOkResult.IsImage)
                {
                    // Rotate image if wrong want around
                    var sourceimage = file.ToImage();

                    // Change the extension to jpg as that's what we are saving it as
                    fileName = fileName.Replace(fileExtension, "");
                    fileName = string.Concat(fileName, "jpg");

                    // Sort the file name
                    newFileName = fileName.CreateFilename();

                    // Upload the image
                    upResult = sourceimage.Upload(uploadFolderPath, newFileName);

                    // Remove now
                    sourceimage.Dispose();
                }
                else
                {
                    // Sort the file name
                    newFileName = fileName.CreateFilename();
                    upResult.UploadedFileUrl = storageProvider.SaveAs(uploadFolderPath, newFileName, file);
                }

                upResult.UploadedFileName = newFileName;
            }
            else
            {
                upResult.UploadSuccessful = false;
                upResult.ErrorMessage     = fileOkResult.Message;
            }

            return(upResult);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Uploads a file from a posted file
        /// </summary>
        /// <param name="file"></param>
        /// <param name="uploadFolderPath"></param>
        /// <param name="localizationService"></param>
        /// <param name="onlyImages"></param>
        /// <returns></returns>
        public static UploadFileResult UploadFile(this HttpPostedFileBase file, string uploadFolderPath,
                                                  ILocalizationService localizationService, IImageCommand imageCommand = null, IImageRepository imageRepository = null, IImageService imageService = null,
                                                  bool isAvatar = false, Guid imageSaveId = default(Guid))
        {
            var extension = Path.GetExtension(file.FileName);
            var fileName  = $"{Guid.NewGuid()}{extension}";
            var upResult  = new UploadFileResult {
                UploadSuccessful = true
            };
            var storageProvider = StorageProvider.Current;

            var fileOkResult = file.CanBeUploaded(localizationService);

            if (fileOkResult.IsOk)
            {
                if (!Directory.Exists(uploadFolderPath))
                {
                    Directory.CreateDirectory(uploadFolderPath);
                }

                var fileExtension = fileOkResult.FileExtension;

                // Store these here as we may change the values within the image manipulation
                string newFileName = string.Empty;

                // See if this is an image, if so then do some extra checks
                if (fileOkResult.IsImage)
                {
                    var sourceimage = file.ToImage();

                    if (!(imageService is null) && !(imageRepository is null))
                    {
                        var newFileId = imageSaveId != default(Guid) ? imageSaveId : Guid.NewGuid();
                        newFileName = $"{newFileId}.webp";

                        var transformedImage = isAvatar ? imageService.TransformImageForAvatar(new Bitmap(sourceimage)) :
                                               imageService.TransformImageForGroupHeader(new Bitmap(sourceimage));

                        var imageStream = new MemoryStream();
                        imageStream.Write(transformedImage.Bytes, 0, transformedImage.Bytes.Length);
                        upResult.UploadedFileUrl = storageProvider.SaveAs(uploadFolderPath, newFileName, imageStream);

                        var imageVM = new ImageViewModel(upResult.UploadedFileName, "user avatar")
                        {
                            FileName      = newFileName,
                            FileSizeBytes = Convert.ToInt32(file.ContentLength),
                            Height        = transformedImage.Height,
                            Width         = transformedImage.Width,
                            MediaType     = transformedImage.MediaType
                        };

                        if (isAvatar)
                        {
                            var membershipUserImageId = imageRepository.GetMembershipUserImageId(imageSaveId);
                            if (!(membershipUserImageId is null))
                            {
                                imageVM.Id = (Guid)membershipUserImageId;
                                _          = imageCommand.Update(imageVM);
                            }
                            else
                            {
                                var imageId = imageCommand.Create(imageVM);
                                _ = imageCommand.UpdateMembershipUserImageId(imageSaveId, imageId, newFileName);
                            }
                        }
                        else
                        {
                            var groupImageId = imageRepository.GetGroupImageId(imageSaveId);
                            if (!(groupImageId is null))
                            {
                                imageVM.Id = (Guid)groupImageId;
                                _          = imageCommand.Update(imageVM);
                            }
                            else
                            {
                                var imageId = imageCommand.Create(imageVM);
                                _ = imageCommand.UpdateGroupImageId(imageSaveId, imageId, newFileName);
                            }
                        }