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); }
private void CalculateUploadVariables(string fileName, FileAttachSettings attachSettings) { fileName = FileHelper.GetValidAnsiFileName(fileName, fileName.Length); //calculate the ListService endpoint based on the API URL. Note that the postAPI URL format //changed between M1 and M2, so we need try to calculate the API URL using both formats. //TODO: Once we ship final and the M1 timebomb expires, we should drop the M1 check. Match match = M1_postApiUrlRegex.Match(UrlHelper.SafeToAbsoluteUri(_postApiUrl)); if (match.Success && match.Groups[1].Success && match.Groups[2].Success) { attachSettings.BaseUrl = match.Groups[1].Value; attachSettings.BlogUrlPart = match.Groups[2].Value; } else { match = M2_postApiUrlRegex.Match(UrlHelper.SafeToAbsoluteUri(_postApiUrl)); if (match.Success && match.Groups[1].Success) { attachSettings.BaseUrl = match.Groups[1].Value; attachSettings.BlogUrlPart = String.Empty; } } int fileNameIndex = fileName.LastIndexOf('/'); if (fileNameIndex != -1) { attachSettings.AttachmentFileName = fileName.Substring(fileNameIndex + 1); } else { attachSettings.AttachmentFileName = fileName; } attachSettings.AttachmentUrl = String.Format(CultureInfo.InvariantCulture, "{0}{1}/_attach/{2}", attachSettings.BaseUrl, attachSettings.BlogUrlPart, attachSettings.AttachmentFileName); string uploadUrl = String.Format(CultureInfo.InvariantCulture, "{0}{1}/_vti_bin/lists.asmx", attachSettings.BaseUrl, attachSettings.BlogUrlPart); attachSettings.UploadServiceUrl = uploadUrl; return; }
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(); }
private void CalculateUploadVariables(string fileName, FileAttachSettings attachSettings) { fileName = FileHelper.GetValidAnsiFileName(fileName, fileName.Length); //calculate the ListService endpoint based on the API URL. Note that the postAPI URL format //changed between M1 and M2, so we need try to calculate the API URL using both formats. //TODO: Once we ship final and the M1 timebomb expires, we should drop the M1 check. Match match = M1_postApiUrlRegex.Match(UrlHelper.SafeToAbsoluteUri(_postApiUrl)); if (match.Success && match.Groups[1].Success && match.Groups[2].Success) { attachSettings.BaseUrl = match.Groups[1].Value; attachSettings.BlogUrlPart = match.Groups[2].Value; } else { match = M2_postApiUrlRegex.Match(UrlHelper.SafeToAbsoluteUri(_postApiUrl)); if (match.Success && match.Groups[1].Success) { attachSettings.BaseUrl = match.Groups[1].Value; attachSettings.BlogUrlPart = String.Empty; } } int fileNameIndex = fileName.LastIndexOf('/'); if (fileNameIndex != -1) attachSettings.AttachmentFileName = fileName.Substring(fileNameIndex + 1); else attachSettings.AttachmentFileName = fileName; attachSettings.AttachmentUrl = String.Format(CultureInfo.InvariantCulture, "{0}{1}/_attach/{2}", attachSettings.BaseUrl, attachSettings.BlogUrlPart, attachSettings.AttachmentFileName); string uploadUrl = String.Format(CultureInfo.InvariantCulture, "{0}{1}/_vti_bin/lists.asmx", attachSettings.BaseUrl, attachSettings.BlogUrlPart); attachSettings.UploadServiceUrl = uploadUrl; return; }
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; }