示例#1
0
        public async Task <object> UploadFile(IFormFile file, FileTypeEnum fileType, string domainId, string folderId)
        {
            try
            {
                var    isImageFile = _pictureService.IsImageFile(file);
                byte[] fileBinary;
                using (var ms = new MemoryStream())
                {
                    await file.CopyToAsync(ms);

                    fileBinary = ms.ToArray();
                }

                var    originalName    = file.FileName;
                var    fileEx          = !string.IsNullOrEmpty(Path.GetExtension(file.FileName).ToLower()) ? Path.GetExtension(file.FileName).ToLower() : "." + _pictureService.GetImageFormat(fileBinary).ToString();
                var    uniqueId        = UniqueIDHelper.GenarateRandomString(12, false);
                var    uniqueCode      = Guid.NewGuid();
                var    fileName        = $"{uniqueCode.ToString().Replace("-", "")}{fileEx}";
                var    seoName         = _webHelper.GetSeName(file.FileName, true, false);
                var    isPortraitImage = false;
                var    pathRoot        = _aqFileProvider.GetAbsolutePath(@"contents\");
                var    fullPath        = string.Empty;
                var    pathSubRoot     = string.Empty;
                var    pathThumb12     = string.Empty;
                var    pathThumb14     = string.Empty;
                var    pathThumb16     = string.Empty;
                var    pathThumb18     = string.Empty;
                byte[] fileThumb12;
                byte[] fileThumb14;
                byte[] fileThumb16;
                byte[] fileThumb18;

                if (!_aqFileProvider.DirectoryExists(pathRoot))
                {
                    _aqFileProvider.CreateDirectory(pathRoot);
                }

                if (isImageFile)
                {
                    pathSubRoot = Path.Combine(pathRoot, $@"images\");
                }
                else
                {
                    pathSubRoot = Path.Combine(pathRoot, $@"docs\");
                }

                if (!_aqFileProvider.DirectoryExists(pathSubRoot))
                {
                    _aqFileProvider.CreateDirectory(pathSubRoot);
                }

                domainId = !string.IsNullOrEmpty(domainId.ToLower()) ? domainId.ToLower() : "others";
                var pathDomain = Path.Combine(pathSubRoot, $@"{domainId}\");
                if (!_aqFileProvider.DirectoryExists(pathDomain))
                {
                    _aqFileProvider.CreateDirectory(pathDomain);
                }

                folderId = !string.IsNullOrEmpty(folderId.ToLower()) ? folderId.ToLower() : "others";
                var saveFilePath = Path.Combine(pathDomain, $@"{folderId}\");
                if (!_aqFileProvider.DirectoryExists(saveFilePath))
                {
                    _aqFileProvider.CreateDirectory(saveFilePath);
                }

                fullPath = Path.Combine(saveFilePath, $"{fileName}");

                if (isImageFile)
                {
                    int quality = 100;
                    //in MB
                    double fileSize = Convert.ToDouble(fileBinary.Length.ToString()) / 1024 / 1024;
                    if (fileSize >= 4)
                    {
                        quality = 45;
                    }
                    else if (fileSize >= 2 && fileSize < 4)
                    {
                        quality = 65;
                    }
                    else if (fileSize > 0.5 && fileSize < 2)
                    {
                        quality = 85;
                    }

                    fileBinary = _pictureService.ValidatePicture(fileBinary, 2048, quality);

                    fileThumb12 = _pictureService.ResizePicture(fileBinary, (int)ThumbRatioEnum.half, quality);
                    fileThumb14 = _pictureService.ResizePicture(fileBinary, (int)ThumbRatioEnum.quarter, quality);
                    fileThumb16 = _pictureService.ResizePicture(fileBinary, (int)ThumbRatioEnum.oneSixth, quality);
                    fileThumb18 = _pictureService.ResizePicture(fileBinary, (int)ThumbRatioEnum.oneEighth, quality);

                    pathThumb12 = Path.Combine(saveFilePath, $"{uniqueCode.ToString().Replace("-", "")}_12{fileEx}");
                    pathThumb14 = Path.Combine(saveFilePath, $"{uniqueCode.ToString().Replace("-", "")}_14{fileEx}");
                    pathThumb16 = Path.Combine(saveFilePath, $"{uniqueCode.ToString().Replace("-", "")}_16{fileEx}");
                    pathThumb18 = Path.Combine(saveFilePath, $"{uniqueCode.ToString().Replace("-", "")}_18{fileEx}");

                    isPortraitImage = _pictureService.isPortraitImage(fileBinary);

                    if (!_aqFileProvider.FileExists(pathThumb12))
                    {
                        using (Stream f = File.OpenWrite(pathThumb12))
                        {
                            await f.WriteAsync(fileThumb12, 0, fileThumb12.Length);
                        };
                    }

                    if (!_aqFileProvider.FileExists(pathThumb14))
                    {
                        using (Stream f = File.OpenWrite(pathThumb14))
                        {
                            await f.WriteAsync(fileThumb14, 0, fileThumb14.Length);
                        };
                    }

                    if (!_aqFileProvider.FileExists(pathThumb16))
                    {
                        using (Stream f = File.OpenWrite(pathThumb16))
                        {
                            await f.WriteAsync(fileThumb16, 0, fileThumb16.Length);
                        };
                    }

                    if (!_aqFileProvider.FileExists(pathThumb18))
                    {
                        using (Stream f = File.OpenWrite(pathThumb18))
                        {
                            await f.WriteAsync(fileThumb18, 0, fileThumb18.Length);
                        };
                    }
                }

                var virtualPath       = fullPath.Replace($@"{_hostingEnvironment.WebRootPath}\", "").Replace(@"\", "/");
                var virtuaPathThumb12 = pathThumb12.Replace($@"{_hostingEnvironment.WebRootPath}\", "").Replace(@"\", "/");
                var virtuaPathThumb14 = pathThumb14.Replace($@"{_hostingEnvironment.WebRootPath}\", "").Replace(@"\", "/");
                var virtuaPathThumb16 = pathThumb16.Replace($@"{_hostingEnvironment.WebRootPath}\", "").Replace(@"\", "/");
                var virtuaPathThumb18 = pathThumb18.Replace($@"{_hostingEnvironment.WebRootPath}\", "").Replace(@"\", "/");

                if (!_aqFileProvider.DirectoryExists(fullPath))
                {
                    using (Stream f = File.OpenWrite(fullPath))
                    {
                        await f.WriteAsync(fileBinary, 0, fileBinary.Length);
                    };
                }

                var fileInfo = new FileStreamInfo()
                {
                    UniqueId        = uniqueId,
                    UniqueCode      = uniqueCode,
                    FileTypeFid     = Convert.ToInt32(fileType),
                    OriginalName    = originalName,
                    FileName        = fileName,
                    Seoname         = seoName,
                    FileExtentions  = fileEx,
                    FileSize        = fileBinary.Length,
                    Path            = virtualPath,
                    PathThumb12     = virtuaPathThumb12,
                    PathThumb14     = virtuaPathThumb14,
                    PathThumb16     = virtuaPathThumb16,
                    PathThumb18     = virtuaPathThumb18,
                    UploadedDateUtc = DateTime.UtcNow,
                    UploadedBy      = "",
                    Deleted         = false,
                    IsNew           = true,
                    IsPortraitImage = isPortraitImage
                };

                await InsertFileInfo(fileInfo);

                var fileData = new FileStreamData()
                {
                    FileId   = fileInfo.FileId,
                    FileData = fileBinary
                };

                await InsertFileData(fileData);

                return(new FileUploadResponse()
                {
                    FileName = fileInfo.FileName,
                    FileSize = fileInfo.FileSize,
                    FileId = fileInfo.FileId
                });
            }
            catch
            {
                throw;
            }
        }