Ejemplo n.º 1
0
        public IEnumerable <Image> GetUploadedImages(HttpRequestBase request, params string[] imageDefinitionKeys)
        {
            var images = new List <Image>();

            foreach (string inputTagName in request.Files)
            {
                var file = request.Files[inputTagName];
                if (file.ContentLength > 0)
                {
                    // upload the image to filesystem
                    if (IsNotImage(file))
                    {
                        throw new ValidationException(string.Format("File '{0}' is not an image file (*.jpg)", file.FileName));
                    }

                    var image = new Image
                    {
                        FileName    = Guid.NewGuid(),
                        Description = Path.GetFileName(file.FileName)
                    };

                    file.SaveAs(imageFileService.GetFullPath(image.FileNameAsString));

                    // convert the image to main and thumb sizes
                    imageService.CreateSizedImages(image, imageDefinitionKeys);

                    // File.Delete(imageFileService.GetFullPath(image.FileNameAsString));

                    images.Add(image);
                }
            }

            return(images);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a main and thumnail image from the given image of the taget size
        /// </summary>
        /// <param name="image"></param>
        /// <param name="imageDefinitionKeys"></param>
        public void CreateSizedImages(Image image, params string[] imageDefinitionKeys)
        {
            var imagePath = imageFileService.GetFullPath(image.FileNameAsString);

            using (var gdiImage = System.Drawing.Image.FromFile(imagePath))
            {
                foreach (var imageDefinitionKey in imageDefinitionKeys)
                {
                    var key      = imageDefinitionKey;
                    var imageDef = imageDefinitions.SingleOrDefault(x => x.Key == key);
                    if (imageDef == null)
                    {
                        throw new InvalidOperationException("Could not find image definition with the key '{0}'".With(imageDefinitionKey));
                    }

                    CreateSizedImage(gdiImage, imageDef.Size, Image.GetExtendedName(imagePath, imageDef.Extension));
                }
            }
        }
Ejemplo n.º 3
0
        public mediaObjectInfo newMediaObject(object blogid, string username, string password, mediaObject mediaobject)
        {
            CreateServices();

            ValidateUser(username, password);

            try
            {
                //We don't validate the file because newMediaObject allows file to be overwritten
                //But we do check the directory and create if necessary
                //The media object's name can have extra folders appended so we check for this here too.
                var path = imageFileService.GetFullPath(mediaobject.name.Replace("/", "\\"));

                if (!Directory.Exists(Path.GetDirectoryName(path)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                }

                using (var fileStream = new FileStream(path, FileMode.Create))
                    using (var binaryWriter = new BinaryWriter(fileStream))
                    {
                        binaryWriter.Write(mediaobject.bits);
                    }
            }
            //Any IO exceptions, we throw a new XmlRpcFault Exception
            catch (IOException exception)
            {
                throw new XmlRpcFaultException(0, exception.Message);
            }

            ////If all works, we return a mediaobjectinfo struct back holding the URL.
            mediaObjectInfo media;

            media.url = baseControllerService.SiteUrl + imageFileService.GetRelativeUrl(mediaobject.name);
            return(media);
        }