Exemplo n.º 1
0
        public ImageUploadIds PrepareImages(long tenantId, CreateUploadModel model, IUnitOfWork unitOfWork = null)
        {
            // If we don't have a unit of work in place, create one now so that we can rollback all changes in case of failure
            IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            // Begin work
            try
            {
                // Check that website allows avatars and that uploaded content is valid image
                AuthenticationValidateResult result = _authenticationValidator.ValidatePrepareImages(tenantId, model);

                // Create thumbnail model
                ResizeInfo thumbnailResizeInfo = new ResizeInfo
                {
                    Width      = result.Web.UserThumbnailImageWidth.Value,
                    Height     = result.Web.UserThumbnailImageHeight.Value,
                    ResizeMode = result.Web.UserThumbnailImageResizeMode.Value
                };
                byte[]            thumbnailContent = _imageAnalysisService.ResizeImage(model.Content, thumbnailResizeInfo);
                CreateUploadModel thumbnailModel   = new CreateUploadModel
                {
                    Content     = thumbnailContent,
                    ContentType = model.ContentType,
                    Name        = model.Name,
                    TenantId    = model.TenantId
                };

                // Create preview model
                ResizeInfo previewResizeInfo = new ResizeInfo
                {
                    Width      = result.Web.UserPreviewImageWidth.Value,
                    Height     = result.Web.UserPreviewImageHeight.Value,
                    ResizeMode = result.Web.UserPreviewImageResizeMode.Value
                };
                byte[]            previewContent = _imageAnalysisService.ResizeImage(model.Content, previewResizeInfo);
                CreateUploadModel previewModel   = new CreateUploadModel
                {
                    Content     = previewContent,
                    ContentType = model.ContentType,
                    Name        = model.Name,
                    TenantId    = model.TenantId
                };

                // Create uploads for thumbnail, preview and original image
                long thumbnailImageUploadId = _uploadService.Create(thumbnailModel, unitOfWork ?? localUnitOfWork);
                long previewImageUploadId   = _uploadService.Create(previewModel, unitOfWork ?? localUnitOfWork);
                long imageUploadId          = _uploadService.Create(model, unitOfWork ?? localUnitOfWork);

                // Commit work if local unit of work in place and return result
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
                return(new ImageUploadIds {
                    ThumbnailImageUploadId = thumbnailImageUploadId, PreviewImageUploadId = previewImageUploadId, ImageUploadId = imageUploadId
                });
            }
            catch (ValidationErrorException)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            catch (Exception ex)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
            finally
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Dispose();
                }
            }
        }