//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;

namespace CommunityServer.Discussions.Controls
{
	public class UploadAttachment : CommunityServer.Controls.UploadAttachment
	{
		private Section _section;
		protected override Section Section
		{
			get
			{
				if (_section == null)
					_section = Forums.GetForum(CSContext.Current.SectionID);

				return _section;
			}
		}

		protected override bool IsFileValid(string filename, bool isRemote, int contentLengthBytes, ref string message)
		{
			if (!CSContext.Current.User.IsForumAdministrator)
			{
				ForumAttachmentSettings fas = ForumConfiguration.Instance().AttachmentSettings;
				if (fas.ValidateExtensions)
				{
					if (!fas.ValidationRegex.IsMatch(filename))
					{
						message = string.Format(ResourceManager.GetString("UploadAttachment_InvalidFileExtension"), fas.Extensions.Replace(";", ","));
						return false;
					}
				}

				if (!isRemote && fas.MaxAttachmentSize > 0 && contentLengthBytes / 1024 > fas.MaxAttachmentSize)
				{
					message = string.Format(ResourceManager.GetString("UploadAttachment_TooLarge"), fas.MaxAttachmentSize.ToString());
					return false;
				}
			}

			return true;
		}
	}
}

