/// <summary>
        /// Performs a SaveAs operation of an open document. Called from SaveItem after the running document table has been updated with the new doc data.
        /// </summary>
        /// <param name="docData">A pointer to the document in the rdt</param>
        /// <param name="newFilePath">The new file path to the document</param>
        /// <returns></returns>
        protected override int AfterSaveItemAs(IntPtr docData, string newFilePath)
        {
            if(String.IsNullOrEmpty(newFilePath))
            {
                throw new ArgumentException(SR.GetString(SR.ParameterCannotBeNullOrEmpty, CultureInfo.CurrentUICulture), "newFilePath");
            }

            int returnCode = VSConstants.S_OK;
            newFilePath = newFilePath.Trim();

            //Identify if Path or FileName are the same for old and new file
            string newDirectoryName = Path.GetDirectoryName(newFilePath);
            Uri newDirectoryUri = new Uri(newDirectoryName);
            string newCanonicalDirectoryName = newDirectoryUri.LocalPath;
            newCanonicalDirectoryName = newCanonicalDirectoryName.TrimEnd(Path.DirectorySeparatorChar);
            string oldCanonicalDirectoryName = new Uri(Path.GetDirectoryName(this.GetMkDocument())).LocalPath;
            oldCanonicalDirectoryName = oldCanonicalDirectoryName.TrimEnd(Path.DirectorySeparatorChar);
            string errorMessage = String.Empty;
            bool isSamePath = NativeMethods.IsSamePath(newCanonicalDirectoryName, oldCanonicalDirectoryName);
            bool isSameFile = NativeMethods.IsSamePath(newFilePath, this.Url);

            // Currently we do not support if the new directory is located outside the project cone
            string projectCannonicalDirecoryName = new Uri(this.ProjectMgr.ProjectFolder).LocalPath;
            projectCannonicalDirecoryName = projectCannonicalDirecoryName.TrimEnd(Path.DirectorySeparatorChar);
            if(!isSamePath && newCanonicalDirectoryName.IndexOf(projectCannonicalDirecoryName, StringComparison.OrdinalIgnoreCase) == -1)
            {
                errorMessage = String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.LinkedItemsAreNotSupported, CultureInfo.CurrentUICulture), Path.GetFileNameWithoutExtension(newFilePath));
                throw new InvalidOperationException(errorMessage);
            }

            //Get target container
            HierarchyNode targetContainer = null;
            if(isSamePath)
            {
                targetContainer = this.Parent;
            }
            else if(NativeMethods.IsSamePath(newCanonicalDirectoryName, projectCannonicalDirecoryName))
            {
                //the projectnode is the target container
                targetContainer = this.ProjectMgr;
            }
            else
            {
                //search for the target container among existing child nodes
                targetContainer = this.ProjectMgr.FindChild(newDirectoryName);
                if(targetContainer != null && (targetContainer is FileNode))
                {
                    // We already have a file node with this name in the hierarchy.
                    errorMessage = String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.FileAlreadyExistsAndCannotBeRenamed, CultureInfo.CurrentUICulture), Path.GetFileNameWithoutExtension(newFilePath));
                    throw new InvalidOperationException(errorMessage);
                }
            }

            if(targetContainer == null)
            {
                // Add a chain of subdirectories to the project.
                string relativeUri = PackageUtilities.GetPathDistance(this.ProjectMgr.BaseURI.Uri, newDirectoryUri);
                Debug.Assert(!String.IsNullOrEmpty(relativeUri) && relativeUri != newDirectoryUri.LocalPath, "Could not make pat distance of " + this.ProjectMgr.BaseURI.Uri.LocalPath + " and " + newDirectoryUri);
                targetContainer = this.ProjectMgr.CreateFolderNodes(relativeUri);
            }
            Debug.Assert(targetContainer != null, "We should have found a target node by now");

            //Suspend file changes while we rename the document
            string oldrelPath = this.ItemNode.GetMetadata(ProjectFileConstants.Include);
            string oldName = Path.Combine(this.ProjectMgr.ProjectFolder, oldrelPath);
            SuspendFileChanges sfc = new SuspendFileChanges(this.ProjectMgr.Site, oldName);
            sfc.Suspend();

            try
            {
                // Rename the node.
                DocumentManager.UpdateCaption(this.ProjectMgr.Site, Path.GetFileName(newFilePath), docData);
                // Check if the file name was actually changed.
                // In same cases (e.g. if the item is a file and the user has changed its encoding) this function
                // is called even if there is no real rename.
                if(!isSameFile || (this.Parent.ID != targetContainer.ID))
                {
                    // The path of the file is changed or its parent is changed; in both cases we have
                    // to rename the item.
                    this.RenameFileNode(oldName, newFilePath, targetContainer.ID);
                    OnInvalidateItems(this.Parent);
                }
            }
            catch(Exception e)
            {
                Trace.WriteLine("Exception : " + e.Message);
                this.RecoverFromRenameFailure(newFilePath, oldrelPath);
                throw;
            }
            finally
            {
                sfc.Resume();
            }

            return returnCode;
        }
Ejemplo n.º 2
0
        static List<string> getListing(string url)
        {
            string name = new Uri(url).AbsolutePath;

            name = Path.GetFileName(name.TrimEnd('/'));

            List<string> list = new List<string>();

            if (File.Exists(datadirectory + Path.DirectorySeparatorChar + name))
            {
                using (StreamReader sr = new StreamReader(datadirectory + Path.DirectorySeparatorChar + name))
                {
                    while (!sr.EndOfStream)
                    {
                        list.Add(sr.ReadLine());
                    }

                    sr.Close();
                }
                return list;
            }

            try
            {
                log.Info("srtm req " + url);

                WebRequest req = HttpWebRequest.Create(url);

                using (WebResponse res = req.GetResponse())
                using (StreamReader resstream = new StreamReader(res.GetResponseStream()))
                {

                    string data = resstream.ReadToEnd();

                    Regex regex = new Regex("href=\"([^\"]+)\"", RegexOptions.IgnoreCase);
                    if (regex.IsMatch(data))
                    {
                        MatchCollection matchs = regex.Matches(data);
                        for (int i = 0; i < matchs.Count; i++)
                        {
                            if (matchs[i].Groups[1].Value.ToString().Contains(".."))
                                continue;
                            if (matchs[i].Groups[1].Value.ToString().Contains("http"))
                                continue;
                            if (matchs[i].Groups[1].Value.ToString().EndsWith("/srtm/version2_1/"))
                                continue;

                            list.Add(url.TrimEnd(new char[] {'/', '\\'}) + "/" + matchs[i].Groups[1].Value.ToString());
                        }
                    }
                }

                using (StreamWriter sw = new StreamWriter(datadirectory + Path.DirectorySeparatorChar + name))
                {
                    list.ForEach(x =>
                    {
                        sw.WriteLine((string)x);
                    });

                    sw.Close();
                }
            }
            catch (WebException ex)
            {
                log.Error(ex);
                throw;
            }

            return list;
        }
Ejemplo n.º 3
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string id = context.Request["id"];
            string url = context.Request["url"];
            string action = context.Request["action"];

            if (string.IsNullOrEmpty(url))
            {
                return;
            }

            //url = url.ToLower();

            if (!url.EndsWith("/"))
            {
                url = url + "/";
            }
            
            string examplesFolder = new Uri(HttpContext.Current.Request.Url, "Examples/").AbsolutePath.ToLower();

            if (!url.StartsWith(examplesFolder,true, CultureInfo.InvariantCulture))
            {
                url = examplesFolder.TrimEnd(new []{'/'}) + url;
            }

            string wId = context.Request["wId"];

            HttpRequest r = HttpContext.Current.Request;
            Uri uri = new Uri(r.Url.Scheme + "://" + r.Url.Authority + url, UriKind.Absolute);

            string path = context.Server.MapPath(uri.AbsolutePath);
            DirectoryInfo dir = new DirectoryInfo(path);

            ExampleConfig cfg = null;

            if (File.Exists(dir.FullName + "config.xml"))
            {
                cfg = new ExampleConfig(dir.FullName + "config.xml", false);
            }

            if (action.IsNotEmpty() && false)
            {
                switch (action)
                {
                    case "comments.count" :
                        int count = 0;

                        if (cfg != null)
                        {
                            count = cfg.Comments.Count;
                        }

                        context.Response.Write(count);
                        context.Response.End();
                        return;
                    case "comments.build" :
                        if (cfg != null)
                        {
                            context.Response.Write(JSON.Serialize(cfg.Comments));
                        }
                        
                        context.Response.End();
                        return;
                    case "comments.add" :
                        if (cfg != null)
                        {
                            this.AddComment(context, cfg);
                        }
                        return;
                    case "tags.add":
                        if (cfg != null)
                        {
                            this.AddTag(context, cfg);
                        }
                        return;
                    case "tags.read":
                        if (cfg != null)
                        {
                            context.Response.Write(JSON.Serialize(cfg.Tags.ConvertAll(input => new { Tag = input })));
                        }
                        context.Response.End();
                        return;
                }
            }

            if (id.StartsWith("extnet"))
            {
                id = "e" + Math.Abs(url.ToLower().GetHashCode());
            }

            string tabs = BuildSourceTabs(id, wId, cfg, dir);

            //string script = string.Format("var w = Ext.getCmp({0});w.add({1});w.doLayout();", JSON.Serialize(wId), tabs);
            Ext.Net.Utilities.CompressionUtils.GZipAndSend(tabs);
            //context.Response.Write(tabs);
        }