コード例 #1
0
        public override async Task <string> DoBeforePublishUploadWork(IFileUploadContext uploadContext)
        {
            const int REQUEST_COUNT = 2;

            // get as many challenge tokens as we'll need (one for each authenticated request)
            FotobilderRequestManager frm = new FotobilderRequestManager(Username, Password);
            var doc = await frm.PerformGet("GetChallenges", null,
                                           "GetChallenges.Qty", REQUEST_COUNT.ToString(CultureInfo.InvariantCulture));

            var challengeNodes = doc.SelectNodes(@"/FBResponse/GetChallengesResponse/Challenge");
            //Debug.Assert(challengeNodes.Count == REQUEST_COUNT);
            Stack challenges = new Stack(challengeNodes.Count);

            foreach (var node in challengeNodes)
            {
                challenges.Push(node.InnerText);
            }

            // login
            long bytesAvailable = long.MaxValue;

            doc = await frm.PerformGet("Login", (string)challenges.Pop(),
                                       "Login.ClientVersion", ApplicationEnvironment.UserAgent);

            var remainingQuotaNode = doc.SelectSingleNode("/FBResponse/LoginResponse/Quota/Remaining");

            if (remainingQuotaNode != null)
            {
                bytesAvailable = long.Parse(remainingQuotaNode.InnerText, CultureInfo.InvariantCulture);
            }

            // upload picture
            using (Stream fileContents = uploadContext.GetContents())
            {
                doc = await frm.PerformPut("UploadPic", (string)challenges.Pop(), fileContents,
                                           "UploadPic.PicSec", "255",
                                           "UploadPic.Meta.Filename", uploadContext.FormatFileName(uploadContext.PreferredFileName),
                                           "UploadPic.Gallery._size", "1",
                                           "UploadPic.Gallery.0.GalName", ApplicationEnvironment.ProductName,
                                           "UploadPic.Gallery.0.GalSec", "255"); // GalSec 0 no longer supported, using 255
            }

            var picUrlNode = doc.SelectSingleNode("/FBResponse/UploadPicResponse/URL");

            if (picUrlNode != null)
            {
                return(picUrlNode.InnerText);
            }
            else
            {
                throw new BlogClientInvalidServerResponseException("LiveJournal.UploadPic", "No URL returned from server", doc.GetXml());
            }
        }
コード例 #2
0
        public override string DoBeforePublishUploadWork(IFileUploadContext uploadContext)
        {
            const int REQUEST_COUNT = 2;

            // get as many challenge tokens as we'll need (one for each authenticated request)
            FotobilderRequestManager frm = new FotobilderRequestManager(Username, Password);
            XmlDocument doc = frm.PerformGet("GetChallenges", null,
                "GetChallenges.Qty", REQUEST_COUNT.ToString(CultureInfo.InvariantCulture));
            XmlNodeList challengeNodes = doc.SelectNodes(@"/FBResponse/GetChallengesResponse/Challenge");
            Trace.Assert(challengeNodes.Count == REQUEST_COUNT);
            Stack challenges = new Stack(challengeNodes.Count);
            foreach (XmlNode node in challengeNodes)
                challenges.Push(node.InnerText);

            // login
            long bytesAvailable = long.MaxValue;
            doc = frm.PerformGet("Login", (string)challenges.Pop(),
                "Login.ClientVersion", ApplicationEnvironment.UserAgent);
            XmlNode remainingQuotaNode = doc.SelectSingleNode("/FBResponse/LoginResponse/Quota/Remaining");
            if (remainingQuotaNode != null)
                bytesAvailable = long.Parse(remainingQuotaNode.InnerText, CultureInfo.InvariantCulture);

            // upload picture
            using (Stream fileContents = uploadContext.GetContents())
            {
                doc = frm.PerformPut("UploadPic", (string)challenges.Pop(), fileContents,
                                     "UploadPic.PicSec", "255",
                                     "UploadPic.Meta.Filename", uploadContext.FormatFileName(uploadContext.PreferredFileName),
                                     "UploadPic.Gallery._size", "1",
                                     "UploadPic.Gallery.0.GalName", ApplicationEnvironment.ProductName,
                                     "UploadPic.Gallery.0.GalSec", "255");
            }

            XmlNode picUrlNode = doc.SelectSingleNode("/FBResponse/UploadPicResponse/URL");
            if (picUrlNode != null)
            {
                return picUrlNode.InnerText;
            }
            else
            {
                throw new BlogClientInvalidServerResponseException("LiveJournal.UploadPic", "No URL returned from server", doc.OuterXml);
            }
        }