예제 #1
0
        /// <summary>
        /// Unpackages the template file and adds a new template in the db.
        /// </summary>
        /// <exception cref="ValidationException">Throws a ValidationException if the posted file is not valid or the application does not have access rights.</exception>
        public static void Add(Template template, Stream packageStream, HttpContextBase context, ITemplatesService service)
        {
            string baseDirectory = null;
            if (packageStream == null)
            {
                throw new ArgumentNullException("postedStream");
            }
            try
            {
                if (packageStream.Length > 1024 * 1024 * 3)
                {
                    throw new ValidationException(new ValidationError("postedFile", ValidationErrorType.MaxLength));
                }
                var cache = new CacheWrapper(context);

                if (cache.Template != null && template.Key != null && cache.Template.Name.ToUpper() == template.Key.ToUpper())
                {
                    throw new ValidationException(new ValidationError("postedFile", ValidationErrorType.DuplicateNotAllowed));
                }

                service.AddOrUpdate(template);

                baseDirectory = Config.General.TemplateFolderPathFull(template.Key);
                SaveFilesToDrive(baseDirectory, packageStream);
                PrepareTemplateBody(baseDirectory + "\\template.html", UrlHelper.GenerateContentUrl(Config.General.TemplateFolderPath(template.Key) + "/", context), context);
                ChopTemplateFile(baseDirectory + "\\template.html");
            }
            catch (ValidationException)
            {
                //Delete the folder
                if (baseDirectory != null)
                {
                    try
                    {
                        SafeIO.Directory_Delete(baseDirectory, true);
                    }
                    catch (UnauthorizedAccessException)
                    {

                    }
                }
                if (template.Id > 0)
                {
                    service.Delete(template.Id);
                }
                throw;
            }
        }