| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.ComponentModel;
|
|---|
| 4 | using System.Data;
|
|---|
| 5 | using System.Drawing;
|
|---|
| 6 | using System.IO;
|
|---|
| 7 | using System.Linq;
|
|---|
| 8 | using System.Net;
|
|---|
| 9 | using System.Text;
|
|---|
| 10 | using System.Threading;
|
|---|
| 11 | using System.Windows.Forms;
|
|---|
| 12 |
|
|---|
| 13 | using CookComputing.XmlRpc;
|
|---|
| 14 | using MTFileUploader.Properties;
|
|---|
| 15 |
|
|---|
| 16 | namespace MTFileUploader
|
|---|
| 17 | {
|
|---|
| 18 | public partial class MainForm : Form
|
|---|
| 19 | {
|
|---|
| 20 | private Settings _settings = new Settings();
|
|---|
| 21 | private Boolean _isUploading = false;
|
|---|
| 22 |
|
|---|
| 23 | public MainForm()
|
|---|
| 24 | {
|
|---|
| 25 | InitializeComponent();
|
|---|
| 26 |
|
|---|
| 27 | comboSites.DataSource = _settings.Sites;
|
|---|
| 28 | UpdateSites();
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | #region イベントハンドラ
|
|---|
| 32 |
|
|---|
| 33 | private void comboSites_SelectedIndexChanged(object sender, EventArgs e)
|
|---|
| 34 | {
|
|---|
| 35 | if (comboSites.SelectedItem != null)
|
|---|
| 36 | txtUploadPath.Text = (comboSites.SelectedItem as SiteSetting).DefaultSitePath;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | private void lvUploadFiles_DragEnter(object sender, DragEventArgs e)
|
|---|
| 40 | {
|
|---|
| 41 | if (!_isUploading && e.Data.GetDataPresent("FileDrop"))
|
|---|
| 42 | {
|
|---|
| 43 | e.Effect = DragDropEffects.Copy;
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | private void lvUploadFiles_DragDrop(object sender, DragEventArgs e)
|
|---|
| 48 | {
|
|---|
| 49 | if (!_isUploading && e.Data.GetDataPresent("FileDrop"))
|
|---|
| 50 | {
|
|---|
| 51 | lvUploadFiles.Items.Clear();
|
|---|
| 52 | foreach (var fileName in (e.Data.GetData("FileDrop") as String[]))
|
|---|
| 53 | {
|
|---|
| 54 | String fileNameOnly = Path.GetFileName(fileName);
|
|---|
| 55 | String dirName = Path.GetDirectoryName(fileName);
|
|---|
| 56 | if (Directory.Exists(fileName))
|
|---|
| 57 | {
|
|---|
| 58 | // ディレクトリ
|
|---|
| 59 | FindUploadFiles(dirName, fileName);
|
|---|
| 60 | }
|
|---|
| 61 | else
|
|---|
| 62 | {
|
|---|
| 63 | // ファイル
|
|---|
| 64 | ListViewItem item = new ListViewItem(fileNameOnly);
|
|---|
| 65 | item.Tag = fileNameOnly;
|
|---|
| 66 | item.ImageKey = "Image_File";
|
|---|
| 67 | item.SubItems.Add(fileNameOnly);
|
|---|
| 68 | item.SubItems.Add(fileName);
|
|---|
| 69 | lvUploadFiles.Items.Add(item);
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 | UpdateServerPath();
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | private void btnUpload_Click(object sender, EventArgs e)
|
|---|
| 77 | {
|
|---|
| 78 | if (_isUploading)
|
|---|
| 79 | {
|
|---|
| 80 | backgroundWorker.CancelAsync();
|
|---|
| 81 | return;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | SiteSetting settings = comboSites.SelectedItem as SiteSetting;
|
|---|
| 85 | if (settings == null)
|
|---|
| 86 | {
|
|---|
| 87 | MessageBox.Show(this, "接続先が選択されていません。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|---|
| 88 | return;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | _isUploading = true;
|
|---|
| 92 | lvUploadFiles.ContextMenuStrip = null;
|
|---|
| 93 | txtUploadPath.Enabled = false;
|
|---|
| 94 | comboSites.Enabled = false;
|
|---|
| 95 | btnUpload.Text = "キャンセル(&C)";
|
|---|
| 96 |
|
|---|
| 97 | foreach (ListViewItem item in lvUploadFiles.Items)
|
|---|
| 98 | item.ImageKey = "Image_File";
|
|---|
| 99 |
|
|---|
| 100 | backgroundWorker.RunWorkerAsync();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | private void btnSettings_Click(object sender, EventArgs e)
|
|---|
| 104 | {
|
|---|
| 105 | using (SettingsForm settingsForm = new SettingsForm())
|
|---|
| 106 | {
|
|---|
| 107 | settingsForm.ShowDialog();
|
|---|
| 108 | _settings.Reload();
|
|---|
| 109 | UpdateSites();
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
|
|---|
| 114 | {
|
|---|
| 115 | ExecuteUpload(e);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
|---|
| 119 | {
|
|---|
| 120 | if (e.Cancelled)
|
|---|
| 121 | toolStripStatusLabel.Text = "ファイルのアップロードはキャンセルされました";
|
|---|
| 122 | else if (e.Result == null)
|
|---|
| 123 | toolStripStatusLabel.Text = "ファイルのアップロードが完了しました";
|
|---|
| 124 | else
|
|---|
| 125 | toolStripStatusLabel.Text = e.Result.ToString();
|
|---|
| 126 |
|
|---|
| 127 | _isUploading = false;
|
|---|
| 128 |
|
|---|
| 129 | btnUpload.Text = "アップロード(&U)";
|
|---|
| 130 | txtUploadPath.Enabled = true;
|
|---|
| 131 | comboSites.Enabled = true;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
|
|---|
| 135 | {
|
|---|
| 136 | StatusState state = e.UserState as StatusState;
|
|---|
| 137 | if (state.Status != Status.Error)
|
|---|
| 138 | {
|
|---|
| 139 | toolStripStatusLabel.Text = state.Text;
|
|---|
| 140 | }
|
|---|
| 141 | else
|
|---|
| 142 | {
|
|---|
| 143 | toolStripStatusLabel.Text = "アップロード中にエラーが発生しました";
|
|---|
| 144 | MessageBox.Show(this, state.Text, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | if (state.Item != null)
|
|---|
| 148 | {
|
|---|
| 149 | state.Item.ImageKey = (
|
|---|
| 150 | state.Status == Status.Done ? "Done" :
|
|---|
| 151 | state.Status == Status.Uploading ? "Publish" :
|
|---|
| 152 | state.Status == Status.Error ? "Error" :
|
|---|
| 153 | "Image_File");
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | private void txtUploadPath_TextChanged(object sender, EventArgs e)
|
|---|
| 158 | {
|
|---|
| 159 | UpdateServerPath();
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | private void lvUploadFiles_KeyUp(object sender, KeyEventArgs e)
|
|---|
| 163 | {
|
|---|
| 164 | if (!_isUploading)
|
|---|
| 165 | {
|
|---|
| 166 | if (e.KeyCode == Keys.Delete && lvUploadFiles.SelectedItems.Count > 0)
|
|---|
| 167 | {
|
|---|
| 168 | var itemsTemp = new List<ListViewItem>(lvUploadFiles.SelectedItems.OfType<ListViewItem>());
|
|---|
| 169 | foreach (var item in itemsTemp)
|
|---|
| 170 | lvUploadFiles.Items.Remove(item);
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
|
|---|
| 176 | {
|
|---|
| 177 | if (!_isUploading)
|
|---|
| 178 | {
|
|---|
| 179 | if (lvUploadFiles.SelectedItems.Count > 0)
|
|---|
| 180 | {
|
|---|
| 181 | var itemsTemp = new List<ListViewItem>(lvUploadFiles.SelectedItems.OfType<ListViewItem>());
|
|---|
| 182 | foreach (var item in itemsTemp)
|
|---|
| 183 | lvUploadFiles.Items.Remove(item);
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | private void lvUploadFiles_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
|
|---|
| 189 | {
|
|---|
| 190 | if (!_isUploading)
|
|---|
| 191 | {
|
|---|
| 192 | lvUploadFiles.ContextMenuStrip = ((lvUploadFiles.SelectedItems.Count > 0) ? contextMenuStrip : null);
|
|---|
| 193 | }
|
|---|
| 194 | }
|
|---|
| 195 | #endregion
|
|---|
| 196 |
|
|---|
| 197 | private void UpdateServerPath()
|
|---|
| 198 | {
|
|---|
| 199 | var baseDir = txtUploadPath.Text.Trim('/');
|
|---|
| 200 | foreach (ListViewItem item in lvUploadFiles.Items)
|
|---|
| 201 | {
|
|---|
| 202 | var fileRelPath = item.Tag as String;
|
|---|
| 203 | item.SubItems[1].Text = (baseDir.Length == 0 ? fileRelPath : String.Format("{0}/{1}", baseDir, fileRelPath));
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | private void ExecuteUpload(DoWorkEventArgs e)
|
|---|
| 208 | {
|
|---|
| 209 | SiteSetting settings = Invoke((Func<Object>)(() => comboSites.SelectedItem)) as SiteSetting;
|
|---|
| 210 | IMetaWeblog metaWeblog = CookComputing.XmlRpc.XmlRpcProxyGen.Create<IMetaWeblog>();
|
|---|
| 211 | metaWeblog.Url = settings.EndPointUrl;
|
|---|
| 212 |
|
|---|
| 213 | // Basic 認証
|
|---|
| 214 | if (!String.IsNullOrEmpty(settings.HttpUserName))
|
|---|
| 215 | {
|
|---|
| 216 | CredentialCache credCache = new CredentialCache();
|
|---|
| 217 | credCache.Add(new Uri(metaWeblog.Url), "Basic", new NetworkCredential(settings.HttpUserName, settings.HttpPassword));
|
|---|
| 218 | metaWeblog.Credentials = credCache;
|
|---|
| 219 | metaWeblog.PreAuthenticate = true;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | var userName = settings.UserName;
|
|---|
| 223 | var password = settings.Password;
|
|---|
| 224 | var blogId = settings.BlogId;
|
|---|
| 225 |
|
|---|
| 226 | // ログインチェック
|
|---|
| 227 | try
|
|---|
| 228 | {
|
|---|
| 229 | backgroundWorker.ReportProgress(0, new StatusState() { Text = "ログイン認証を確認しています..." });
|
|---|
| 230 | metaWeblog.GetUsersBlogs(1, userName, password);
|
|---|
| 231 | }
|
|---|
| 232 | catch (XmlRpcFaultException ex)
|
|---|
| 233 | {
|
|---|
| 234 | String errorMessage = String.Format("ログイン時にエラーが発生しました。{0}\r\n\r\n詳細なメッセージ: \r\n{1}", ex.FaultString, ex.Message);
|
|---|
| 235 | backgroundWorker.ReportProgress(0, new StatusState() { Text = errorMessage, Status = Status.Error });
|
|---|
| 236 | e.Result = "アップロード中にエラーが発生しました。";
|
|---|
| 237 | return;
|
|---|
| 238 | }
|
|---|
| 239 | catch (XmlRpcServerException ex)
|
|---|
| 240 | {
|
|---|
| 241 | String errorMessage = String.Format("サーバへ接続時にエラーが発生しました。\r\n\r\n詳細なメッセージ: \r\n{0}", ex.Message);
|
|---|
| 242 | backgroundWorker.ReportProgress(0, new StatusState() { Text = errorMessage, Status = Status.Error });
|
|---|
| 243 | e.Result = "アップロード中にエラーが発生しました。";
|
|---|
| 244 | return;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | var baseDir = txtUploadPath.Text.Trim('/');
|
|---|
| 248 | // アップロード
|
|---|
| 249 | foreach (var i in Enumerable.Range(0, lvUploadFiles.Items.Count))
|
|---|
| 250 | {
|
|---|
| 251 | // キャンセル
|
|---|
| 252 | if (backgroundWorker.CancellationPending)
|
|---|
| 253 | {
|
|---|
| 254 | e.Cancel = true;
|
|---|
| 255 | return;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | var item = Invoke((Func<ListViewItem>)(() => lvUploadFiles.Items[i])) as ListViewItem;
|
|---|
| 259 | var fileRelPath = item.SubItems[1].Text;
|
|---|
| 260 | var statusText = String.Format("ファイル {0} をアップロードしています(残り{1}個のファイル)...", fileRelPath, lvUploadFiles.Items.Count - i);
|
|---|
| 261 | Int32 progressPct = (i / lvUploadFiles.Items.Count) * 100;
|
|---|
| 262 | backgroundWorker.ReportProgress(progressPct, new StatusState() { Text = statusText, Item = item, Status = Status.Uploading });
|
|---|
| 263 |
|
|---|
| 264 | var mediaObject = new CookComputing.XmlRpc.XmlRpcStruct();
|
|---|
| 265 | mediaObject.Add("name", fileRelPath);
|
|---|
| 266 | mediaObject.Add("bits", File.ReadAllBytes(item.SubItems[2].Text));
|
|---|
| 267 | try
|
|---|
| 268 | {
|
|---|
| 269 | metaWeblog.NewMediaObject(blogId, userName, password, mediaObject);
|
|---|
| 270 | backgroundWorker.ReportProgress(progressPct, new StatusState() { Text = statusText, Item = item, Status = Status.Done });
|
|---|
| 271 | }
|
|---|
| 272 | catch (XmlRpcFaultException ex)
|
|---|
| 273 | {
|
|---|
| 274 | String errorMessage = String.Format("エラーが発生しました。{0}\r\n\r\n詳細なメッセージ: \r\n{1}", ex.FaultString, ex.Message);
|
|---|
| 275 | backgroundWorker.ReportProgress(progressPct, new StatusState() { Text = errorMessage, Item = item, Status = Status.Error });
|
|---|
| 276 | e.Result = "アップロード中にエラー発生しました";
|
|---|
| 277 | return;
|
|---|
| 278 | }
|
|---|
| 279 | catch (XmlRpcServerException ex)
|
|---|
| 280 | {
|
|---|
| 281 | String errorMessage = String.Format("サーバへ接続時にエラーが発生しました。\r\n\r\n詳細なメッセージ: \r\n{0}", ex.Message);
|
|---|
| 282 | backgroundWorker.ReportProgress(progressPct, new StatusState() { Text = errorMessage, Item = item, Status = Status.Error });
|
|---|
| 283 | e.Result = "アップロード中にエラー発生しました";
|
|---|
| 284 | return;
|
|---|
| 285 | }
|
|---|
| 286 | }
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | private void FindUploadFiles(String baseDir, String searchDir)
|
|---|
| 290 | {
|
|---|
| 291 | foreach (var dirName in Directory.GetDirectories(searchDir))
|
|---|
| 292 | {
|
|---|
| 293 | FindUploadFiles(baseDir, dirName);
|
|---|
| 294 | }
|
|---|
| 295 | foreach (var fileName in Directory.GetFiles(searchDir))
|
|---|
| 296 | {
|
|---|
| 297 | // ファイル
|
|---|
| 298 | String fileNameOnly = Path.GetFileName(fileName);
|
|---|
| 299 | String dirName = Path.GetDirectoryName(fileName);
|
|---|
| 300 | String fileRelativePath = fileName.Remove(0, baseDir.Length + 1);
|
|---|
| 301 |
|
|---|
| 302 | ListViewItem item = new ListViewItem(fileNameOnly, "Image_File");
|
|---|
| 303 | item.Tag = fileRelativePath.Replace('\\', '/');
|
|---|
| 304 | item.SubItems.Add(fileRelativePath.Replace('\\', '/')); // Server path
|
|---|
| 305 | item.SubItems.Add(fileName); // File path
|
|---|
| 306 | lvUploadFiles.Items.Add(item);
|
|---|
| 307 | }
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | private void UpdateSites()
|
|---|
| 311 | {
|
|---|
| 312 | comboSites.DataSource = _settings.Sites;
|
|---|
| 313 | comboSites.BindingContext[_settings.Sites].SuspendBinding();
|
|---|
| 314 | comboSites.BindingContext[_settings.Sites].ResumeBinding();
|
|---|
| 315 | comboSites.SelectedItem = null;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | private enum Status
|
|---|
| 319 | {
|
|---|
| 320 | Ready = 1 << 0,
|
|---|
| 321 | Uploading = 1 << 2,
|
|---|
| 322 | Done = 1 << 3,
|
|---|
| 323 | Error = 255
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | private class StatusState
|
|---|
| 327 | {
|
|---|
| 328 | public String Text { get; set; }
|
|---|
| 329 | public Status Status { get; set; }
|
|---|
| 330 | public ListViewItem Item { get; set; }
|
|---|
| 331 | }
|
|---|
| 332 | }
|
|---|
| 333 | }
|
|---|