封装成函数如下:(测试账号及api文档见1楼及3楼)
'函数调用0=======================
'Functions.Execute("seafile_上传文件",ml0,fn)
'将本地文件上传到远程seafile,上传前需将文件复制到指定本地上传目录
'ml0:seafile上传相对目录,用于合成目录seafile_OAml & ml0
'fn:上传文件名
'函数调用1=======================
Dim fcn As String = "seafile_上传文件"
Dim ml0 As String = args(0) '相对目录
Dim fn As String = args(1) '文件名
Dim ml As String = seafile_OAml & ml0 'seaflie上传目录
Dim file As String = gs_OAup & ml0 & fn '本地上传文件路径
Dim timeout As Integer = 10000
If args.length>2
timeout = args(2)
End If
Dim id As String = seafile_ID
If args.length>3
id= args(3)
End If
'ts(fcn,"id ",id )
'获取上传链接
Dim url As String = "api2/repos/" & id & "/upload-link/?p=" & ml
'ts(fcn,"url",url )
Dim str As String = Functions.Execute("seafile_get",url)
'ts(fcn,"str",str)
'获取失败则返回错误
If Iserr(str)
ts(fcn,"获取上传链接失败",str)
Return str
End If
str=str.Trim("""")
'ts(fcn,"获取上传链接成功",str)
url = str
'获取cookieheader
Dim cookieheader As String
str = Functions.Execute("seafile_验证token")
If Iserr(str)
ts(fcn,"seafile_验证token失败",str)
Return ""
End If
cookieheader = "token " & seafile_token
'ts(fcn,"seafile_验证token成功",cookieheader )
'边界符
Dim boundary As String = "---------------------------" + DateTime.Now.Ticks.ToString("x") '起始
Dim hl As Byte() = Encoding.ASCII.GetBytes(vbCrLf) '换行
Dim beginBoundary As Byte() = Encoding.ASCII.GetBytes("--" & boundary & vbCrLf) '分割符
Dim endBoundary As Byte() = Encoding.ASCII.GetBytes(vbcrlf & "--" & boundary & "--" & vbcrlf) '结束
'文件流
Dim fileStream As new IO.FileStream(file, io.FileMode.Open, io.FileAccess.Read)
'定义变量
Dim responseContent As String
Dim memStream As New IO.MemoryStream
Dim header As String
Dim headerbytes As Byte()
'设置属性
Dim webRequest = System.Net.HttpWebRequest.Create(url)
webRequest.Method = "POST"
webRequest.Timeout = 10000
webRequest.C & boundary
webRequest.Headers.Add("Authorization",cookieheader)
'ts(fcn,"webRequest.ContentType",webRequest.ContentType)
'文件路径
memStream.Write(beginBoundary, 0, beginBoundary.Length)
header = "Content-Disposition: form-data;" & " name=""parent_dir" & """" & vbCrLf & vbCrLf
header &= ml
headerbytes = Encoding.UTF8.GetBytes(header)
memStream.Write(headerbytes, 0, headerbytes.Length)
'文件
memStream.Write(hl, 0, hl.Length)
memStream.Write(beginBoundary, 0, beginBoundary.Length)
header = "Content-Disposition: form-data;" & " name=""file" & """" & ";filename=" & """" & fn & """" & vbCrLf
header &= "Content-Type: application/octet-stream" & vbCrLf & vbCrLf
headerbytes = Encoding.UTF8.GetBytes(header)
memStream.Write(headerbytes, 0, headerbytes.Length)
Dim buffer(1024) As Byte
Dim bytesRead As Integer = fileStream.Read(buffer, 0, buffer.Length)
While bytesRead > 0
memStream.Write(buffer, 0, bytesRead)
bytesRead = fileStream.Read(buffer, 0, buffer.Length)
End While
'写入最后的结束边界符
memStream.Write(endBoundary, 0, endBoundary.Length)
webRequest.ContentLength = memStream.Length
try
Dim requestStream = webRequest.GetRequestStream
memStream.Position = 0
Dim tempBuffer(memStream.Length - 1) As Byte
memStream.Read(tempBuffer, 0, tempBuffer.Length)
memStream.Close()
requestStream.Write(tempBuffer, 0, tempBuffer.Length)
requestStream.Close()
Dim httpWebResponse As Net.WebResponse = webRequest.GetResponse
Dim httpStreamReader As new Io.StreamReader(httpWebResponse.GetResponseStream, Encoding.GetEncoding("utf-8"))
responseContent = httpStreamReader.ReadToEnd
fileStream.Close()
httpWebResponse.Close()
webRequest.Abort()
ts(fcn,"seafile_文件上传成功",responseContent)
Return responseContent
Catch ex As Exception
Return Functions.Execute("捕获错误输出",ex,"seafile_文件上传失败")
End Try
'函数调用0===================
'Functions.Execute("seafile_验证token")
'每次seafile_api调用需验证.成功返回token,失败返回错误代码
'引用seafile_token,seafile_url0,seafile_ok
'函数调用1===================
If seafile_ok = False OrElse seafile_token = ""
Return Functions.Execute("seafile_获取token")
Else
Dim url As String = "api2/auth/ping/"
url = seafile_url0 & url
Dim cookieheader As String = "Token " & seafile_token
try
Dim rqst As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(url)
rqst.Headers.Add("Authorization",cookieheader)
Dim rsps As System.Net.HttpWebResponse = rqst.GetResponse
Dim stm As System.IO.Stream = rsps.GetResponseStream()
Dim reader As New System.IO.StreamReader(stm)
Dim str As String = reader.ReadToEnd
rsps.Close
stm.Close
reader.close
If str="""" & "pong" & """"
output.Show("Token验证成功:" & seafile_token)
Return seafile_token
Else
Return Functions.Execute("seafile_获取token")
End If
Catch ex As Exception
Functions.Execute("捕获错误输出",ex,"token验证失败!可能登录超时,将重新登录!")
Return Functions.Execute("seafile_获取token")
End try
End If
Return ""
'函数调用0===================
'Functions.Execute("seafile_获取token")
'成功则返回seafile_token,失败返回"error:***"
'主要用于初始登录验证,并更新seafile_token和seafile_ok
'引用:seafile_user,seafile_pas,seafile_ok,seafile_url0,seafile_token,seafile_ok
'函数调用1===================
Dim url0 As String = seafile_url0
Dim url As String = url0 & "api2/auth-token/"
'msgbox(url)
Dim post As String = "username=" & seafile_user & "&password=" & seafile_pas
Dim msg As String = post
Dim str As String
try
Dim req = System.Net.WebRequest.Create(url)
req.Method = "POST"
req.C
Dim aryBuf As Byte() = Encoding.GetEncoding("utf-8").GetBytes(msg)
req.ContentLength = aryBuf.Length
Dim writer = req.GetRequestStream()
writer.Write(aryBuf, 0, aryBuf.Length)
writer.Close()
writer.Dispose()
Dim pos = req.GetResponse
Dim stm As System.IO.Stream = pos.GetResponseStream()
Dim reader As New System.IO.StreamReader(stm)
str = reader.ReadToEnd
pos.Close
stm.Close
reader.close
Dim data As object = Functions.Execute("解析json数组",str)
Dim token As String = data.token
output.Show("获取token:" & Token)
seafile_token = Token
seafile_ok =True
Return seafile_token
Catch ex As Exception
seafile_ok =False
seafile_token = ""
Return Functions.Execute("捕获错误输出",ex,"seafile登录验证失败!")
End Try