Beispiel #1
0
        public override File422 CreateFile(string fileName)
        {
            File422 stdFile = null;

            //if at root of filesystem, gets rid of extra '/'
            string path = (_path == "/") ? "" : _path;

            if (!StdFSHelper.ContainsPathCharacters(fileName) ||
                string.IsNullOrEmpty(fileName))
            {
                //if we are at /a/b/, makes /a/b/c.txt
                //where c.txt is fileName
                path = path + "/" + fileName;

                //if already exists clear existing data.
                if (File.Exists(path))
                {
                    File.WriteAllText(path, "");
                }

                //if exists and not read-only contents are overwritten
                using (FileStream fs = File.Create(path))
                {
                    //set length to 0.
                    fs.SetLength(0);

                    //this is the parent
                    stdFile = new StdFSFile(path, this);
                }
            }

            return(stdFile);
        }
Beispiel #2
0
        public override File422 CreateFile(string name)
        {
            StdFSFile newFile = new StdFSFile();

            // validate the name
            if (IsValidName(name))
            {
                // Create file in actual fileSystem
                Console.WriteLine("path: " + _path);
                Console.WriteLine("Creating: " + _path + "/" + name);
                FileStream stream = File.Create(_path + "/" + name);
                stream.Close();
                stream.Dispose();
                Console.WriteLine("made it past close");

                // Create a new stdFile ref
                newFile = new StdFSFile(_path + "/" + name, name, this);
                // Add that to the list of sub dirs for this dir
                _files.Add(newFile);
                return(newFile);
            }

            Console.WriteLine("CreateDir(): Invalid Name");
            return(null);
        }
Beispiel #3
0
        public override File422 GetFile(string name)
        {
            if (name.Contains("/") || name.Contains("\\") || name == "")
            {
                return(null);
            }

            if (ContainsFile(name, false))
            {
                //StdFSFile file = new StdFSFile(m_path + "/" + name, this); //Linux
                StdFSFile file = new StdFSFile(m_path + "\\" + name, this);     //Windows
                return(file);
            }
            else
            {
                return(null);
            }
        }
Beispiel #4
0
        public override File422 CreateFile(string name)
        {
            // Check for invalid characters, same as CreateDir
            // If not, create the file within the DIR and set size to 0
            // If it already exists, truncate it back to size 0, ereasing all content
            if (name.Contains("/") || name.Contains("\\"))
            {
                return(null);
            }

            try
            {
                Stream file = File.Create(m_path + "\\" + name);     //Windows
                //Stream file = File.Create(m_path + "/" + name); //Linux
                file.Close();
                StdFSFile newFile = new StdFSFile(m_path + "\\" + name, this);     //Windows
                //StdFSFile newFile = new StdFSFile(m_path + "/" + name, this); //Linux
                return(newFile);
            }
            catch { return(null); }
        }
Beispiel #5
0
        public override File422 GetFile(string name)
        {
            Console.WriteLine("GetFile()");
            Console.WriteLine("cwd: " + _path);
            Console.WriteLine("name: " + name);
            string filePath = _path + "/" + name;

            Console.WriteLine("Looking for " + filePath);

            if (IsValidName(name))
            {
                if (File.Exists(filePath))
                {
                    // Return file
                    StdFSFile file = new StdFSFile(filePath, name, this);
                    return(file);
                }
                Console.WriteLine("could not find file with name: " + name);
                Console.WriteLine("Searched for: " + filePath);
            }
            return(null);
        }
Beispiel #6
0
        public override void Handler(WebRequest Req)
        {
            Dir422 Root = m_FS.GetRoot();

            //Remove Last / here!!!!
            if (Req.URI.LastIndexOf('/') == Req.URI.Length - 1)
            {
                Req.URI = Req.URI.Substring(0, Req.URI.Length - 1);
            }
            //int x = 0;


            //1. Percent-decode URI.
            // THIS NOT GOOD ENOUGH!!!

            Req.URI = Utility.PercentDecode(Req.URI);
            //string test = Req.URI.Replace ("%20", " ");;

            // Set name of requested file||dir.
            string uriName = Utility.NameFromPath(Req.URI);

            //2. If it refers to file somewhere in the shared folder.
            if (Root.ContainsFile(uriName, true))
            {
                string path = Req.URI.Substring(0, Req.URI.LastIndexOf("/"));

                Dir422     Dir          = Utility.TraverseToDir(Root, path);
                StdFSFile  File         = (StdFSFile)Dir.GetFile(uriName);
                FileStream MyFileStream = (FileStream)File.OpenReadOnly();

                if (Req.headers.ContainsKey("Range"))
                {
                    // process partial response here
                    Console.WriteLine("Process Partial Request");
                    int x = 0;
                }
                else
                {
                    string contentType = Utility.ContentType(uriName);
                    string response    = Utility.BuildFileResponseString(
                        MyFileStream.Length.ToString(), contentType);

                    //if (contentType != "video/mp4")
                    //{
                    byte[] sendResponseString = Encoding.ASCII.GetBytes(response);
                    Req.bodyRequest.Write(sendResponseString, 0, sendResponseString.Length);
                    //}

                    //byte[] sendResponseString = Encoding.ASCII.GetBytes(response);

                    int    read = 0;
                    byte[] send = new byte[7500];

                    while (read < MyFileStream.Length)
                    {
                        read = read + MyFileStream.Read(send, 0, send.Length);
                        Req.bodyRequest.Write(send, 0, send.Length);
                    }
                }
            }

            //3. Else if it refers to a folder somewhere in the shared folder.
            //	 Or is the shared directory
            else if (Root.ContainsDir(uriName, true) || Req.URI == ServiceURI || uriName == "")
            {
                if (Req.URI == ServiceURI || uriName == "")
                {
                    string dirHTMLListing = BuildDirHTML(Root);

                    byte[] sendResponseString = Encoding.ASCII.GetBytes(dirHTMLListing);
                    Req.bodyRequest.Write(sendResponseString, 0, sendResponseString.Length);

                    //Req.WriteHTMLResponse (dirHTMLListing);
                }
                else
                {
                    Dir422 Dir            = Utility.TraverseToDir(Root, Req.URI);
                    string dirHTMLListing = BuildDirHTML(Dir);

                    byte[] sendResponseString = Encoding.ASCII.GetBytes(dirHTMLListing);
                    Req.bodyRequest.Write(sendResponseString, 0, sendResponseString.Length);

                    //Req.WriteHTMLResponse (dirHTMLListing);
                }
            }

            //4.Else it’s a bad URI.
            else
            {
                Req.WriteNotFoundResponse("File or directory not found");
            }
        }