/// <summary>
 /// Create a new Upload object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 public static Upload CreateUpload(global::System.Int32 id)
 {
     Upload upload = new Upload();
     upload.Id = id;
     return upload;
 }
        /// <summary>
        /// Adds a new image to the gallery
        /// </summary>
        /// <param name="galleryName"></param>
        /// <param name="fileName"></param>
        /// <param name="user"></param>
        /// <param name="inputStream"></param>
        /// <returns></returns>
        public string UploadImage(string galleryName, string fileName, IPrincipal user, Stream inputStream)
        {
            lock (UploadSyncContext)
             {
            var destinationFile = GetGalleryFile(galleryName, fileName);

            var entities = new DatabaseEntities();
            var uploads = from upload in entities.Uploads
                          where upload.Gallery == galleryName && upload.User == user.Identity.Name
                          select upload;

            var uploadEntity = uploads.Where(upload => upload.Image == fileName).SingleOrDefault();
            if (uploadEntity != null)
            {
               uploadEntity.LastUpdate = DateTime.Now;
               uploadEntity.User = user.Identity.Name;
            }
            else
            {
               if (!IsAdminUser(user))
               {
                  var uploadEnabled = (from set in entities.Settings where set.Gallery == galleryName select set.UploadEnabled).SingleOrDefault();
                  if (uploadEnabled != true)
                  {
                     throw new SecurityException("Not authorized");
                  }

                  if (destinationFile.Exists)
                  {
                     // duplicate file name, not allowed to overwrite other's files
                     throw new DuplicateNameException("A file with same name already exists in this gallery.");
                  }

                  // check upload limit, if any
                  if (UserUploadLimit > 0 && uploads.Count() >= UserUploadLimit)
                  {
                     throw new IndexOutOfRangeException("Upload limit exceeded.");
                  }
               }

               // add new upload record
               uploadEntity = new Upload
               {
                  Gallery = galleryName,
                  Image = fileName,
                  User = user.Identity.Name,
                  LastUpdate = DateTime.Now
               };

               entities.Uploads.AddObject(uploadEntity);
            }

            var success = false;

            try
            {
               // remove thumbnail
               var thumbnailFile = GetGalleryFile(galleryName, "thumb\\" + fileName);
               if (thumbnailFile.Exists)
               {
                  thumbnailFile.Delete();
               }

               var resizeTo = GetMaxImageSize();

               using (var image = Image.FromStream(inputStream))
               {
                  var title = image.GetImageTitle();
                  if (string.IsNullOrWhiteSpace(title))
                  {
                     title = Path.GetFileNameWithoutExtension(destinationFile.Name);
                  }

                  uploadEntity.Title = title;

                  using (var resized = image.Resize(resizeTo.Width, resizeTo.Height))
                  {
                     // resize and save to file
                     resized.Save(destinationFile.FullName, 100, image.RawFormat);
                  }
               }

               // commit db changes
               entities.SaveChanges();

               // done
               success = true;
            }
            finally
            {
               if (!success)
               {
                  destinationFile.Delete();
               }
            }

            return destinationFile.FullName;
             }
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the Uploads EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToUploads(Upload upload)
 {
     base.AddObject("Uploads", upload);
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the Uploads EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToUploads(Upload upload)
 {
     base.AddObject("Uploads", upload);
 }