示例#1
0
        /// <summary>
        ///     Creates a new image
        /// </summary>
        /// <param name="imageFileUploadId">The image file upload id.</param>
        /// <param name="imageEntityData">The image entity data.</param>
        /// <param name="fileExtension">The file extension.</param>
        /// <returns>
        ///     The requested data.
        /// </returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public EntityRef CreateImageFromUploadedFile(string imageFileUploadId, EntityData imageEntityData, string fileExtension)
        {
            if (string.IsNullOrEmpty(imageFileUploadId))
            {
                throw new ArgumentException(@"The imageFileUploadId is empty.", "imageFileUploadId");
            }

            if (imageEntityData == null)
            {
                throw new ArgumentNullException(@"imageEntityData");
            }

            if (string.IsNullOrEmpty(fileExtension))
            {
                throw new ArgumentNullException(@"fileExtension");
            }

            ImageFileType imageFile;
            string        filePath = string.Empty;

            try
            {
                // Create a new image entity
#pragma warning disable 618
                var entityInfoService = new EntityInfoService( );
#pragma warning restore 618
                imageFile = entityInfoService.CreateEntity(imageEntityData).Entity.AsWritable <ImageFileType>( );

                var        fileManagerService = new FileManagerService( );
                FileDetail fileDetails        = fileManagerService.GetFileDetails(imageFileUploadId);

                filePath = fileDetails.FullName;

                int width;
                int height;

                using (Image image = Image.FromFile(fileDetails.FullName))
                {
                    width  = image.Width;
                    height = image.Height;
                }

                string token;
                using (var source = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    token = FileRepositoryHelper.AddTemporaryFile(source);
                }

                imageFile.FileDataHash  = token;
                imageFile.FileExtension = FileHelper.GetFileExtension(filePath);
                imageFile.ImageWidth    = width;
                imageFile.ImageHeight   = height;
                imageFile.Save( );
            }
            finally
            {
                // Delete the underlying temp file
                if (!string.IsNullOrEmpty(filePath))
                {
                    File.Delete(filePath);
                }
            }

            return(imageFile);
        }