public override string DoBeforePublishUploadWork(IFileUploadContext uploadContext)
        {
            string albumName = ApplicationEnvironment.ProductName;

            string path = uploadContext.GetContentsLocalFilePath();

            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";
            string srcUrl;
            string editUri = uploadContext.Settings.GetString(EDIT_MEDIA_LINK, null);
            if (editUri == null || editUri.Length == 0)
            {
                PostNewImage(albumName, path, out srcUrl, out editUri);
            }
            else
            {
                try
                {
                    UpdateImage(editUri, path, out srcUrl, out editUri);
                }
                catch (Exception e)
                {
                    Trace.Fail(e.ToString());
                    if (e is WebException)
                        HttpRequestHelper.LogException((WebException)e);

                    bool success = false;
                    srcUrl = null; // compiler complains without this line
                    try
                    {
                        // couldn't update existing image? try posting a new one
                        PostNewImage(albumName, path, out srcUrl, out editUri);
                        success = true;
                    }
                    catch
                    {
                    }
                    if (!success)
                        throw;  // rethrow the exception from the update, not the post
                }
            }
            uploadContext.Settings.SetString(EDIT_MEDIA_LINK, editUri);

            PicasaRefererBlockingWorkaround(uploadContext.BlogId, uploadContext.Role, ref srcUrl);

            return srcUrl;
        }
Exemple #2
0
        public override string DoBeforePublishUploadWork(IFileUploadContext uploadContext)
        {
            FileAttachSettings attachSettings = new FileAttachSettings(uploadContext.Settings);

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

            return(attachSettings.AttachmentUrl);
        }
Exemple #3
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());
            }
        }
Exemple #4
0
        public string DoBeforePublishUploadWork(IFileUploadContext uploadContext)
        {
            string albumName = ApplicationEnvironment.ProductName;

            string path = uploadContext.GetContentsLocalFilePath();

            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]);
                }
            }

            return(PostNewImage(albumName, path));
        }
Exemple #5
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();
        }
Exemple #6
0
        public string DoBeforePublishUploadWork(IFileUploadContext uploadContext)
        {
            string albumName = ApplicationEnvironment.ProductName;

            string path = uploadContext.GetContentsLocalFilePath();

            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";
            string srcUrl;
            string editUri = uploadContext.Settings.GetString(EDIT_MEDIA_LINK, null);

            if (editUri == null || editUri.Length == 0)
            {
                PostNewImage(albumName, path, out srcUrl, out editUri);
            }
            else
            {
                try
                {
                    UpdateImage(editUri, path, out srcUrl, out editUri);
                }
                catch (Exception e)
                {
                    Trace.Fail(e.ToString());
                    if (e is WebException)
                    {
                        HttpRequestHelper.LogException((WebException)e);
                    }

                    bool success = false;
                    srcUrl = null; // compiler complains without this line
                    try
                    {
                        // couldn't update existing image? try posting a new one
                        PostNewImage(albumName, path, out srcUrl, out editUri);
                        success = true;
                    }
                    catch
                    {
                    }
                    if (!success)
                    {
                        throw;  // rethrow the exception from the update, not the post
                    }
                }
            }
            uploadContext.Settings.SetString(EDIT_MEDIA_LINK, editUri);

            PicasaRefererBlockingWorkaround(uploadContext.BlogId, uploadContext.Role, ref srcUrl);

            return(srcUrl);
        }
        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);
            }
        }
        public override Uri DoUploadWorkBeforePublish(IFileUploadContext uploadContext)
        {
            try
            {
                ConnectForUpload();

                string uploadPath = uploadContext.Settings.GetString(UPLOAD_PATH, null);
                bool overwrite = uploadPath != null;
                if (uploadPath == null)
                {
                    string uploadFolder = null;
                    string fileName = uploadContext.PreferredFileName;
                    string filePath = uploadContext.FormatFileName(fileName);
                    string[] pathParts = filePath.Split('/');
                    if (pathParts.Length > 1)
                    {
                        uploadFolder = FileHelper.GetValidAnsiFileName(pathParts[0]);
                        for (int i = 1; i < pathParts.Length - 1; i++)
                            uploadFolder = uploadFolder + "/" + FileHelper.GetValidAnsiFileName(pathParts[i]);
                    }

                    fileName = FileHelper.GetValidAnsiFileName(pathParts[pathParts.Length - 1]);
                    uploadPath = _fileDestination.CombinePath(uploadFolder, fileName);
                    if (_fileDestination.FileExists(uploadPath))
                    {
                        string fileBaseName = Path.GetFileNameWithoutExtension(fileName);
                        string fileExtension = Path.GetExtension(fileName);
                        try
                        {
                            Hashtable existingFiles = new Hashtable();
                            foreach (string name in _fileDestination.ListFiles(uploadFolder))
                                existingFiles[name] = name;
                            for (int i = 3; i < Int32.MaxValue && existingFiles.ContainsKey(fileName); i++)
                            {
                                fileName = FileHelper.GetValidAnsiFileName(fileBaseName + "_" + i + fileExtension);
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.Fail("Error while calculating unique filename", e.ToString());
                        }

                        uploadPath = _fileDestination.CombinePath(uploadFolder, fileName);
                        if (_fileDestination.FileExists(uploadPath))
                        {
                            Debug.Fail("Failed to calculate unique filename");
                            fileName = FileHelper.GetValidAnsiFileName(fileBaseName + Guid.NewGuid().ToString() + fileExtension);
                            uploadPath = _fileDestination.CombinePath(uploadFolder, fileName);
                        }
                    }
                }

                // transfer the file
                _fileDestination.DoTransfer(
                    uploadContext.GetContentsLocalFilePath(),
                    uploadPath,
                    overwrite);

                uploadContext.Settings.SetString(UPLOAD_PATH, uploadPath);

                // return the url to the transferred file
                string baseUrl = UrlHelper.InsureTrailingSlash(_settings.UrlMapping);
                string relativeUrl = uploadPath;
                return new Uri(UrlHelper.UrlCombine(baseUrl, relativeUrl));
            }
            catch (Exception ex)
            {
                WebPublishMessage message = WebPublishUtils.ExceptionToErrorMessage(ex);
                throw new BlogClientFileTransferException(new FileInfo(uploadContext.GetContentsLocalFilePath()), message.Title, message.Text);
            }
        }
Exemple #9
0
        public override Uri DoUploadWorkBeforePublish(IFileUploadContext uploadContext)
        {
            try
            {
                ConnectForUpload();

                string uploadPath = uploadContext.Settings.GetString(UPLOAD_PATH, null);
                bool   overwrite  = uploadPath != null;
                if (uploadPath == null)
                {
                    string   uploadFolder = null;
                    string   fileName     = uploadContext.PreferredFileName;
                    string   filePath     = uploadContext.FormatFileName(fileName);
                    string[] pathParts    = filePath.Split('/');
                    if (pathParts.Length > 1)
                    {
                        uploadFolder = FileHelper.GetValidAnsiFileName(pathParts[0]);
                        for (int i = 1; i < pathParts.Length - 1; i++)
                        {
                            uploadFolder = uploadFolder + "/" + FileHelper.GetValidAnsiFileName(pathParts[i]);
                        }
                    }

                    fileName   = FileHelper.GetValidAnsiFileName(pathParts[pathParts.Length - 1]);
                    uploadPath = _fileDestination.CombinePath(uploadFolder, fileName);
                    if (_fileDestination.FileExists(uploadPath))
                    {
                        string fileBaseName  = Path.GetFileNameWithoutExtension(fileName);
                        string fileExtension = Path.GetExtension(fileName);
                        try
                        {
                            Hashtable existingFiles = new Hashtable();
                            foreach (string name in  _fileDestination.ListFiles(uploadFolder))
                            {
                                existingFiles[name] = name;
                            }
                            for (int i = 3; i < Int32.MaxValue && existingFiles.ContainsKey(fileName); i++)
                            {
                                fileName = FileHelper.GetValidAnsiFileName(fileBaseName + "_" + i + fileExtension);
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.Fail("Error while calculating unique filename", e.ToString());
                        }

                        uploadPath = _fileDestination.CombinePath(uploadFolder, fileName);
                        if (_fileDestination.FileExists(uploadPath))
                        {
                            Debug.Fail("Failed to calculate unique filename");
                            fileName   = FileHelper.GetValidAnsiFileName(fileBaseName + Guid.NewGuid().ToString() + fileExtension);
                            uploadPath = _fileDestination.CombinePath(uploadFolder, fileName);
                        }
                    }
                }

                // transfer the file
                _fileDestination.DoTransfer(
                    uploadContext.GetContentsLocalFilePath(),
                    uploadPath,
                    overwrite);

                uploadContext.Settings.SetString(UPLOAD_PATH, uploadPath);

                // return the url to the transferred file
                string baseUrl     = UrlHelper.InsureTrailingSlash(_settings.UrlMapping);
                string relativeUrl = uploadPath;
                return(new Uri(UrlHelper.UrlCombine(baseUrl, relativeUrl)));
            }
            catch (Exception ex)
            {
                WebPublishMessage message = WebPublishUtils.ExceptionToErrorMessage(ex);
                throw new BlogClientFileTransferException(new FileInfo(uploadContext.GetContentsLocalFilePath()), message.Title, message.Text);
            }
        }
        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);
        }
        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);
            }
        }
        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();
        }
        public override string DoBeforePublishUploadWork(IFileUploadContext uploadContext)
        {

            FileAttachSettings attachSettings = new FileAttachSettings(uploadContext.Settings);
            if (attachSettings.AttachmentFileName == null)
            {
                string fileName = uploadContext.FormatFileName(CleanUploadFilename(uploadContext.PreferredFileName));
                CalculateUploadVariables(fileName, attachSettings);
            }

            return attachSettings.AttachmentUrl;
        }