public async Task UpdateProfilePicture(UpdateProfilePictureInput input)
        {
            var allowToUseGravatar = await SettingManager.GetSettingValueAsync <bool>(AppSettings.UserManagement.AllowUsingGravatarProfilePicture);

            if (!allowToUseGravatar)
            {
                input.UseGravatarProfilePicture = false;
            }

            await SettingManager.ChangeSettingForUserAsync(
                AbpSession.ToUserIdentifier(),
                AppSettings.UserManagement.UseGravatarProfilePicture,
                input.UseGravatarProfilePicture.ToString().ToLowerInvariant()
                );

            if (input.UseGravatarProfilePicture)
            {
                return;
            }

            byte[] byteArray;

            var imageBytes = _tempFileCacheManager.GetFile(input.FileToken);

            if (imageBytes == null)
            {
                throw new UserFriendlyException("There is no such image file with the token: " + input.FileToken);
            }

            using (var bmpImage = new Bitmap(new MemoryStream(imageBytes)))
            {
                var width  = (input.Width == 0 || input.Width > bmpImage.Width) ? bmpImage.Width : input.Width;
                var height = (input.Height == 0 || input.Height > bmpImage.Height) ? bmpImage.Height : input.Height;
                var bmCrop = bmpImage.Clone(new Rectangle(input.X, input.Y, width, height), bmpImage.PixelFormat);

                using (var stream = new MemoryStream())
                {
                    bmCrop.Save(stream, bmpImage.RawFormat);
                    byteArray = stream.ToArray();
                }
            }

            if (byteArray.Length > MaxProfilPictureBytes)
            {
                throw new UserFriendlyException(L("ResizedProfilePicture_Warn_SizeLimit",
                                                  AppConsts.ResizedMaxProfilPictureBytesUserFriendlyValue));
            }

            var user = await UserManager.GetUserByIdAsync(AbpSession.GetUserId());

            if (user.ProfilePictureId.HasValue)
            {
                await _binaryObjectManager.DeleteAsync(user.ProfilePictureId.Value);
            }

            var storedFile = new BinaryObject(AbpSession.TenantId, byteArray, $"Profile picture of user {AbpSession.UserId}. {DateTime.UtcNow}");
            await _binaryObjectManager.SaveAsync(storedFile);

            user.ProfilePictureId = storedFile.Id;
        }
        public ActionResult DownloadTempFile(FileDto file)
        {
            var fileBytes = _tempFileCacheManager.GetFile(file.FileToken);

            if (fileBytes == null)
            {
                return(NotFound(L("RequestedFileDoesNotExists")));
            }

            return(File(fileBytes, file.FileType, file.FileName));
        }
        public ActionResult DownloadTempFile(FileDto file)
        {
            var fileBytes = _tempFileCacheManager.GetFile(file.FileToken);

            if (fileBytes == null)
            {
                return(NotFound(L(AppConsts.FileNotExistMessage)));
            }

            return(File(fileBytes, file.FileType, file.FileName));
        }
Exemple #4
0
        public async Task UpdateProfilePicture(UpdateProfilePictureInput input)
        {
            byte[] byteArray;

            var imageBytes = _tempFileCacheManager.GetFile(input.FileToken);

            if (imageBytes == null)
            {
                throw new UserFriendlyException("There is no such image file with the token: " + input.FileToken);
            }

            using (var bmpImage = new Bitmap(new MemoryStream(imageBytes)))
            {
                var width  = (input.Width == 0 || input.Width > bmpImage.Width) ? bmpImage.Width : input.Width;
                var height = (input.Height == 0 || input.Height > bmpImage.Height) ? bmpImage.Height : input.Height;
                var bmCrop = bmpImage.Clone(new Rectangle(input.X, input.Y, width, height), bmpImage.PixelFormat);

                using (var stream = new MemoryStream())
                {
                    bmCrop.Save(stream, bmpImage.RawFormat);
                    byteArray = stream.ToArray();
                }
            }

            if (byteArray.Length > MaxProfilPictureBytes)
            {
                throw new UserFriendlyException(L("ResizedProfilePicture_Warn_SizeLimit", AppConsts.ResizedMaxProfilPictureBytesUserFriendlyValue));
            }

            var user = await UserManager.GetUserByIdAsync(AbpSession.GetUserId());

            if (user.ProfilePictureId.HasValue)
            {
                await _binaryObjectManager.DeleteAsync(user.ProfilePictureId.Value);
            }

            var storedFile = new BinaryObject(AbpSession.TenantId, byteArray);

            _binaryObjectManager.SaveAsync(storedFile);

            user.ProfilePictureId = storedFile.Id;
        }
        private byte[] CompressFiles(List <FileDto> files)
        {
            using (var outputZipFileStream = new MemoryStream())
            {
                using (var zipStream = new ZipArchive(outputZipFileStream, ZipArchiveMode.Create))
                {
                    foreach (var file in files)
                    {
                        var fileBytes = _tempFileCacheManager.GetFile(file.FileToken);
                        var entry     = zipStream.CreateEntry(file.FileName);

                        using (var originalFileStream = new MemoryStream(fileBytes))
                            using (var zipEntryStream = entry.Open())
                            {
                                originalFileStream.CopyTo(zipEntryStream);
                            }
                    }
                }

                return(outputZipFileStream.ToArray());
            }
        }
Exemple #6
0
        /// <summary>
        /// 修改用户头像
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task UpdateHeadImage(UpdateHeadImageInput input)
        {
            string fixedFilePath = String.Empty, absoluteFilePath = String.Empty;
            string absolutePath = "/AppData/FileUpload/Image/HeadImage/";
            string fixedPath    = _environment.ContentRootPath + absolutePath;
            var    imageBytes   = _tempFileCacheManager.GetFile(input.FileToken);

            if (imageBytes == null)
            {
                throw new UserFriendlyException("There is no such image file with the token: " + input.FileToken);
            }
            using (var bmpImage = new Bitmap(new MemoryStream(imageBytes)))
            {
                var width  = (input.Width == 0 || input.Width > bmpImage.Width) ? bmpImage.Width : input.Width;
                var height = (input.Height == 0 || input.Height > bmpImage.Height) ? bmpImage.Height : input.Height;
                var bmCrop = bmpImage.Clone(new Rectangle(input.X, input.Y, width, height), bmpImage.PixelFormat);
                // TODO:限制裁剪后图片大小 1、先将图片保存成流 2、判断大小 3、大了结束。小了再将流保存成图片
                if (!Directory.Exists(fixedPath))
                {
                    Directory.CreateDirectory(fixedPath);
                }
                absoluteFilePath = absolutePath + input.FileToken + ".jpg";
                fixedFilePath    = fixedPath + input.FileToken + ".jpg";
                bmCrop.Save(fixedFilePath, bmpImage.RawFormat);
            }

            User user = await UserManager.GetUserByIdAsync(AbpSession.GetUserId());

            if (user.ProfilePictureId.HasValue)
            {
                await _binaryObjectManager.DeleteAsync(user.ProfilePictureId.Value);
            }

            var storedFile = new BinaryObject(AbpSession.TenantId, absoluteFilePath);
            await _binaryObjectManager.SaveAsync(storedFile);

            user.ProfilePictureId = storedFile.Id;
        }
        public async Task UpdateDocuments(UpdateDocumentsInput inputDocuments)
        {
            byte[] byteArray;
            string _fileExt = inputDocuments.FileExt;
            var    docBytes = _tempFileCacheManager.GetFile(inputDocuments.FileToken);

            /* if (inputDocuments.FileType != null)
             * {
             *   _fileType = inputProductImages.FileType;
             *   _fileExtArr = _fileType.Split("/");
             *   if (_fileExtArr.Length > 1)
             *       _fileExt = _fileExtArr[1];
             * }*/


            if (docBytes == null)
            {
                throw new UserFriendlyException("There is no such image file with the token: " + inputDocuments.FileToken);
            }
            using (var docStream = new MemoryStream(docBytes))
            {
                byteArray = docStream.ToArray();
            }

            if (byteArray.Length > MaxProductImagesBytes)
            {
                throw new UserFriendlyException(L("Image_Warn_SizeLimit", AppConsts.MaxImageBytesUserFriendlyValue));
            }
            var inputDto = new DocumentDto {
            };

            if (inputDocuments.ProductId != 0)
            {
                inputDto = new DocumentDto
                {
                    ProductId   = inputDocuments.ProductId,
                    Url         = inputDocuments.FileToken + "." + _fileExt,
                    Description = inputDocuments.Description,
                    Name        = inputDocuments.Name
                };
            }
            else
            {
                inputDto = new DocumentDto
                {
                    ServiceId   = inputDocuments.ServiceId,
                    Url         = inputDocuments.FileToken + "." + _fileExt,
                    Description = inputDocuments.Description,
                    Name        = inputDocuments.Name
                };
            }


            var documentDto = ObjectMapper.Map <Document>(inputDto);


            if (AbpSession.TenantId != null)
            {
                documentDto.TenantId = (int?)AbpSession.TenantId;
            }

            documentDto.Bytes = byteArray;

            await _documentRepository.InsertAsync(documentDto);
        }
        public async Task UpdateProductImages(UpdateProductImagesInput inputProductImages)
        {
            byte[] byteArray;
            string _fileType;

            string[] _fileExtArr;
            string   _fileExt   = "jpeg";
            var      imageBytes = _tempFileCacheManager.GetFile(inputProductImages.FileToken);

            if (inputProductImages.FileType != null)
            {
                _fileType   = inputProductImages.FileType;
                _fileExtArr = _fileType.Split("/");
                if (_fileExtArr.Length > 1)
                {
                    _fileExt = _fileExtArr[1];
                }
            }


            if (imageBytes == null)
            {
                throw new UserFriendlyException("There is no such image file with the token: " + inputProductImages.FileToken);
            }

            using (var bmpImage = new Bitmap(new MemoryStream(imageBytes)))
            {
                var width  = (inputProductImages.Width == 0 || inputProductImages.Width > bmpImage.Width) ? bmpImage.Width : inputProductImages.Width;
                var height = (inputProductImages.Height == 0 || inputProductImages.Height > bmpImage.Height) ? bmpImage.Height : inputProductImages.Height;
                var bmCrop = bmpImage.Clone(new Rectangle(inputProductImages.X, inputProductImages.Y, width, height), bmpImage.PixelFormat);

                using (var stream = new MemoryStream())
                {
                    bmCrop.Save(stream, bmpImage.RawFormat);
                    byteArray = stream.ToArray();
                }
            }

            if (byteArray.Length > MaxProductImagesBytes)
            {
                throw new UserFriendlyException(L("Image_Warn_SizeLimit", AppConsts.MaxImageBytesUserFriendlyValue));
            }

            var inputDto = new ProductImageDto
            {
                ProductId   = inputProductImages.ProductId,
                Url         = inputProductImages.FileToken + "." + _fileExt,
                Description = inputProductImages.Description
            };

            var productImageDto = ObjectMapper.Map <ProductImage>(inputDto);


            if (AbpSession.TenantId != null)
            {
                productImageDto.TenantId = (int?)AbpSession.TenantId;
            }

            productImageDto.Bytes = byteArray;

            await _productImageRepository.InsertAsync(productImageDto);
        }
Exemple #9
0
        public async Task CheckAttachment <T>(int AttachmentTypeId, string ObjectID, AttachmentsViewModelWithEntityDto <T> input, AttachmentUploadType type)
        {
            var attachmentType = await _lookup_attachmentTypeRepository.GetAsync(AttachmentTypeId);

            if (attachmentType == null)
            {
                throw new UserFriendlyException(L("InvalidAttachmentTypeIdCode"), L("InvalidAttachmentTypeIdCode_Detail"));
            }

            if ((input.Attachments != null && input.Attachments.Count > 0) || (input.AttachmentsToDelete != null && input.AttachmentsToDelete.Count > 0))
            {
                var version       = 1;
                var numOfAttached = input.Attachments.Count;

                if (type == AttachmentUploadType.edit)
                {
                    var oldAttachments = await _attachmentFileRepository.GetAll().Where(e => e.ObjectId == ObjectID && e.AttachmentTypeId == AttachmentTypeId).OrderByDescending(e => e.Version).ToListAsync();

                    var anyAttachment = oldAttachments.FirstOrDefault();

                    if (anyAttachment != null)
                    {
                        var lastGroup = oldAttachments.Where(e => e.Version == anyAttachment.Version).ToList();

                        var tokensToDelete = new Dictionary <string, string>();
                        foreach (var item in input.AttachmentsToDelete)
                        {
                            if (!tokensToDelete.ContainsKey(item.FileToken))
                            {
                                tokensToDelete.Add(item.FileToken, item.FileToken);
                            }
                        }
                        version = anyAttachment.Version;

                        var notDeletedAttaches = lastGroup.Where(e => !tokensToDelete.ContainsKey(e.FileToken)).ToList();
                        var DeletedAttaches    = lastGroup.Where(e => tokensToDelete.ContainsKey(e.FileToken)).ToList();
                        if (notDeletedAttaches.Count != lastGroup.Count)
                        {
                            version = anyAttachment.Version + 1;

                            foreach (var att in notDeletedAttaches)
                            {
                                att.Version = version;
                            }
                        }
                        foreach (var item in DeletedAttaches)
                        {
                            await _attachmentFileRepository.DeleteAsync(item);
                        }
                        numOfAttached += notDeletedAttaches.Count;
                    }
                }

                if (numOfAttached > attachmentType.MaxAttachments)
                {
                    throw new UserFriendlyException(L("MaxAttachmentsCode"), L("MaxAttachmentsCode_Detail"));
                }
                var listAttachments = new List <UploadFilesInputDto>();
                foreach (var att in input.Attachments)
                {
                    var FileByte = _tempFileCacheManager.GetFile(att.FileToken);
                    if (FileByte == null)
                    {
                        throw new UserFriendlyException("There is no such image file with the token: " + att.FileToken);
                    }
                    CheckFileType(att.FileName, attachmentType);
                    if (attachmentType.MaxSize != 0 && FileByte.Length > attachmentType.MaxSize * 1000)
                    {
                        throw new UserFriendlyException(L("FileSizeNotAllowdCode"), L("FileSizeNotAllowdCode_Detail"));
                    }
                    var UploadFilesInputDto = ObjectMapper.Map <UploadFilesInputDto>(att);
                    UploadFilesInputDto.FileByte = FileByte;
                    UploadFilesInputDto.Version  = version;
                    listAttachments.Add(UploadFilesInputDto);
                }
                await AddAttachment(
                    attachmentType, ObjectID,
                    listAttachments
                    );
            }
            else
            {
                if (attachmentType.IsRequired)
                {
                    throw new UserFriendlyException(L("AttachmentNotFoundCode"), L("AttachmentNotFoundCode_Detail"));
                }
            }
        }