-
Notifications
You must be signed in to change notification settings - Fork 8
/
MiniserverForm.cs
288 lines (258 loc) · 9.15 KB
/
MiniserverForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows.Forms;
namespace LoxStatEdit
{
public partial class MiniserverForm: Form
{
#region class FileItem
class FileItem: IComparable<FileItem>
{
internal MsFileInfo MsFileInfo;
internal FileInfo FileInfo;
private string _name;
public string FileName
{
get
{
return (MsFileInfo != null) ? MsFileInfo.FileName : FileInfo.Name;
}
}
public string Name
{
get
{
if(_name == null)
{
try
{
if(FileInfo != null)
_name = LoxStatFile.Load(FileInfo.FullName, true).Text;
}
catch(Exception)
{
_name = null;
}
if(string.IsNullOrEmpty(_name))
_name = FileName;
}
return _name;
}
}
public DateTime YearMonth
{
get
{
return LoxStatFile.GetYearMonth(FileName);
}
}
public string Status
{
get
{
if(FileInfo == null)
{
return "Only on MS";
}
if(MsFileInfo == null)
{
return "Only on FS";
}
if(FileInfo.LastWriteTime > MsFileInfo.Date)
{
return "Newer on FS";
}
if(FileInfo.LastWriteTime < MsFileInfo.Date)
{
return "Newer on MS";
}
if(FileInfo.Length > MsFileInfo.Size)
{
return "Larger on FS";
}
if(FileInfo.Length < MsFileInfo.Size)
{
return "Larger on MS";
}
return "Same date/size";
}
}
public override string ToString()
{
return string.Format("{0} ({1})", Name, Status);
}
public int CompareTo(FileItem other)
{
var c = StringComparer.OrdinalIgnoreCase.Compare(Name, other.Name);
return (c != 0) ? c : YearMonth.CompareTo(other.YearMonth);
}
}
#endregion
#region Fields
readonly string[] _args;
IList<MsFileInfo> _msFolder = new MsFileInfo[0];
IList<FileInfo> _localFolder = new FileInfo[0];
IList<FileItem> _fileItems;
#endregion
#region Constructor
public MiniserverForm(params string[] args)
{
_args = args;
InitializeComponent();
}
#endregion
#region Methods
private void RefreshGridView()
{
var msMap = _msFolder.ToLookup(e => e.FileName, StringComparer.OrdinalIgnoreCase);
var localMap = _localFolder.ToLookup(e => e.Name, StringComparer.OrdinalIgnoreCase);
_fileItems = msMap.Select(e => e.Key).Union(localMap.Select(e => e.Key)).
Select(f => new FileItem
{
MsFileInfo = msMap[f].FirstOrDefault(),
FileInfo = localMap[f].FirstOrDefault(),
}).
OrderBy(f => f).
ToList();
_dataGridView.RowCount = _fileItems.Count;
_dataGridView.Refresh();
}
private void RefreshMs()
{
_msFolder = MsFileInfo.Load(GetMsUriBuilder().Uri);
}
private void RefreshLocal()
{
Directory.CreateDirectory(_folderTextBox.Text);
_localFolder = Directory.EnumerateFiles(_folderTextBox.Text).
Select(fileName => new FileInfo(fileName)).ToList();
}
private UriBuilder GetMsUriBuilder()
{
var uriBuilder = new UriBuilder(new Uri(_urlTextBox.Text).
GetComponents(UriComponents.Scheme | UriComponents.UserInfo |
UriComponents.Host | UriComponents.Port, UriFormat.UriEscaped));
uriBuilder.Path = "stats";
return uriBuilder;
}
private Uri GetFileNameUri(string fileName)
{
var uriBuilder = GetMsUriBuilder();
uriBuilder.Path += "/" + fileName;
return uriBuilder.Uri;
}
private void Download(FileItem fileItem)
{
var ftpWebRequest = (FtpWebRequest)FtpWebRequest.
Create(GetFileNameUri(fileItem.FileName));
ftpWebRequest.Method = WebRequestMethods.Ftp.DownloadFile;
var targetFileName = Path.Combine(_folderTextBox.Text, fileItem.FileName);
using(var response = ftpWebRequest.GetResponse())
using(var ftpStream = response.GetResponseStream())
using(var fileStream = File.OpenWrite(targetFileName))
ftpStream.CopyTo(fileStream);
File.SetLastWriteTime(targetFileName, fileItem.MsFileInfo.Date);
}
private void Upload(FileItem fileItem)
{
var ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(GetFileNameUri(fileItem.FileName));
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
using(var fileStream = File.OpenRead(
Path.Combine(_folderTextBox.Text, fileItem.FileInfo.FullName)))
using(var ftpStream = ftpWebRequest.GetRequestStream())
fileStream.CopyTo(ftpStream);
using(var response = ftpWebRequest.GetResponse()) { }
}
#endregion
#region Events
private void RefreshMsButton_Click(object sender, EventArgs e)
{
RefreshMs();
RefreshGridView();
}
private void RefreshFolderButton_Click(object sender, EventArgs e)
{
RefreshLocal();
RefreshGridView();
}
private void MiniserverForm_Load(object sender, EventArgs e)
{
_folderTextBox.Text = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments), "LoxStatEdit");
if(_args.Length >= 1)
{
_urlTextBox.Text = _args[0];
RefreshMs();
}
if(_args.Length >= 2)
_folderTextBox.Text = Path.GetFullPath(_args[1]);
RefreshLocal();
RefreshGridView();
}
private void DataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
try
{
var fileItem = _fileItems[e.RowIndex];
switch(e.ColumnIndex)
{
case 0: e.Value = fileItem.Name; break;
case 1: e.Value = fileItem.YearMonth; break;
case 2: e.Value = fileItem.Status; break;
case 3: e.Value = "Edit"; break;
case 4: e.Value = "Download"; break;
case 5: e.Value = "Upload"; break;
default: e.Value = null; break;
}
}
catch
{
#if DEBUG
Debugger.Break();
#endif
e.Value = null;
}
}
private void DataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if(e.RowIndex < 0) return; //When clicking the header row
var fileItem = _fileItems[e.RowIndex];
switch(e.ColumnIndex)
{
case 3: //Edit
using(var form = new LoxStatFileForm(fileItem.FileInfo.FullName))
form.ShowDialog(this);
break;
case 4: //Download
Download(fileItem);
RefreshLocal();
RefreshGridView();
break;
case 5: //Upload
Upload(fileItem);
RefreshMs();
RefreshGridView();
break;
}
}
private void DownloadButton_Click(object sender, EventArgs e)
{
foreach(DataGridViewRow row in _dataGridView.SelectedRows)
Download(_fileItems[row.Index]);
RefreshLocal();
RefreshGridView();
}
private void UploadButton_Click(object sender, EventArgs e)
{
foreach(DataGridViewRow row in _dataGridView.SelectedRows)
Upload(_fileItems[row.Index]);
RefreshMs();
RefreshGridView();
}
#endregion
}
}