The Netstorage class is the preferred interface for calling libraries indending to leverage the Netstorage API. All of the available actions are innumerated in this library and are responsible for the correct business logic to assemble the request to the API. Some early safetys are added in this library to limit errors. TODO: Add "LIST" support for ObjectStore TODO: Detect FileStore v. ObjectStore TODO: Extract xml response from various requests into standard object representation Author: [email protected] (Colin Bendell)
        public NetStorage SetupNetstorage(out WebHeaderCollection headers, out MemoryStream requestStream)
        {
            string TestURIProtocol = "asdf";
            WebRequest.RegisterPrefix(TestURIProtocol, new WebRequestTestCreate());
            var request = (HttpWebRequestTest)WebRequest.Create("asdf://www.example.com/");
            var response = request.CreateResponse(HttpStatusCode.OK);
            request.NextResponse = response;

            headers = request.Headers;
            requestStream = request.RequestStream;

            NetStorage ns = new NetStorage("www.example.com", "user1", "secret1", false);
            ns.Request = request;

            return ns;
        }
        public void TestNetstorageGetUri()
        {
            var ns = new NetStorage("www.example.com", "user1", "secret1", false);
            Assert.AreEqual(ns.getNetStorageUri("/foobar"), "http://www.example.com/foobar");

            ns = new NetStorage("www.example.com", "user1", "secret1", true);
            Assert.AreEqual(ns.getNetStorageUri("/foobar"), "https://www.example.com/foobar");
        }
예제 #3
0
        static void execute(string action, string user, string key, string netstorageURI,
            string uploadfile, string outputfile, string target, string dst, bool indexZip)
        {
            if (action == null || netstorageURI == null || user == null || key == null)
            {
                help();
                return;
            }

            string[] hostpath = netstorageURI.Split("/".ToCharArray(), 2);
            string host = hostpath[0];
            string path = hostpath[1];
            NetStorage ns = new NetStorage(host, user, key);
            Stream result = null;
            bool success = true;

            switch (action)
            {
                case "delete":
                    success = ns.Delete(path);
                    break;
                case "quickdelete":
                    success = ns.QuickDelete(path);
                    break;
                case "dir":
                    result = ns.Dir(path);
                    break;
                case "download":
                    result = ns.Download(path);
                    break;
                case "du":
                    result = ns.DU(path);
                    break;
                case "mkdir":
                    success = ns.MkDir(path);
                    break;
                case "mtime":
                    success = ns.MTime(path);
                    break;
                case "rename":
                    if (dst == null)
                    {
                        help();
                        return;
                    }
                    success = ns.Rename(path, dst);
                    break;
                case "rmdir":
                    success = ns.RmDir(path);
                    break;
                case "stat":
                    result = ns.Stat(path);
                    break;
                case "symlink":
                    if (target == null)
                    {
                        help();
                        return;
                    }
                    success = ns.Symlink(path, target);
                    break;
                case "upload":
                    if (uploadfile == null)
                    {
                        help();
                        return;
                    }
                    success = ns.Upload(path, new FileInfo(uploadfile), indexZip);
                    break;
                default:
                    help();
                    return;
            }

            if (result != null)
            {
                Stream output = Console.OpenStandardOutput();
                if (outputfile != null)
                    output = new FileInfo(outputfile).OpenWrite();

                using (output)
                {
                    using (result)
                    {
                        byte[] buffer = new byte[32*1024];
                        int bytesRead = 0;

                        while ((bytesRead = result.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            output.Write(buffer, 0, bytesRead);
                        }
                    }
                }
            }
            else if (success)
                Console.Out.WriteLine("Success.");
            else
                Console.Error.WriteLine("Error.");
        }