private static ImagenDto ConvertToDto(Imagen imagen)
 {
     return Mapper.Map<ImagenDto>(imagen);
 }
 private void MapEditedToOriginal(Imagen originalImage, Imagen imagen)
 {
     originalImage.IsDeleted = imagen.IsDeleted;
     originalImage.Created = imagen.Created;
     originalImage.IsFavourite = imagen.IsFavourite;
     originalImage.Name = imagen.Name;
     originalImage.OriginalUrl = imagen.OriginalUrl;
     originalImage.Path = imagen.Path;
    // originalImage.Tags = imagen.Tags;
     originalImage.UserUploaded = imagen.UserUploaded;
 }
        // Refactor
        private void SetTagsToUserUploadedImage(string tags, Imagen img)
        {
            if (!String.IsNullOrEmpty(tags))
            {
                var tagsArray = tags.Split(',');

                foreach (var currentTag in tagsArray)
                {
                    var originalTag = this.tagService.GetTagByName(currentTag);
                    var imagenTag = new ImagenTag();
                    if (originalTag != null)
                    {
                        imagenTag.Tag = originalTag;
                    }
                    else
                    {
                        var tag = new Tag() 
                        {
                            Name = currentTag,
                        };
                        imagenTag.Tag = tag;
                    }
                    img.ImagenTags.Add(imagenTag);
                }
            }
        }
        public IHttpActionResult UploadImage()
        {
            var request = HttpContext.Current.Request;
            HttpPostedFile file = request.Files["pcFile"];

            if (file != null)
            {
                //string serverImagesDirectoryForCurrentUserUrl = PathsAndUrlsHelper.GetCurrentUserImagesDirectoryFullPath();

                var newImage = new Imagen 
                { 
                    Name = request.Form["imageName"],
                    OriginalUrl = request.Form["url"],
                    Created = DateTime.Now,
                    UserUploaded = true
                };

                SetTagsToUserUploadedImage(request.Form["tags"], newImage);


                string pic = PathsAndUrlsHelper.CreateLocalFileName(newImage.Name, newImage.OriginalUrl);
                string localPath = Path.Combine(PathsAndUrlsHelper.GetLocalImagesDirectoryPathForCurrentLoggedInUser(), pic);
                newImage.Path = PathsAndUrlsHelper.CreateImageFullUrl(newImage.Name, newImage.OriginalUrl);

                // file is uploaded and info stored in the DB
                file.SaveAs(localPath);

                var createdImage = this.imagenService.Create(newImage);

                using (MemoryStream ms = new MemoryStream())
                {
                    file.InputStream.CopyTo(ms);
                    byte[] array = ms.GetBuffer();
                }

                return Ok(ConvertToDto(createdImage));
            }

            // after successfully uploading redirect the user
            return BadRequest();
        }