コード例 #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 void DoAfterPublishUploadWork(IFileUploadContext uploadContext)
        {
            FileAttachSettings attachSettings = new FileAttachSettings(uploadContext.Settings);

            if (attachSettings.AttachmentFileName == null)
            {
                CalculateUploadVariables(uploadContext.FormatFileName(uploadContext.PreferredFileName), attachSettings);
            }

            string listGuid = SharepointBlogIdToListGuid(uploadContext.BlogId);

            if (listGuid != null)
            {
                SharePointListsService listsServicesharePointLists = new SharePointListsService(attachSettings.UploadServiceUrl);
                listsServicesharePointLists.Credentials = GetHttpCredentials();

                //The AddAttachment() call will throw an error if the attachment already exists, so we need to delete
                //the attachment first (if it exists).  To delete the attachment, we must construct the attachment URL
                //that is typically generated internally by the server.
                //Sample URL: http://sharepoint/sites/writer/b2/blog/Lists/Posts/Attachments/13/Sunset_thumb1.jpg
                string attachDeleteUrl = String.Format(CultureInfo.InvariantCulture, "{0}{1}/Lists/Posts/Attachments/{2}/{3}", attachSettings.BaseUrl, attachSettings.BlogUrlPart, uploadContext.PostId, attachSettings.AttachmentFileName);
                try
                {
                    listsServicesharePointLists.DeleteAttachment(listGuid, uploadContext.PostId, attachDeleteUrl);
                }
                catch (Exception) {}

                //Add the attachment
                using (Stream fileContents = uploadContext.GetContents())
                    listsServicesharePointLists.AddAttachment(listGuid, uploadContext.PostId, attachSettings.AttachmentFileName, Convert.ToBase64String(StreamHelper.AsBytes(fileContents)));

                uploadContext.Settings.SetString(uploadContext.PostId, FILE_ALREADY_UPLOADED);

                return;
            }
            throw new BlogClientFileUploadNotSupportedException();
        }
コード例 #3
0
        public override string DoBeforePublishUploadWork(IFileUploadContext uploadContext)
        {
            string uploadFileName = uploadContext.FormatFileName(CleanUploadFilename(uploadContext.PreferredFileName));

            // call the method
            XmlNode result;
            using (Stream fileContents = uploadContext.GetContents())
            {
                result = CallMethod("metaWeblog.newMediaObject",
                                             new XmlRpcString(uploadContext.BlogId),
                                             new XmlRpcString(Username),
                                             new XmlRpcString(Password, true),
                                             new XmlRpcStruct(new XmlRpcMember[]
                                                                   {
                                                                       new XmlRpcMember( "name", uploadFileName ),
                                                                       new XmlRpcMember( "type", MimeHelper.GetContentType(Path.GetExtension(uploadContext.PreferredFileName),MimeHelper.APP_OCTET_STREAM )),
                                                                       new XmlRpcMember( "bits", new XmlRpcBase64( StreamHelper.AsBytes(fileContents) ) ),
                                                                   }
                                                ));
            }

            // return the url to the file on the server
            XmlNode urlNode = result.SelectSingleNode("struct/member[name='url']/value");
            if (urlNode != null)
            {
                return urlNode.InnerText;
            }
            else
            {
                throw new BlogClientInvalidServerResponseException("metaWeblog.newMediaObject", "No URL returned from server", result.OuterXml);
            }
        }
コード例 #4
0
        public async Task <string> DoBeforePublishUploadWork(IFileUploadContext uploadContext)
        {
            string albumName = ApplicationEnvironment.ProductName;

            string fileName   = uploadContext.PreferredFileName;
            var    fileStream = uploadContext.GetContents();

            if (Options.FileUploadNameFormat != null && Options.FileUploadNameFormat.Length > 0)
            {
                string   formattedFileName = uploadContext.FormatFileName(uploadContext.PreferredFileName);
                string[] chunks            = StringHelper.Reverse(formattedFileName).Split(new char[] { '/' }, 2);
                if (chunks.Length == 2)
                {
                    albumName = StringHelper.Reverse(chunks[1]);
                }
            }

            string EDIT_MEDIA_LINK = "EditMediaLink";

            var postImage = new PicasaPostImage();

            //postImage.editUri = uploadContext.Settings.GetString(EDIT_MEDIA_LINK, null);

            if (postImage.editUri == null || postImage.editUri.Length == 0)
            {
                await PostNewImage(albumName, fileStream, fileName, postImage);
            }
            else
            {
                try
                {
                    await UpdateImage(postImage.editUri, fileStream, fileName, postImage);
                }
                catch (Exception e)
                {
                    Debug.Fail(e.ToString());
                    if (e is WebException)
                    {
                        HttpRequestHelper.LogException((WebException)e);
                    }

                    bool success = false;
                    postImage.srcUrl = null; // compiler complains without this line
                    try
                    {
                        // couldn't update existing image? try posting a new one
                        await PostNewImage(albumName, fileStream, fileName, postImage);

                        success = true;
                    }
                    catch
                    {
                    }
                    if (!success)
                    {
                        throw;  // rethrow the exception from the update, not the post
                    }
                }
            }
            //uploadContext.Settings.SetString(EDIT_MEDIA_LINK, postImage.editUri);

            postImage.srcUrl = await PicasaRefererBlockingWorkaround(uploadContext.BlogId, uploadContext.Role, postImage.srcUrl);

            return(postImage.srcUrl);
        }
コード例 #5
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);
            }
        }
コード例 #6
0
        public override void DoAfterPublishUploadWork(IFileUploadContext uploadContext)
        {
            FileAttachSettings attachSettings = new FileAttachSettings(uploadContext.Settings);
            if (attachSettings.AttachmentFileName == null)
            {
                CalculateUploadVariables(uploadContext.FormatFileName(uploadContext.PreferredFileName), attachSettings);
            }

            string listGuid = SharepointBlogIdToListGuid(uploadContext.BlogId);
            if (listGuid != null)
            {
                SharePointListsService listsServicesharePointLists = new SharePointListsService(attachSettings.UploadServiceUrl);
                listsServicesharePointLists.Credentials = GetHttpCredentials();

                //The AddAttachment() call will throw an error if the attachment already exists, so we need to delete
                //the attachment first (if it exists).  To delete the attachment, we must construct the attachment URL
                //that is typically generated internally by the server.
                //Sample URL: http://sharepoint/sites/writer/b2/blog/Lists/Posts/Attachments/13/Sunset_thumb1.jpg
                string attachDeleteUrl = String.Format(CultureInfo.InvariantCulture, "{0}{1}/Lists/Posts/Attachments/{2}/{3}", attachSettings.BaseUrl, attachSettings.BlogUrlPart, uploadContext.PostId, attachSettings.AttachmentFileName);
                try
                {
                    listsServicesharePointLists.DeleteAttachment(listGuid, uploadContext.PostId, attachDeleteUrl);
                }
                catch (Exception) { }

                //Add the attachment
                using (Stream fileContents = uploadContext.GetContents())
                    listsServicesharePointLists.AddAttachment(listGuid, uploadContext.PostId, attachSettings.AttachmentFileName, Convert.ToBase64String(StreamHelper.AsBytes(fileContents)));

                uploadContext.Settings.SetString(uploadContext.PostId, FILE_ALREADY_UPLOADED);

                return;
            }
            throw new BlogClientFileUploadNotSupportedException();
        }