温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:LiveBlog v1.0测试版源码
当前文件:
LiveBlog/LiveBlog.Web/Api/BlogImporter.asmx,打开代码结构图
LiveBlog/LiveBlog.Web/Api/BlogImporter.asmx,打开代码结构图1<%@ WebService Language="C#" Class="BlogImporter" %> 2
3
using System; 4
using System.IO; 5
using System.Net; 6
using System.Web; 7
using System.Collections.ObjectModel; 8
using System.Web.Services; 9
using System.Web.Security; 10
using System.Web.Services.Protocols; 11
using System.Text.RegularExpressions; 12
using LiveBlog.Core; 13
14
/// <summary> 15
/// Web Service API for Blog Importer 16
/// </summary> 17
[WebService(Namespace = "http://iusers.com.cn/")] 18
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 19
public class BlogImporter : System.Web.Services.WebService { 20
/// <summary> 21
/// Name/Type of Blog Software 22
/// </summary> 23
/// <returns>Blog Software name</returns> 24
[WebMethod] 25
public string BlogType() { 26
return "LiveBlog"; 27
} 28
29
/// <summary> 30
/// Version Number of the Blog 31
/// </summary> 32
/// <returns>Version number in string</returns> 33
[WebMethod] 34
public string BlogVersion() { 35
return BlogSettings.Instance.Version(); 36
} 37
38
/// <summary> 39
/// Relative File Handler path 40
/// </summary> 41
/// <returns>file handler path as string</returns> 42
[WebMethod] 43
public string BlogFileHandler() { 44
return "file.axd?file="; 45
} 46
47
/// <summary> 48
/// Relative Image Handler path 49
/// </summary> 50
/// <returns>image handler path as string</returns> 51
[WebMethod] 52
public string BlogImageHandler() { 53
return "image.axd?picture="; 54
} 55
56
/// <summary> 57
/// Add new blog post to system 58
/// </summary> 59
/// <param name="import">ImportPost object</param> 60
/// <param name="previousUrl">Old Post Url (for Url re-writing)</param> 61
/// <param name="removeDuplicate">Search for duplicate post and remove?</param> 62
/// <returns>string containing unique post identifier</returns> 63
[SoapHeader("AuthenticationHeader")] 64
[WebMethod] 65
public string AddPost(ImportPost import, string previousUrl, bool removeDuplicate) { 66
if (!IsAuthenticated()) 67
throw new InvalidOperationException("Wrong credentials"); 68
69
if (removeDuplicate) { 70
if (!Post.IsTitleUnique(import.Title)) { 71
// Search for matching post (by date and title) and delete it 72
foreach (Post temp in Post.GetPostsByDate(import.PostDate.AddDays(-2), import.PostDate.AddDays(2))) { 73
if (temp.Title == import.Title) { 74
temp.Delete(); 75
temp.Import(); 76
} 77
} 78
} 79
} 80
81
Post post = new Post(); 82
post.Title = import.Title; 83
post.Author = import.Author; 84
post.DateCreated = import.PostDate; 85
post.Content = import.Content; 86
post.Description = import.Description; 87
post.IsPublished = import.Publish; 88
//TODO: Save Previous Url? 89
90
AddCategories(import.Categories, post); 91
//TODO: Add Tag Support 92
post.Import(); 93
94
return post.Id.ToString(); 95
96
} 97
98
/// <summary> 99
/// Add Comment to specified post 100
/// </summary> 101
/// <param name="postID">postId as string</param> 102
/// <param name="author">commenter username</param> 103
/// <param name="email">commenter email</param> 104
/// <param name="website">commenter url</param> 105
/// <param name="description">actual comment</param> 106
/// <param name="date">comment datetime</param> 107
[SoapHeader("AuthenticationHeader")] 108
[WebMethod] 109
public void AddComment(string postID, string author, string email, string website, string description, DateTime date) { 110
if (!IsAuthenticated()) 111
throw new InvalidOperationException("Wrong credentials"); 112
113
//Post post = Post.GetPost(new Guid(postID)); 114
Post post = Post.Load(new Guid(postID)); 115
if (post != null) { 116
Comment comment = new Comment(); 117
comment.Author = author; 118
comment.Email = email; 119
Uri url; 120
if (Uri.TryCreate(website, UriKind.Absolute, out url)) 121
comment.Website = url; 122
123
comment.Content = description; 124
comment.DateCreated = date; 125
comment.Parent = post; 126
comment.IsApproved = true; 127
post.ImportComment(comment); 128
post.Import(); 129
} 130
} 131
132
/// <summary> 133
/// Force Reload of all posts 134
/// </summary> 135
[SoapHeader("AuthenticationHeader")] 136
[WebMethod] 137
public void ForceReload() 138
{ 139
if (!IsAuthenticated()) 140
throw new InvalidOperationException("Wrong credentials"); 141
142
Post.Reload(); 143
} 144
145
/// <summary> 146
/// Downloads specified file to specified location 147
/// </summary> 148
/// <param name="source">source file path</param> 149
/// <param name="destination">relative destination path</param> 150
/// <returns></returns> 151
[SoapHeader("AuthenticationHeader")] 152
[WebMethod] 153
public bool GetFile(string source, string destination) { 154
bool response; 155
try { 156
string rootPath = BlogSettings.Instance.StorageLocation + "files/" + DateTime.Now.ToString("yyyyMM") + "/"; 157
string serverPath = Server.MapPath(rootPath); 158
string saveFolder = serverPath; 159
string fileName = destination; 160
161
// Check/Create Folders & Fix fileName 162
if (fileName.LastIndexOf('/') > -1) { 163
saveFolder += fileName.Substring(0, fileName.LastIndexOf('/')); 164
saveFolder = saveFolder.Replace('/', Path.DirectorySeparatorChar); 165
166
fileName = fileName.Substring(fileName.LastIndexOf('/') + 1); 167
} else { 168
if (saveFolder.EndsWith(Path.DirectorySeparatorChar.ToString())) 169
saveFolder = saveFolder.Substring(0, saveFolder.Length - 1); 170
} 171
172
if (!Directory.Exists(saveFolder)) 173
Directory.CreateDirectory(saveFolder); 174
saveFolder += Path.DirectorySeparatorChar; 175
176
using (WebClient client = new WebClient()) { 177
client.DownloadFile(source, saveFolder + fileName); 178
} 179
response = true; 180
} catch (Exception) { 181
// The file probably didn't exist. No action needed. 182
response = false; 183
} 184
185
return response; 186
} 187
188
private bool IsAuthenticated() { 189
return Membership.ValidateUser(AuthenticationHeader.Username, AuthenticationHeader.Password); 190
} 191
192
public AuthHeader AuthenticationHeader; 193
194
public class AuthHeader : SoapHeader { 195
public string Username; 196
public string Password; 197
} 198
199
public class ImportPost { 200
public string Title; 201
public string Author; 202
public DateTime PostDate; 203
public string Content; 204
public string Description; 205
public Collection<string> Categories; 206
public Collection<string> Tags; 207
public bool Publish; 208
} 209
210
private static void AddCategories(Collection<string> categories, Post post) { 211
try { 212
foreach (string category in categories) { 213
bool added = false; 214
foreach (Category cat in Category.Categories) { 215
if (cat.Title.Equals(category, StringComparison.OrdinalIgnoreCase)) { 216
post.Categories.Add(cat); 217
added = true; 218
} 219
} 220
if (!added) { 221
Category newCat = new Category(category, string.Empty); 222
newCat.Save(); 223
post.Categories.Add(newCat); 224
} 225
} 226
} catch (Exception ex) { 227
string test = ex.Message; 228
} 229
} 230
231
} 232
233







