📄 service.cs
字号:
/// <param name="UserName">用户名</param>
/// <param name="PassWord">密码</param>
/// <returns>标志:1已存在,2失败,0成功</returns>
[WebMethod(Description = "注册 需要提供合法票据")]
public int Register(string UserName,string PassWord)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from users where name='" + UserName + "'";
cmd.Connection = conn;
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
return 1;
}
dr.Dispose();
conn.Close();
cmd.CommandText = "insert into users(name,pass) values(@_name,@_pass)";
cmd.Connection = conn;
cmd.Parameters.Add("@_name", SqlDbType.VarChar);
cmd.Parameters.Add("@_pass", SqlDbType.VarChar);
cmd.Parameters[0].Value = UserName;
cmd.Parameters[1].Value = PassWord;
try
{
conn.Open();
cmd.ExecuteNonQuery();
return 0;
}
catch
{
return 2;
}
finally
{
cmd.Dispose();
conn.Dispose();
}
}
#endregion
#region Share
/// <summary>
/// 获取共享文件夹列表
/// </summary>
/// <param name="ticket">加密票</param>
/// <param name="author"></param>
/// <returns></returns>
[WebMethod(Description = "刷新共享文件 需要提供合法票据")]
public string GetShareFile(string ticket, string author)
{
///查看加密票是否有效
if (IsTicketValid(ticket, false))
{
///获取共享文件夹文件列表
string Results = ListShow(rootdir + author);
return Results;
}
else return "验证失败";
}
/// <summary>
/// 设置共享
/// </summary>
/// <param name="ticket">加密票</param>
/// <param name="Folder">路径</param>
/// <param name="Accept">接受共享者</param>
/// <returns>成功返回空,否则返回错误信息</returns>
[WebMethod(Description = "设置共享 需要提供合法票据")]
public string SetShare(string ticket,string Folder,string Accept)
{
if (IsTicketValid(ticket,false))
{
string Path = rootdir + FormsAuthentication.Decrypt(ticket).Name + Folder;
FileInfo file = new FileInfo(Path);
///判断当前是否是文件夹
if ((file.Attributes & FileAttributes.Directory) != 0)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
SqlCommand cmd = new SqlCommand("insert into Share(author,ShareName,Accept) values(@author,@ShareName,@Accept)", conn);
cmd.Parameters.Add("@author", SqlDbType.VarChar);
cmd.Parameters.Add("@ShareName", SqlDbType.VarChar);
cmd.Parameters.Add("@Accept", SqlDbType.VarChar);
cmd.Parameters[0].Value = FormsAuthentication.Decrypt(ticket).Name;
cmd.Parameters[1].Value = Folder;
cmd.Parameters[2].Value = Accept;
conn.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Dispose();
return null;
}
else
{
return "只有文件夹可以设置共享";
}
}
else return "验证失败";
}
/// <summary>
/// 取得当前用户可用的共享列表
/// </summary>
/// <param name="ticket">加密票</param>
/// <returns>成功返回列表,否则返回空</returns>
[WebMethod(Description = "共享 需要提供合法票据")]
public string ShowShare(string ticket)
{
if (IsTicketValid(ticket,false))
{
string Results = string.Empty;
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
SqlCommand cmd = new SqlCommand("select ShareName,author from share where Accept = @Accept", conn);
cmd.Parameters.Add("@Accept", SqlDbType.VarChar);
cmd.Parameters[0].Value = FormsAuthentication.Decrypt(ticket).Name;
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Results += dr["ShareName"].ToString() + "|";
Results += dr["author"].ToString() + "*";
}
cmd.Dispose();
conn.Dispose();
return Results;
}
else return null;
}
/// <summary>
/// 取得共享文件大小
/// </summary>
/// <param name="ticket">加密票</param>
/// <param name="FileName">文件路径</param>
/// <returns></returns>
[WebMethod(Description = "取得共享文件大小 需要提供合法票据")]
public long GetShareFileSize(string ticket, string FileName)
{
if (IsTicketValid(ticket,false))
{
string FilePath = rootdir + FileName;
// check that requested file exists
if (!File.Exists(FilePath))
CustomSoapException("File not found", "The file " + FilePath + " does not exist");
return new FileInfo(FilePath).Length;
}
else return 0;
}
[WebMethod(Description = "下载共享 需要提供合法票据")]
public byte[] DownShare(string ticket, string Author, long Offset, int BufferSize)
{
if (IsTicketValid(ticket,false))
{
string FilePath = rootdir + Author;
/// check that requested file exists
if (!File.Exists(FilePath))
CustomSoapException("File not found", "The file " + FilePath + " does not exist");
long FileSize = new FileInfo(FilePath).Length;
/// if the requested Offset is larger than the file, bail out.
if (Offset > FileSize)
CustomSoapException("Invalid Download Offset", String.Format("The file size is {0}, received request for offset {1}", FileSize, Offset));
/// open the file to return the requested chunk as a byte[]
byte[] TmpBuffer;
int BytesRead;
using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read))
{
fs.Seek(Offset, SeekOrigin.Begin); /// this is relevent during a retry. otherwise, it just seeks to the start
TmpBuffer = new byte[BufferSize];
BytesRead = fs.Read(TmpBuffer, 0, BufferSize); /// read the first chunk in the buffer (which is re-used for every chunk)
}
if (BytesRead != BufferSize)
{
/// the last chunk will almost certainly not fill the buffer, so it must be trimmed before returning
byte[] TrimmedBuffer = new byte[BytesRead];
Array.Copy(TmpBuffer, TrimmedBuffer, BytesRead);
return TrimmedBuffer;
}
else
return TmpBuffer;
}
else return null;
}
/// <summary>
/// 属性
/// </summary>
/// <param name="ticket">加密票</param>
/// <param name="Name">路径</param>
/// <returns>成功返回属性值,否则返回空</returns>
[WebMethod(Description = "属性 需要提供合法票据")]
public string ShareAttribute(string ticket, string Name)
{
if (IsTicketValid(ticket,false))
{
string Results = string.Empty;
string Path = rootdir + FormsAuthentication.Decrypt(ticket).Name + Name;
UploadPath = rootdir + FormsAuthentication.Decrypt(ticket).Name;
DirectoryInfo dirAll = new DirectoryInfo(UploadPath);
long CurrSize = DirSize(dirAll);
long AllowSize = CheckSize(FormsAuthentication.Decrypt(ticket).Name);
Results += "总共:" + FormatFileSize(AllowSize);
Results += "|已用:" + FormatFileSize(CurrSize);
Results += "|剩余:" + FormatFileSize(AllowSize - CurrSize);
Results += "|创建时间:" + File.GetCreationTime(Path);
Results += "|修改时间:" + File.GetLastWriteTime(Path);
FileInfo file = new FileInfo(Path);
DirectoryInfo dir = new DirectoryInfo(Path);
///判断当前是否是文件夹
if ((file.Attributes & FileAttributes.Directory) != 0)
{
///如果是文件夹
Results += "|大小:" + FormatFileSize(DirSize(dir));
int DirCount = dir.GetDirectories().GetLength(0);
int FileCount = dir.GetFiles().GetLength(0);
Results += "|共有文件夹 " + DirCount.ToString() + " 个|";
Results += "文件 " + FileCount.ToString() + " 个";
}
else
{
///如果是文件
Results += "|大小:" + FormatFileSize(GetFileSize(ticket, Name));
}
return Results;
}
else return null;
}
#endregion
#region Role
/// <summary>
/// 设置空间大小
/// </summary>
/// <param name="ticket">加密票</param>
/// <param name="size">空间大小 单位(MB)</param>
/// <returns>成功返回空,否则返回错误信息</returns>
[WebMethod(Description = "设置空间大小 需要提供合法票据并且是管理员身份")]
public string SetSpaceSize(string ticket,int size)
{
if (IsTicketValid(ticket, true))
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
conn.Open();
SqlCommand cmd = new SqlCommand("update users set disksize=@disksize where name=@name", conn);
cmd.Parameters.Add("@disksize", SqlDbType.Int);
cmd.Parameters.Add("@name", SqlDbType.VarChar);
cmd.Parameters[0].Value = size;
cmd.Parameters[1].Value = FormsAuthentication.Decrypt(ticket).Name;
try
{
cmd.ExecuteNonQuery();
return null;
}
catch
{
return "设置失败";
}
}
else return "验证失败";
}
/// <summary>
/// 设置下载速度
/// </summary>
/// <param name="ticket">加密票</param>
/// <param name="size">下载速度 单位(KB)</param>
/// <returns>成功返回空,否则返回错误信息</returns>
[WebMethod(Description = "设置下载速度 需要提供合法票据并且是管理员身份")]
public string SetSpeed(string ticket, int speed)
{
if (IsTicketValid(ticket, true))
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
conn.Open();
SqlCommand cmd = new SqlCommand("update users set speed=@speed where name=@name", conn);
cmd.Parameters.Add("@speed", SqlDbType.Int);
cmd.Parameters.Add("@name", SqlDbType.VarChar);
cmd.Parameters[0].Value = speed;
cmd.Parameters[1].Value = FormsAuthentication.Decrypt(ticket).Name;
try
{
cmd.ExecuteNonQuery();
return null;
}
catch
{
return "设置失败";
}
}
else return "验证失败";
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -