Exemple #1
0
        public async Task <IList <FileModel> > SaveImageAsync(IEnumerable <HttpPostedFileBase> files, string path = null, ImageProcessType imageType = ImageProcessType.Nan)
        {
            var fileList = new List <FileModel>();

            if (files == null)
            {
                return(fileList);
            }
            foreach (var file in files)
            {
                if (imageType == ImageProcessType.Attachment)
                {
                    var validationFormat = await _imageValidation.GetFormatAttachment(file);

                    if (validationFormat != null)
                    {
                        throw new ArgumentNullException(nameof(validationFormat));
                    }
                }
                else
                {
                    var validationFormat = await _imageValidation.GetFormatImage(file);

                    if (validationFormat != null)
                    {
                        throw new ArgumentNullException(nameof(validationFormat));
                    }
                }

                if (path == null)
                {
                    path = FileConst.ImagesWebPath;
                }
                var fileSaved = new FileModel()
                {
                    Id        = SequentialGuidGenerator.NewSequentialGuid(),
                    Name      = SequentialGuidGenerator.NewSequentialGuid() + Path.GetExtension(file.FileName),
                    Type      = FileConst.FileType,
                    Extension = Path.GetExtension(file.FileName),
                    Size      = file.ContentLength,
                    Path      = path
                };
                var byteFile = await ConvertToByteArrayAsync(file);

                var pathToSave = Path.Combine(path, fileSaved.Name);
                await CreateFileAsync(byteFile, fileSaved.Name, path);

                if (imageType != ImageProcessType.Attachment)
                {
                    var thumbPath       = HttpContext.Current.Server.MapPath(FileConst.ThumbPath);
                    var pathThumbToSave = Path.Combine(thumbPath, fileSaved.Name);
                }

                fileList.Add(fileSaved);
                switch (imageType)
                {
                case ImageProcessType.ProductImage:
                    await _imageBuilder.ProductImageProcessAsync(pathToSave);

                    break;

                case ImageProcessType.CompanyCoverFileName:
                    await _imageBuilder.CompanyCoverFileProcessAsync(pathToSave);

                    break;

                case ImageProcessType.LogoFileName:
                    await _imageBuilder.LogoProcessAsync(pathToSave);

                    break;

                case ImageProcessType.CompanyImages:
                    await _imageBuilder.CompanyImagesFileProcessAsync(pathToSave);

                    break;

                case ImageProcessType.ProductImages:
                    await _imageBuilder.ProductImageProcessAsync(pathToSave);

                    break;
                }
            }
            return(fileList);
        }