-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFaTaPhat.cs
134 lines (122 loc) · 4.16 KB
/
FaTaPhat.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
using System;
using System.IO;
using System.Net;
using System.Net.FtpClient;
using System.Xml;
using System.Collections.Specialized;
namespace gutenberg.collect
{
public class FaTaPhat : IDisposable
{
public FaTaPhat()
{
this.server = FaTaPhat.serverProps["hostname"];
this.login = FaTaPhat.serverProps["login"];
this.password = FaTaPhat.serverProps["password"];
this.bankroot = FaTaPhat.serverProps["bankroot"];
}
public void Connect()
{
if (client != null && client.IsConnected) return;
client = new FtpClient();
client.Host = server;
client.Credentials = new NetworkCredential(login, password);
client.DataConnectionType = FtpDataConnectionType.PASV;
client.Connect();
}
public void Disconnect()
{
if (client != null && client.IsConnected)
client.Disconnect();
}
public bool Exists(string remote)
{
remote = bankroot + "/" + remote;
return client.FileExists(remote);
}
public bool Put(string local, string remote, FtpDataType fileType)
{
remote = bankroot + "/" + remote;
byte[] buffer = new byte[262144];
Stream istream = File.Open(local, FileMode.Open);
int bytesRead = 0;
using (Stream ostream = client.OpenWrite(remote, fileType))
{
while ((bytesRead =
istream.Read(buffer, 0, buffer.Length)) != 0)
{
ostream.Write(buffer, 0, bytesRead);
ostream.Flush();
}
ostream.Close();
istream.Close();
}
return true;
}
public bool Get(string local, string remote, FtpDataType fileType)
{
remote = bankroot + "/" + remote;
Stream ostream = File.Create(local);
byte[] buffer = new byte[262144];
int bytesRead = 0;
using (Stream istream = client.OpenRead(remote, fileType))
{
while ((bytesRead =
istream.Read(buffer, 0, buffer.Length)) != 0)
{
ostream.Write(buffer, 0, bytesRead);
ostream.Flush();
}
ostream.Close();
istream.Close();
}
return true;
}
public void Dispose()
{
client.Dispose();
}
private FtpClient client;
private string server, login, password, bankroot;
private static NameValueCollection serverProps = FaTaPhat.GetServerProps();
private static NameValueCollection GetServerProps()
{
string configFile = Path.Combine(System.Environment.GetFolderPath
(Environment.SpecialFolder.Personal), ".scanbot");
configFile = Path.Combine(configFile, "config.xml");
bool runLocal = File.Exists(configFile);
/**
****************************************************************
* WARNING: DO NOT EDIT THIS BLOCK OF CODE, YOU RISK SENDING
* SCANS TO PRODUCTION BY ACCIDENT
****************************************************************
*/
NameValueCollection properties = new NameValueCollection();
if (!runLocal)
{
properties.Add("hostname", "109.74.201.62");
properties.Add("login", "gutenberg");
properties.Add("password", "shibb0leth");
properties.Add("bankroot", "/home/gutenberg/bank");
}
else
{
/**
****************************************************
** configuration file has your password, at least it
** is in a hidden folder in YOUR $HOME.
****************************************************
*/
XmlDocument config = new XmlDocument();
config.LoadXml(File.ReadAllText(configFile));
XmlNodeList elements = config.GetElementsByTagName("entry");
foreach (XmlNode element in elements)
{
properties.Add(element.Attributes["key"].Value,
element.InnerText);
}
}
return properties;
}
}
}