private void DownloadFile(System.Web.HttpContext context, CmsContent content, String filename)
        {
            CmsContentField mimeTypeField = content.FindField("mimetype");
            string mimeType = "application/octet-stream";
            if (mimeTypeField != null)
                mimeType = mimeTypeField.Value;

            Data.Guid siteGuid = CurrentSite.Guid;
            ContentFileUploadImpl filehandler = new ContentFileUploadImpl();
            StorageFile fileinfo = filehandler.GetInfo(siteGuid, filename);

            context.Response.Clear();
            context.Response.ClearHeaders();
            context.Response.ClearContent();
            context.Response.ContentType = mimeType;
            context.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
            context.Response.AppendHeader("Content-Length", fileinfo.Size.ToString());
            context.Response.AppendHeader("Content-Transfer-Encoding", "binary");
            context.Response.Expires = 60;
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(0));
            filehandler.Read(siteGuid,filename,context.Response.OutputStream);

            context.Response.Flush();
            context.Response.Close();
            context.Response.End();
        }
Esempio n. 2
0
        private void PopulateFields(Data.Guid siteGuid, Control parent, CmsContent item, String oldFilename)
        {
            if (item.ContentType.IsFileType)
            {
                FileUpload upload = (FileUpload)ControlHelper.FindRecursive(parent, "fileupload");
                if ((!upload.HasFile) && (oldFilename == null))
                    throw new ArgumentException("This content type requires that a file be uploaded");

                if (upload.HasFile)
                {
                    byte[] data = upload.FileBytes;
                    String filename = (oldFilename == null) ? upload.FileName : oldFilename;
                    String mimeType = upload.PostedFile.ContentType;

                    Boolean overwrite = false;
                    if (!String.IsNullOrWhiteSpace(oldFilename))
                        overwrite = true;

                    if (!ContentFileUploadImpl.IsValidFileType(filename))
                        throw new ArgumentException("The specified filetype is not currently supported by the CMS. Valid file types are:" + ContentFileUploadImpl.ValidExtensionsString);

                    ContentFileUploadImpl handler = new ContentFileUploadImpl();
                    handler.Save(siteGuid,data, filename, overwrite);

                    CmsContentField filenameField = new CmsContentField();
                    CmsContentField mimeTypeField = new CmsContentField();

                    filenameField.Name = "filename";
                    filenameField.ObjectType = "System.String";
                    filenameField.Value = filename;
                    filenameField.Parent = item;

                    mimeTypeField.Name = "mimetype";
                    mimeTypeField.ObjectType = "System.String";
                    mimeTypeField.Value = mimeType;
                    mimeTypeField.Parent = item;

                    item.AddField(filenameField);
                    item.AddField(mimeTypeField);
                }
            }

            //Loop through each expected id and build up the CMS content item
            IList<CmsContentTypeField> typeFields = ContentManager.Instance.GetContentTypeFields(item.ContentType.Guid);
            foreach (CmsContentTypeField typeField in typeFields)
            {
                String id = ContentWebControlManager.GetControlId(typeField);
                String[] parts = id.Split('_');
                String objectType = parts[1];
                String propertyName = parts[2];

                String result = null;
                Control control = ControlHelper.FindRecursive(parent, id);
                switch (objectType.ToLower())
                {
                    case CmsContentTypeField.Textbox:
                    case CmsContentTypeField.Textarea:
                        result = ContentWebControlManager.GetTextboxValue(control);
                        break;
                    case CmsContentTypeField.Datetime:
                        result = ContentWebControlManager.GetDateTimeValue(control);
                        break;
                    case CmsContentTypeField.Dropdown:
                        result = ContentWebControlManager.GetDropdownValue(control);
                        break;
                }

                CmsContentField field = new CmsContentField();
                field.Name = typeField.SystemName;
                field.ObjectType = typeField.ObjectType;
                field.Value = result;

                field.Parent = item;
                item.AddField(field);
            }
        }
Esempio n. 3
0
        public List<String> Import(ImportSiteMessage message)
        {
            Data.Hash importHash = Data.Hash.New(message.ImportHash);
            Data.Guid subscriptionId = Data.Guid.New(message.SubscriptionId);
            Boolean deleteExisting = message.DeleteExisting;

            List<String> status = new List<String>();

            byte[] data;

            AddStatus(importHash, status, "Site import started at " + UtcDateTime.Now.ToString());
            CmsSubscription subscription = SubscriptionManager.GetSubscription(subscriptionId);
            CmsTheme defaultTheme = ThemeManager.Instance.GetDefaultBySite(subscriptionId);

            //Check if the import-template already exists
            CmsTemplate template = TemplateManager.Instance.GetTemplate(subscriptionId, "import-template");
            if (template == null)
            {
                template = new CmsTemplate();
                template.Content = "{content}";
                template.Name = "import-template";
                template.IsGlobalTemplateType = false;
                template.LastSaved = UtcDateTime.Now;
                template.SubscriptionGuid = subscriptionId.Value;
                template.Theme = defaultTheme;

                TemplateManager.Instance.Save(template);
                AddStatus(importHash, status, "Successfully created and associated import template to deafult theme");
            }

            IDictionary<ImportType, IList<ImportedItem>> items = this.GetImportedItems(importHash);

            //Check if we need to delete the existing site
            if (deleteExisting)
            {
                //Erase all of the existing data
                SubscriptionManager.Erase(subscription.Guid, false, false);
                SessionProvider.Instance.Close();
                SessionProvider.Instance.GetOpenSession();

                //Setup the default
                SiteHelper.Configure(subscription.Guid);
            }

            //First, import all of the images
            IList<ImportedItem> images = items[ImportType.Image];
            foreach (ImportedItem image in images)
            {
                CmsUrl uri = new CmsUrl(image.Uri);
                data = SimpleWebClient.GetResponse(uri.ToUri());

                ImageManager.Instance.AddImage(subscriptionId, StorageClientConst.RootFolder, uri.Path, image.ContentType, data);
                AddStatus(importHash, status, "Successfully imported image: " + uri.ToString() + " (" + image.ContentType + ")");
            }

            //Create the sitemap and then add the page itself
            CmsSitePath root = CmsSiteMap.Instance.GetPath(subscriptionId, CmsSiteMap.RootPath);

            Dictionary<CmsUrl, int> cssUses = new Dictionary<CmsUrl, int>();
            Dictionary<CmsUrl, int> jsUses = new Dictionary<CmsUrl, int>();

            IList<ImportedItem> pages = NormalizeImport(items[ImportType.Page]);
            foreach (ImportedItem page in pages)
            {
                try
                {
                    CmsUrl uri = new CmsUrl(page.Uri);
                    CmsUrlWalker walker = new CmsUrlWalker(uri);

                    while (walker.Next())
                    {
                        String parent = walker.GetParentPath();
                        String current = walker.GetIndividualPath();
                        String fullpath = CmsSiteMap.PathCombine(parent, current);
                        int depth = walker.Depth;

                        if (!walker.IsLast)
                        {
                            //Check if the current path exists, if not, create it
                            if (!CmsSiteMap.Instance.Exists(subscriptionId, fullpath))
                                CmsSiteMap.Instance.AddChildDirectory(subscriptionId, parent, current);
                        }
                    }

                    String pageName = walker.GetIndividualPath();
                    CmsPage newpage = GetPage(template.Name, subscription.Culture, pageName, message.ReplacePhoneNumbers, page, cssUses, jsUses);
                    newpage.SubscriptionId = subscriptionId.Value;

                    //Add the page to the cms system
                    PageManager.Instance.AddNewPage(walker.GetParentPath(), pageName, newpage);
                    AddStatus(importHash, status, "Successfully imported page " + page.Uri);
                }
                catch (Exception ex)
                {
                    if (!ex.Message.Contains("404"))
                        Logging.Database.Write("import-site-manager", "Failed to import page: " + page.Uri + ", cause:" + ex.Message + ", stack:" + ex.StackTrace);
                    AddStatus(importHash, status, "Failed to import page " + page.Uri + ", Reason:" + ex.Message);
                }
            }

            //Import the css
            IList<ImportedItem> css = items[ImportType.Css];

            int sortOrder = 0;
            CssManager cssManager = new CssManager(null);
            cssManager.SubscriptionId = subscriptionId;
            foreach (ImportedItem item in css)
            {
                CmsUrl uri = new CmsUrl(item.Uri);
                data = SimpleWebClient.GetResponse(uri.ToUri());

                cssManager.Save(defaultTheme.ThemeGuid, uri.Path, data, true, 0);
                cssManager.UpdateSortInfo(defaultTheme, uri.Path, sortOrder++);
                AddStatus(importHash, status, "Successfully imported css file: " + uri.ToString());
            }

            //Import the javascript
            IList<ImportedItem> js = items[ImportType.Javascript];

            sortOrder = 0;
            JavascriptManager jsManager = new JavascriptManager(null);
            jsManager.SubscriptionId = subscriptionId;
            foreach (ImportedItem item in js)
            {
                CmsUrl uri = new CmsUrl(item.Uri);
                data = SimpleWebClient.GetResponse(uri.ToUri());

                jsManager.Save(defaultTheme.ThemeGuid, uri.Path, data, true, 0);
                jsManager.UpdateSortInfo(defaultTheme, uri.Path, sortOrder++);
                AddStatus(importHash, status, "Successfully imported javascript file: " + uri.ToString());
            }

            //Import any documents
            IList<ImportedItem> documents = items[ImportType.Document];
            foreach (ImportedItem item in documents)
            {
                CmsUrl uri = new CmsUrl(item.Uri);

                ContentFileUploadImpl handler = new ContentFileUploadImpl();
                String filename = uri.Path;
                if (ContentFileUploadImpl.IsValidFileType(filename))
                {
                    data = SimpleWebClient.GetResponse(uri.ToUri());

                    handler.Save(subscriptionId, data, uri.Path, true);
                }
            }

            AddStatus(importHash, status, "Site import completed successfully at " + UtcDateTime.Now.ToString());

            ImportedItemDao dao = new ImportedItemDao();
            dao.DeleteAllByImportHash(importHash);

            return status;
        }