4
4
using System . Globalization ;
5
5
using System . IO ;
6
6
using System . Net ;
7
+ using System . Text . RegularExpressions ;
7
8
using System . Windows . Forms ;
8
9
9
10
namespace LoxStatEdit
@@ -18,38 +19,40 @@ public static IList<MsFileInfo> Load(Uri uri)
18
19
{
19
20
try
20
21
{
21
- // TO DO: Does not connect with Miniserver Gen 2. Probably because of TLS
22
22
var list = new List < MsFileInfo > ( ) ;
23
23
var ftpWebRequest = ( FtpWebRequest ) FtpWebRequest . Create ( uri ) ;
24
- ftpWebRequest . Method = WebRequestMethods . Ftp . ListDirectory ;
24
+ ftpWebRequest . Method = WebRequestMethods . Ftp . ListDirectoryDetails ;
25
25
using ( var response = ftpWebRequest . GetResponse ( ) )
26
26
using ( var ftpStream = response . GetResponseStream ( ) )
27
27
using ( var streamReader = new StreamReader ( ftpStream ) )
28
28
while ( ! streamReader . EndOfStream )
29
29
{
30
- // Hacky but works fair enough in our particular use case (I hope...)
31
30
var line = streamReader . ReadLine ( ) ;
32
- Debug . WriteLine ( line ) ;
33
- int datePos = line . IndexOf ( ' ' , 24 ) ;
34
- int size ;
35
- if ( ! int . TryParse ( line . Substring ( 18 , datePos - 18 ) , out size ) )
36
- size = - 1 ;
37
- datePos ++ ;
38
- int fileNamePos ;
39
- DateTime dateTime ;
40
- if ( DateTime . TryParseExact ( line . Substring ( datePos , 12 ) , "MMM dd HH:mm" ,
41
- CultureInfo . InvariantCulture , DateTimeStyles . None , out dateTime ) )
42
- fileNamePos = datePos + 13 ;
43
- else if ( DateTime . TryParseExact ( line . Substring ( datePos , 11 ) , "MMM dd yyyy" ,
44
- CultureInfo . InvariantCulture , DateTimeStyles . None , out dateTime ) )
45
- fileNamePos = datePos + 12 ;
46
- else fileNamePos = line . LastIndexOf ( ' ' ) + 1 ;
47
- list . Add ( new MsFileInfo
31
+
32
+ // string pattern that matches Miniserver Gen 1 and Miniserver Gen 2
33
+ string pattern = @"[-rwx]{10}\s+[0-9]+\s+[0-9]+\s+[0-9]+\s+([0-9]+)\s+([A-Za-z]{3}\s+[0-9]{1,2}\s+[0-9:]+)\s+([0-9a-z_\-\.]+)" ;
34
+ var result = Regex . Match ( line , pattern ) ;
35
+
36
+ if ( result . Success )
48
37
{
49
- FileName = line . Substring ( fileNamePos ) ,
50
- Date = dateTime ,
51
- Size = size ,
52
- } ) ;
38
+ var groups = result . Groups ;
39
+ int . TryParse ( groups [ 1 ] . Value , out int size ) ;
40
+
41
+ DateTime dateTime ;
42
+ if ( DateTime . TryParseExact ( groups [ 2 ] . Value . Replace ( " " , " " ) , "MMM dd HH:mm" ,
43
+ CultureInfo . InvariantCulture , DateTimeStyles . None , out dateTime ) ) ;
44
+ else if ( DateTime . TryParseExact ( groups [ 2 ] . Value . Replace ( " " , " " ) , "MMM dd yyyy" ,
45
+ CultureInfo . InvariantCulture , DateTimeStyles . None , out dateTime ) ) ;
46
+
47
+ var fileName = groups [ 3 ] . Value ;
48
+
49
+ list . Add ( new MsFileInfo
50
+ {
51
+ FileName = fileName ,
52
+ Date = dateTime ,
53
+ Size = size ,
54
+ } ) ;
55
+ }
53
56
}
54
57
return list ;
55
58
}
@@ -64,23 +67,7 @@ public static IList<MsFileInfo> Load(Uri uri)
64
67
}
65
68
catch ( Exception ex )
66
69
{
67
- if ( ex . Source == "mscorlib" && ex . HResult == - 2146233086 )
68
- {
69
- MessageBox . Show (
70
- "The connection to a Miniserver newer generation (with TLS) is " +
71
- "not working yet. Use a third party FTP client (e.g. Filezilla, " +
72
- "Windows Explorer, ...) to download and upload the statistics.\n \n " +
73
- "Feel free to contribute to this project and help us to fix this. " +
74
- "You find the GitHub link at the bottom of the main window." ,
75
- "Error - IList" ,
76
- MessageBoxButtons . OK ,
77
- MessageBoxIcon . Error
78
- ) ;
79
- }
80
- else
81
- {
82
- MessageBox . Show ( ex . Message , "Error - IList" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
83
- }
70
+ MessageBox . Show ( ex . Message , "Error - IList" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
84
71
85
72
return null ;
86
73
}
0 commit comments