Exemple #1
0
        static bool Run(Args args)
        {
            CookieContainer cookBox = new CookieContainer();

            if (args.IgnoreCertificateErrors)
            {
                ServicePointManager.ServerCertificateValidationCallback =
                    delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                    {
                        return true;
                    };
            }

            string requestUri = string.Format("{0}/servlets/ProjectDocumentAdd?folderID={1}", args.Site, args.Folder);
            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(requestUri);
            wr.UserAgent = UserAgentName;
            wr.CookieContainer = cookBox;
            string responseUri;
            string text;

            using (WebResponse response = wr.GetResponse())
            {
                responseUri = response.ResponseUri.ToString();
                text = GetText(response);
            }

            MaybeLogin(args, cookBox, requestUri, responseUri, ref text);

            // Ok, we are logged in; let's upload the files

            if (!string.IsNullOrEmpty(args.Result) && File.Exists(args.Result))
                File.Delete(args.Result);

            foreach (string file in args.Files)
            {
                Console.WriteLine("Uploading {0}", Path.GetFileName(file));
                wr = (HttpWebRequest)WebRequest.Create(requestUri + "&action=Add%20document");
                wr.UserAgent = UserAgentName;
                wr.CookieContainer = cookBox;
                wr.Timeout = 1800 * 1000; // Half an hour should be enough
                using (WebFormWriter wfw = new WebFormWriter(wr, WebRequestPostDataEncoding.MultipartFormData, "POST"))
                {
                    wfw.AddValue("name", args.Name ?? Path.GetFileName(file));
                    wfw.AddValue("status", args.Status ?? "Draft");
                    wfw.AddValue("description", args.Description ?? "Description");
                    wfw.AddValue("initiallylocked", "");
                    wfw.AddValue("type", "file");
                    wfw.AddValue("textFormat", "pre");
                    wfw.AddFile("file", file);
                    wfw.AddValue("docUrl", "");
                    wfw.AddValue("submit", "submit");
                    wfw.AddValue("maxDepth", "");
                }

                using (WebResponse response = wr.GetResponse())
                {
                    text = GetText(response);

                    if (text.Contains("\"error"))
                    {
                        Console.Error.WriteLine("Upload failed");
                        return false;
                    }

                    // This page is currently not valid Xml; use regular expressions instead
                    Regex re = new Regex("\\<a[^>]*href=\"(?<url>[^\"]*" + Regex.Escape(Path.GetFileName(file)) + ")\"[^>]*\\>"
                        + "\\s*" + Regex.Escape(args.Name ?? Path.GetFileName(file)) + "\\s*</a>");

                    Match m = re.Match(text);

                    if (m.Success)
                    {
                        if (!string.IsNullOrEmpty(args.Result))
                            File.AppendAllText(args.Result, new Uri(new Uri(requestUri), m.Groups["url"].Value).AbsoluteUri + Environment.NewLine);
                    }
                    else
                    {
                        Console.Error.WriteLine("Failed to retrieve url from upload output");
                        Console.Error.WriteLine();
                        Console.Error.WriteLine("=====================");
                        Console.Error.WriteLine(text);
                        Console.Error.WriteLine("=====================");
                    }
                }
            }

            if (args.List)
            {
                foreach(Document doc in GetDocumentList(args, cookBox))
                {
                    Console.WriteLine(doc.DownloadUri.AbsoluteUri);
                }
            }

            if (args.MaxUploads > 0 || args.Keep != TimeSpan.Zero)
            {
                List<Document> docs = GetDocumentList(args, cookBox);

                RemoveOnStatus(args, docs); // Filter all not interesting documents (status, locks)
                SortDocs(docs);

                if (args.MaxUploads > 0 && docs.Count > args.MaxUploads)
                {
                    int from = docs.Count - args.MaxUploads;
                    docs.RemoveRange(from, docs.Count - from);
                }

                if (args.Keep > TimeSpan.Zero)
                {
                    FilterOnDate(docs, DateTime.Now.Date - args.Keep);
                }

                GC.KeepAlive(docs);
                foreach (Document doc in docs)
                {
                    Console.WriteLine("Deleting {0}", doc.Name);

                    DeleteFile(args, cookBox, doc.FileId);
                }
            }
            return true;
        }