Пример #1
0
        public long CreateUpload(HtmlUpload upload, IUnitOfWork unitOfWork = null)
        {
            IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork);

            try
            {
                string sql = _sqlManager.GetSql("Sql.CreateHtmlUpload.sql");
                dbm.SetSQL(sql);
                dbm.AddParameter("@TenantId", FieldType.BigInt, upload.TenantId);
                dbm.AddParameter("@ElementId", FieldType.BigInt, upload.ElementId);
                dbm.AddParameter("@ImageTenantId", FieldType.BigInt, upload.ImageTenantId ?? (object)DBNull.Value);
                dbm.AddParameter("@ThumbnailImageUploadId", FieldType.BigInt, upload.ThumbnailImageUploadId ?? (object)DBNull.Value);
                dbm.AddParameter("@PreviewImageUploadId", FieldType.BigInt, upload.PreviewImageUploadId ?? (object)DBNull.Value);
                dbm.AddParameter("@ImageUploadId", FieldType.BigInt, upload.ImageUploadId ?? (object)DBNull.Value);
                dbm.AddParameter("@UploadTenantId", FieldType.BigInt, upload.UploadTenantId ?? (object)DBNull.Value);
                dbm.AddParameter("@UploadId", FieldType.BigInt, upload.UploadId ?? (object)DBNull.Value);
                dbm.AddOutputParameter("@HtmlUploadId", FieldType.BigInt);
                Dictionary <string, object> outputValues = new Dictionary <string, object>();
                dbm.ExecuteNonQuery(outputValues);
                return((long)outputValues["@HtmlUploadId"]);
            }
            finally
            {
                if (unitOfWork == null)
                {
                    dbm.Dispose();
                }
            }
        }
Пример #2
0
        public HtmlUpload ReadUpload(long tenantId, long elementId, long htmlUploadId, IUnitOfWork unitOfWork = null)
        {
            IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork);

            try
            {
                HtmlUpload htmlUpload = null;
                string     sql        = _sqlManager.GetSql("Sql.ReadHtmlUpload.sql");
                dbm.SetSQL(sql);
                dbm.AddParameter("@TenantId", FieldType.BigInt, tenantId);
                dbm.AddParameter("@ElementId", FieldType.BigInt, elementId);
                dbm.AddParameter("@HtmlUploadId", FieldType.BigInt, htmlUploadId);
                dbm.ExecuteReader();
                if (dbm.Read())
                {
                    htmlUpload = GetHtmlUploadFromDatabaseManager(dbm);
                }
                return(htmlUpload);
            }
            finally
            {
                if (unitOfWork == null)
                {
                    dbm.Dispose();
                }
            }
        }
Пример #3
0
        public Upload ReadUpload(long tenantId, long elementId, long uploadId, string format, IUnitOfWork unitOfWork = null)
        {
            HtmlUpload htmlUpload = _htmlRepository.ReadUpload(tenantId, elementId, uploadId, unitOfWork);

            if (htmlUpload == null)
            {
                return(null);
            }

            bool isImage = htmlUpload.ImageTenantId.HasValue && htmlUpload.ThumbnailImageUploadId.HasValue &&
                           htmlUpload.PreviewImageUploadId.HasValue && htmlUpload.ImageUploadId.HasValue;
            bool isUpload = htmlUpload.UploadTenantId.HasValue && htmlUpload.UploadId.HasValue;

            if ((!isImage && !isUpload) || (isImage && isUpload))
            {
                return(null);
            }

            if (isImage)
            {
                switch (format)
                {
                case "preview":
                    return((Image)_uploadService.Read(htmlUpload.ImageTenantId.Value, htmlUpload.PreviewImageUploadId.Value, GetHtmlUploadStorageHierarchy(elementId), unitOfWork));

                case "thumbnail":
                    return((Image)_uploadService.Read(htmlUpload.ImageTenantId.Value, htmlUpload.ThumbnailImageUploadId.Value, GetHtmlUploadStorageHierarchy(elementId), unitOfWork));

                default:
                    return(null);
                }
            }

            return(_uploadService.Read(htmlUpload.UploadTenantId.Value, htmlUpload.UploadId.Value, GetHtmlUploadStorageHierarchy(elementId), unitOfWork));
        }
Пример #4
0
        public long PrepareImages(long tenantId, long elementId, 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 uploaded content is valid image
                System.Drawing.Size imageSize = _htmlValidator.ValidatePrepareImages(tenantId, elementId, model);

                // Get HTML settings (does not have to be within unit of work)
                HtmlSettings htmlSettings = (HtmlSettings)New(tenantId);
                htmlSettings.ElementId = elementId;
                Read(htmlSettings);

                // Create thumbnail model
                byte[] thumbnailContent = model.Content;
                if (imageSize.Width > htmlSettings.ThumbnailImageWidth || imageSize.Height > htmlSettings.ThumbnailImageHeight)
                {
                    ResizeInfo thumbnailResizeInfo = new ResizeInfo
                    {
                        Width      = htmlSettings.ThumbnailImageWidth,
                        Height     = htmlSettings.ThumbnailImageHeight,
                        ResizeMode = htmlSettings.ThumbnailImageResizeMode
                    };
                    thumbnailContent = _imageAnalysisService.ResizeImage(model.Content, thumbnailResizeInfo);
                }
                CreateUploadModel thumbnailModel = new CreateUploadModel
                {
                    Content     = thumbnailContent,
                    ContentType = model.ContentType,
                    Name        = model.Name,
                    TenantId    = model.TenantId
                };

                // Create preview model
                byte[] previewContent = model.Content;
                if (imageSize.Width > htmlSettings.PreviewImageWidth || imageSize.Height > htmlSettings.PreviewImageHeight)
                {
                    ResizeInfo previewResizeInfo = new ResizeInfo
                    {
                        Width      = htmlSettings.PreviewImageWidth,
                        Height     = htmlSettings.PreviewImageHeight,
                        ResizeMode = htmlSettings.PreviewImageResizeMode
                    };
                    previewContent = _imageAnalysisService.ResizeImage(model.Content, previewResizeInfo);
                }
                CreateUploadModel previewModel = new CreateUploadModel
                {
                    Content     = previewContent,
                    ContentType = model.ContentType,
                    Name        = model.Name,
                    TenantId    = model.TenantId
                };

                // Create uncommitted 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 uploads
                _uploadService.Commit(tenantId, thumbnailImageUploadId, GetHtmlUploadStorageHierarchy(elementId), unitOfWork ?? localUnitOfWork);
                _uploadService.Commit(tenantId, previewImageUploadId, GetHtmlUploadStorageHierarchy(elementId), unitOfWork ?? localUnitOfWork);
                _uploadService.Commit(tenantId, imageUploadId, GetHtmlUploadStorageHierarchy(elementId), unitOfWork ?? localUnitOfWork);

                // Create HTML image, recording upload IDs of newly created images
                HtmlUpload upload = new HtmlUpload
                {
                    TenantId               = tenantId,
                    ElementId              = elementId,
                    ImageTenantId          = tenantId,
                    ThumbnailImageUploadId = thumbnailImageUploadId,
                    PreviewImageUploadId   = previewImageUploadId,
                    ImageUploadId          = imageUploadId
                };
                long htmlImageId = _htmlRepository.CreateUpload(upload, unitOfWork ?? localUnitOfWork);

                // Commit work if local unit of work in place and return result
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
                return(htmlImageId);
            }
            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();
                }
            }
        }
Пример #5
0
 private bool HtmlUploadIsUpload(HtmlUpload htmlUpload)
 {
     return(htmlUpload.UploadTenantId.HasValue && htmlUpload.UploadId.HasValue);
 }
Пример #6
0
 private bool HtmlUploadIsImage(HtmlUpload htmlUpload)
 {
     return(htmlUpload.ImageTenantId.HasValue && htmlUpload.ThumbnailImageUploadId.HasValue && htmlUpload.PreviewImageUploadId.HasValue && htmlUpload.ImageUploadId.HasValue);
 }