Beispiel #1
0
        private string BuildDirHTML(Dir422 Directory)
        {
            var html = new System.Text.StringBuilder("<html>");

            html.Append("<h1>Folders</h1>");

            // PERCENT ENCODE HERE
            //get rid of ( # reserved html key)!!!!!!
            foreach (Dir422 Dir in Directory.GetDirs())
            {
                html.AppendFormat(
                    "<a href=\"{0}\">{1}</a><br>", Utility.AbsolutePath(Dir), Dir.Name
                    );
            }

            html.Append("<h1>Files</h1>");

            foreach (File422 file in Directory.GetFiles())
            {
                html.AppendFormat(
                    "<a href=\"{0}\">{1}</a><br>", Utility.AbsolutePath(file.Parent) + "/" + file.Name, file.Name
                    );
            }

            html.Append("<html>");
            return(html.ToString());
        }
Beispiel #2
0
        public virtual bool ContainsDir(string dirName, bool recursive)
        {
            Dir422 directory = GetDir(dirName);

            if (directory != null)
            {
                return(true);
            }

            if (!recursive)
            {
                return(false);
            }

            IList <Dir422> directories = GetDirs();

            for (int i = 0; i < directories.Count(); i++)
            {
                Dir422 dir = directories [i];

                if (dir.ContainsDir(dirName, true))
                {
                    return(true);
                }
            }

            return(false);
        }
        public void g_RejectDir()
        {
            Dir422 BlahDir = root.CreateDir("/BlahDir/");

            Assert.AreEqual(null, BlahDir);

            Dir422 BlahDir2 = root.CreateDir("BlahDir2\\");

            Assert.AreEqual(null, BlahDir2);

            bool test = root.ContainsDir("Blah/Dir/", true);

            Assert.AreEqual(false, test);

            bool test2 = root.ContainsDir("BlahDir\\", true);

            Assert.AreEqual(false, test2);

            Dir422 testDir = root.GetDir("BlahDir/");

            Assert.AreEqual(null, testDir);

            Dir422 testDir2 = root.GetDir("Blah\\Dir");

            Assert.AreEqual(null, testDir2);
        }
Beispiel #4
0
        // Check if specified file is within this directory.
        // Allows recursive checks.
        public override bool ContainsFile(string fileName, bool recursive)
        {
            File422 file = GetFile(fileName);

            if (file != null)
            {
                return(true);
            }

            if (!recursive)
            {
                return(false);
            }

            IList <Dir422> directories = GetDirs();

            for (int i = 0; i < directories.Count(); i++)
            {
                Dir422 dir = directories [i];

                if (dir.ContainsFile(fileName, true))
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #5
0
        public void MemFSDirCreateDirDupeTest()
        {
            Dir422 three     = root.CreateDir("three");
            Dir422 threeDupe = root.CreateDir("three"); //duplicate, since we already created dir

            Assert.AreSame(three, threeDupe);
        }
Beispiel #6
0
        //used to build the files html
        private string BuildFileHTML(File422 file)
        {
            StringBuilder html = new StringBuilder();
            string        link = file.Name;

            //recurse through all parent directories until roo
            //also, all spaces are replaced with %20

            Dir422 parent = file.Parent;

            //since the root has a null parent, we iterate until we are there
            //once we are, we append the /files/ HTML request
            while (parent.Name != r_sys.GetRoot().Name)
            {
                link   = parent.Name + "/" + link;
                parent = parent.Parent;
            }

            link = "/files/" + link;


            html.AppendFormat(
                "<a href=\"{0}\">{1}</a><br>",
                link,
                file.Name
                );

            //get HREF for File422 object:
            //last part FILE422.Name
            //recurse through parent directories until hitting root
            //for each one, append directory name to FRONT of string

            return(html.ToString());
        }
Beispiel #7
0
        public void a_CreateNewStandardFileSystem()
        {
            sfs  = StandardFileSystem.Create(rootDir);
            root = sfs.GetRoot();

            Assert.AreEqual("Test", root.Name);
        }
Beispiel #8
0
        public override IList <Dir422> GetDirs()
        {
            Console.WriteLine("GetDirs():");
            List <string> dirNames = Directory.EnumerateDirectories(_path).ToList <string>();
            List <Dir422> dirList  = new List <Dir422>();

            int i = 0;

            foreach (string dir in dirNames)
            {
                string fileName = GetFileNameFromFullPath(dir);
                if (_dirs.Contains(GetDir(dir)))
                { /* do nothing */
                }
                else
                {
                    //Add dir to list
                    //Console.WriteLine("Adding " + fileName);
                    Dir422 newd = GetDir(fileName);
                    //Console.WriteLine("GetDir returned : " + newd);
                    dirList.Add(newd);
                    //dirList.Add(GetDir(dir));
                    i++;
                }
            }

            i--;
            //Console.WriteLine("added " + i + " dirs to list");
            //Console.WriteLine("dirList.Count = "+ dirList.Count);

            _dirs = dirList;
            return(dirList);
        }
Beispiel #9
0
        public MemFSDir(string name, Dir422 parent = null)
        {
            _parent = parent;
            _name   = name;

            Initialize();
        }
Beispiel #10
0
        static int Main(string[] args)
        {
            StandardFileSystem sfs2 = StandardFileSystem.Create("/Users/drewm");

            Dir422 dir1 = sfs2.GetRoot().GetDir("Desktop");

            //Assert.NotNull(dir1.CreateFile("hello.txt"));
            //Assert.NotNull(dir1.CreateFile("Test.txt"));

            dir1.CreateFile("A");
            string datastring = "new data";

            byte[] info = new UTF8Encoding(true).GetBytes(datastring);

            File422 a = dir1.GetFile("A");

            if (a is StdFSFile)
            {
                a = a as StdFSFile;

                FileStream fs = (FileStream)((StdFSFile)a).OpenReadWrite();
                fs.Write(info, 0, info.Length);
                fs.Close();
            }

            return(0);
        }
Beispiel #11
0
 public MemFSDir(string Name)
 {
     dirs   = new List <Dir422> ();
     files  = new List <File422> ();
     name   = Name;
     parent = null;
 }
Beispiel #12
0
 public MemFSDir(Dir422 Parent, string Name)
 {
     dirs   = new List <Dir422> ();
     files  = new List <File422> ();
     parent = Parent;
     name   = Name;
 }
Beispiel #13
0
        public void MReadWriteFile()
        {
            MemoryFileSystem mfs   = new MemoryFileSystem();
            Dir422           dir1  = mfs.GetRoot().CreateDir("Dir1");
            File422          file1 = dir1.CreateFile("File1");

            string datastring = "new data";

            byte[] info = new UTF8Encoding(true).GetBytes(datastring);

            //Assert.NotNull(file1._stream);
            Stream open = file1.OpenReadWrite();

            Assert.True(true);
            Assert.NotNull(open);

            open.Write(info, 0, info.Length);

            open.Close();

            Stream read = file1.OpenReadWrite();

            byte[] readIn = new byte[256];
            read.Read(readIn, 0, 256);

            string str = System.Text.Encoding.Default.GetString(readIn).TrimEnd('\0');

            StringAssert.AreEqualIgnoringCase(str, datastring);
        }
Beispiel #14
0
        public void SDirTests()
        {
            StandardFileSystem sfs2 = StandardFileSystem.Create("/Users/drewm");

            Dir422 dir1 = sfs2.GetRoot().GetDir("Desktop");

            //Assert.NotNull(dir1.CreateFile("hello.txt"));
            //Assert.NotNull(dir1.CreateFile("Test.txt"));

            Assert.NotNull(dir1.CreateFile("A"));
            string datastring = "new data";

            byte[] info = new UTF8Encoding(true).GetBytes(datastring);

            File422 a = dir1.GetFile("A");

            if (a is StdFSFile)
            {
                Assert.NotNull(a);
                a = a as StdFSFile;

                FileStream fs = (FileStream)((StdFSFile)a).OpenReadWrite();
                fs.Write(info, 0, info.Length);
                fs.Close();
            }

            else
            {
                Assert.IsTrue(false);
            }
        }
        public void HandlePut(WebRequest req, Dir422 dir)
        {
            File422 file         = dir.CreateFile(req.URI.Split('/', '\\').Last());
            Stream  outputStream = file.OpenReadWrite();
            Stream  inputStream  = req._networkStream;

            inputStream.ReadTimeout = 1000;
            int BUFFER_SIZE = 8000;
            int bytesRead   = 0;

            byte[] bytes = new byte[BUFFER_SIZE];
            try
            {
                bytesRead = inputStream.Read(bytes, 0, bytes.Length);
            }
            catch { }

            while (bytesRead != 0)
            {
                // Translate data bytes to a ASCII string.
                outputStream.Write(bytes, 0, bytesRead);
                bytes = new byte[BUFFER_SIZE];
                try { bytesRead = inputStream.Read(bytes, 0, bytes.Length); }
                catch { break; }
                Console.WriteLine(bytesRead.ToString());
            }

            req.WriteHTMLResponse("Hello");
            outputStream.Close();
            inputStream.Close();
        }
Beispiel #16
0
        string BuildDirHTML(Dir422 dir)
        {
            _cwd = new StringBuilder();
            // Generate Html
            var html = new StringBuilder("<html> <h1>Folders</h1>");

            // Append dirs
            foreach (Dir422 d in dir.GetDirs())
            {
                // Construct cwd href as we go
                string cwd = ConstructCWD(dir);

                Console.WriteLine("cwd: " + cwd.ToString());
                Console.WriteLine("d.name: " + d.Name);
                html.AppendFormat("<a href=\"{0}/{1}\">{1}</a> <br>", cwd, d.Name);
            }

            html.AppendLine("<h1>Files</h1>");

            foreach (File422 file in dir.GetFiles())
            {
                string cwd = ConstructCWD(file);

                Console.WriteLine("cwd: " + cwd.ToString());
                Console.WriteLine("file.name: " + file.Name);
                html.AppendFormat("<a href=\"{0}/{1}\">{1}</a> <br>", cwd, file.Name);
            }

            html.AppendLine("</html>");
            return(html.ToString());
        }
Beispiel #17
0
        public void StdFSDirParentTest()
        {
            Dir422 two = root.GetDir("one");
            Dir422 a   = two.GetDir("a");

            Assert.AreSame(two, a.Parent);
        }
Beispiel #18
0
        public static string Make(Dir422 dir, string name)
        {
            if (dir == null)
            {
                return("");
            }

            Stack <string> builder = new Stack <string>();

            while (dir != null)
            {
                builder.Push(dir.Name);

                dir = dir.Parent;
            }

            string path = "";

            while (builder.Count > 0)
            {
                path += builder.Pop() + "/";
            }
            path += name;

            return(path);
        }
Beispiel #19
0
        string BuildDirHTML(Dir422 directory)
        {
            var root  = directory.Name + "/";
            var files = directory.GetFiles();
            var dirs  = directory.GetDirs();

            Console.WriteLine("inside build {0} dirs, {1} files", dirs.Count, files.Count);

            var dirStr = "";
            var path   = getPath(directory);

            Console.WriteLine("-------------------------");
            Console.WriteLine(path);

            foreach (var dir in dirs)
            {
                dirStr += String.Format(RESPONSE_ENTRY_FORMAT, path + dir.Name, dir.Name);
            }
            var fileStr = "";

            foreach (var file in files)
            {
                fileStr += String.Format(RESPONSE_ENTRY_FORMAT, path + file.Name, file.Name);
            }

            return(String.Format(RESPONSE_FORMAT, dirStr, fileStr));
        }
Beispiel #20
0
        public MemFSFile(string name, Dir422 parent)
        {
            _name   = name;
            _parent = parent;

            Initialize();
        }
Beispiel #21
0
        //parent cannot be null
        public StdFSFile(string path, Dir422 parent)
        {
            _path   = path;
            _parent = parent;

            _name = StdFSHelper.getBaseFromPath(path);
        }
Beispiel #22
0
        // Methods
        public override void Handler(WebRequest request)
        {
            Console.WriteLine("Service Handler: OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
            _request = request;

            // Parse the URI and determine which of the 3 is the case:
            string[]      parsedURI = request.URI.Split('/');
            List <string> dirNames  = new List <string>();
            int           count     = 0;

            foreach (string s in parsedURI)
            {
                if (count > 1)// so we only have traversable dirnames
                {
                    Console.WriteLine("adding: " + parsedURI[count]);
                    dirNames.Add(parsedURI[count]);
                }
                Console.WriteLine("s: " + s);
                Console.WriteLine("c: " + count);
                count++;
            }
            Console.WriteLine("dirNames.Count: " + dirNames.Count);

            Dir422  currentDir = _fs.GetRoot();
            Dir422  lastDir;
            File422 file = null;

            foreach (string s in dirNames)
            {
                if (!String.IsNullOrEmpty(s))
                {
                    Console.WriteLine("locating: " + s);
                    lastDir    = currentDir;
                    currentDir = currentDir.GetDir(s);
                    if (null == currentDir) // check to see if a file
                    {
                        Console.WriteLine("Null Dir");
                        file = lastDir.GetFile(s);
                        if (null == file)
                        {
                            Console.WriteLine("Null File");
                            //requested Resource does not exist
                            // so 404 write to network and return
                            request.WriteNotFoundResponse("Could not find file: " + s + ".");
                            return;
                        }
                        // otherwise write file contents as html
                        WriteFileContents(file);
                        return;
                    }
                }
            }

            // If this point is reached then we should have a dir and
            // we must write its file listing to the network
            WriteDirListing(currentDir);

            // Provide support for partial content responses
            // (i.e. support the Range header)
        }
Beispiel #23
0
        public void WriteDirListing(Dir422 dir)
        {
            Console.WriteLine("WriteDirListing():XXXXXXXXXXXXXXXXXXXXXXXXXXXX ");
            string html = BuildDirHTML(dir);

            _request.WriteHTMLResponse(html);
        }
Beispiel #24
0
        private bool ContainsFileRecursive(string fileName, Dir422 dir)
        {
            IList <File422> files = dir.GetFiles();

            foreach (File422 file in files)
            {
                if (file.Name == fileName)
                {
                    return(true);
                }
            }

            // Get the current dir's subdirs
            IList <Dir422> subDirs = dir.GetDirs();

            //Here we have looked at all the files in our directory and did nt
            // find our filename, now we need to look in a subdirectory
            foreach (Dir422 subDir in subDirs)
            {
                if (ContainsFileRecursive(fileName, subDir))
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #25
0
        public void StandardFileSystemContainsFileTest()
        {
            Dir422  c   = root.GetDir("two").GetDir("a").GetDir("b").GetDir("c");
            File422 txt = c.CreateFile("dummy.txt");

            Assert.True(stdFS.Contains(txt));
        }
Beispiel #26
0
        public static string Make(Dir422 dir)
        {
            if (null == dir)
            {
                return("");
            }

            Stack <string> builder = new Stack <string>();

            while (dir != null)
            {
                builder.Push(dir.Name);

                dir = dir.Parent;
            }

            string path = "";

            while (builder.Count > 0)
            {
                path += builder.Pop() + "/";
            }

            return(path.Substring(0, path.Length - 1));            // Get rid of last '/'
        }
        private void RespondWithList(Dir422 dir, WebRequest req)
        {
            string html = BuildDirHTML(dir);

            html += "</html>";
            req.WriteHTMLResponse(html);
        }
Beispiel #28
0
        public override void Handler(WebRequest req)
        {
            if (!req.URI.StartsWith(this.ServiceURI))
            {
                throw new InvalidOperationException();
            }

            // Percent-decode our filename
            string percenDecoded = Uri.UnescapeDataString(req.URI);

            //If we want the root folder
            if (percenDecoded == ServiceURI)
            {
                RespondWithList(r_sys.GetRoot(), req);
                return;
            }

            // The pluse one is the '/' after the '/files' so we remove '/files/'
            string[] peices = percenDecoded.Substring(ServiceURI.Length + 1).Split('/');

            Dir422 dir = r_sys.GetRoot();

            for (int i = 0; i < peices.Length - 1; i++)
            {
                dir = dir.GetDir(peices[i]);

                if (dir == null)
                {
                    //This wants a response?
                    req.WriteNotFoundResponse(string.Empty);
                    return;
                }
            }

            string lastPeice = peices [peices.Length - 1];


            File422 file = dir.GetFile(lastPeice);

            if (file != null)
            {
                //Send back the file to them.
                RespondWithFile(file, req);
            }
            else
            {
                //Send the dir contents (if it is a dir to send back)
                dir = dir.GetDir(lastPeice);
                if (dir != null)
                {
                    //Respond with the list of files and dirs
                    RespondWithList(dir, req);
                }
                else
                {
                    req.WriteNotFoundResponse(string.Empty);
                }
            }
        }
Beispiel #29
0
        string BuildDirHTML(Dir422 directory)
        {
            StringBuilder builder = new StringBuilder();

            // Styling
            builder.Append("<html>");
            builder.Append("<head><style>");

            // It's gotta look stylish right?
            builder.Append(" tr:nth-child(even) {background-color: #f2f2f2} ");
            builder.Append(" table { text-align : center; border: 2px solid black } ");
            builder.Append(" .center { position: absolute; width : 75%; left: 12.5% } ");
            builder.Append(" h1 { font-size: 4em } ");
            builder.Append(" a { font-size: 2em } ");
            builder.Append("</style>");
            builder.Append("</head>");
            builder.Append("<body style='text-align:center'><div class='center'>");
            builder.Append("<h1>Directories</h1>");

            // Get all directories
            builder.Append("<table width='100%'>");
            foreach (var dir in directory.GetDirs())
            {
                // Need to get rid of '**/{current_dir}/' in uri
                string uri = ServiceURI + "/" + FullPath.Make(directory, Uri.EscapeDataString(dir.Name)).Replace(fileSystem.GetRoot().Name + "/", "");

                builder.Append("<tr><td><a href='" + uri + "'>" + dir.Name + "</a></td></tr>");
            }
            builder.Append("</table>");

            builder.Append("<h1>Files</h1>");
            // Get all files
            builder.Append("<table width='100%'>");


            foreach (var file in directory.GetFiles())
            {
                // Need to get rid of '**/{current_dir}/' in uri
                string uri = ServiceURI + "/" + FullPath.Make(directory, Uri.EscapeDataString(file.Name)).Replace(fileSystem.GetRoot().Name + "/", "");

                builder.Append("<tr><td><a href='" + uri + "'>" + file.Name + "</a></td></tr>");
            }
            builder.Append("</table>");

            // Add the abillity to upload files
            if (allowUploads)
            {
                builder.Append(SCRIPT);
                builder.AppendFormat(
                    "<hr><h3 id='uploadHdr'>Upload</h3><br>" +
                    "<input id=\"uploader\" type='file' " +
                    "onchange='selectedFileChanged(this,\"{0}\")' /><hr>",
                    GetHREF(directory, true));
            }

            builder.Append("</div></body></html>");

            return(builder.ToString());
        }
Beispiel #30
0
        public override IList <Dir422> GetDirs()
        {
            Dir422[] dirs = new Dir422[directories.Values.Count];

            directories.Values.CopyTo(dirs, 0);

            return(dirs);
        }