예제 #1
0
        public ActionResult Index()
        {
            var model = new PageTemplatesIndexViewModel();

            model.SelectedTemplate = settingService.Get <string>(SettingField.Theme);

            var viewDirectory = Server.MapPath("~/Views");

            foreach (var directory in Directory.GetDirectories(viewDirectory))
            {
                var name = directory.Substring(directory.LastIndexOf('\\') + 1);
                if (name == "Shared")
                {
                    continue;
                }

                var thumbUrl = Url.Content("~/Content/Themes/" + name + "/screenshot.png");

                if (!System.IO.File.Exists(Server.MapPath(thumbUrl)))
                {
                    try
                    {
                        ScreenshotHelper.GenerateScreenshot(
                            Url.Action("Index", "Home", new { area = "" }, Request.Url.Scheme) + "?theme=" + name,
                            Server.MapPath(thumbUrl));
                    }
                    catch
                    {
                        // Unable to generate screenshot. No big deal
                    }
                }

                thumbUrl += "?seed=" + new Random().Next();

                model.Templates.Add(new PageTemplateIndexViewModel
                {
                    Name      = name,
                    Thumbnail = thumbUrl
                });
            }

            return(View(model));
        }
예제 #2
0
        public ActionResult Create(string name, HttpPostedFileBase file)
        {
            if (name.Contains(".") || name.Contains("/") || name.Contains("\\") ||
                file == null || file.ContentLength <= 0)
            {
                return(null);
            }

            var importDir = Server.MapPath("~/Import");
            var zipFile   = Path.Combine(importDir,
                                         "Template_" + name + "_" + DateTime.Now.ToString("yyyyMMddTHHmmss") + ".zip");

            file.SaveAs(zipFile);

            var viewsDir = Path.Combine(Server.MapPath("~/Views"), name);

            if (!Directory.Exists(viewsDir))
            {
                Directory.CreateDirectory(viewsDir);
            }

            var contentDir = Path.Combine(Server.MapPath("~/Content/Themes"), name);

            if (!Directory.Exists(contentDir))
            {
                Directory.CreateDirectory(contentDir);
            }

            using (var zip = ZipFile.Read(zipFile))
            {
                foreach (var zipEntry in zip.Entries)
                {
                    string fileName;
                    string targetPath;
                    if (zipEntry.FileName.StartsWith("Views/"))
                    {
                        fileName   = zipEntry.FileName.Replace("Views/", "");
                        targetPath = Path.Combine(viewsDir, fileName);
                    }
                    else if (zipEntry.FileName.StartsWith("Content/"))
                    {
                        fileName   = zipEntry.FileName.Replace("Content/", "");
                        targetPath = Path.Combine(contentDir, fileName);
                    }
                    else
                    {
                        continue;
                    }
                    if (string.IsNullOrWhiteSpace(fileName))
                    {
                        continue;
                    }
                    if (zipEntry.IsDirectory)
                    {
                        if (!Directory.Exists(targetPath))
                        {
                            Directory.CreateDirectory(targetPath);
                        }
                    }
                    else
                    {
                        using (var outfile = System.IO.File.OpenWrite(targetPath))
                        {
                            zipEntry.Extract(outfile);
                        }
                    }
                }
            }

            try
            {
                var thumbUrl = Url.Content("~/Content/Themes/" + name + "/screenshot.png");
                ScreenshotHelper.GenerateScreenshot(
                    Url.Action("Index", "Home", new { area = "" }, Request.Url.Scheme) + "?theme=" + name,
                    Server.MapPath(thumbUrl));
            }
            catch
            {
                // Unable to generate screenshot. No big deal
            }

            refreshBundles();

            return(RedirectToAction("Index").WithSuccess("The template has been uploaded successfully".TA()));
        }