標籤:
最近開始學習.Net Core,並使用Visual Studio Code工具來開發。感覺開發起來特別的方便,但是有個頭疼的地方:許多的類庫被修改了,一時半會兒還熟悉不了,需要查閱官方API。。。
Microsoft Office Web Apps(以下簡稱OWA)是由微軟推出的基於Web端的線上辦公工具,它將Microsoft Office產品的體驗延伸到可支援的瀏覽器上。OWA讓你可以在任何地方共用自己的Office文檔。
系統整合OWA需要參考官方的系統介面定義(https://wopi.readthedocs.io/en/latest/),這個介面簡稱WOPI(Web Application Open Platform Interface)。
下面介紹一個完整的OWA的例子,最終效果
wopi協議的工作流程如
我們所要做的就是開發一個OWA用戶端,提供檔案資訊及檔案流給OWA伺服器,當然也接收從OWA服務POST來的檔案流來儲存檔案,出於安全考慮也可以加上access_token進行自訂許可權驗證。
標準的WOPI服務包括:CheckFileInfo、GetFile、Lock、GetLock、RefreshLock、Unlock、UnlockAndRelock、PutFile、PutRelativeFile、RenameFile、DeleteFile、PutUserInfo等
返回狀態代碼定義:
200 OK 成功
400 Bad Request 錯誤請求
401 Unauthorized 非法 與access_token相關
409 Conflict 衝突 目標檔案已經存在或LOCK
413 Request Entity Too Large 檔案太大
500 Internal Server Error 內部伺服器錯誤
501 Not Implemented 不支援,如果CheckFileInfo的SupportsUpdate和UserCanNotWriteRelative都設定為true,則必須返回501
OwaFileInfo類
需要定義一個檔案資訊類,該類的主要屬性包括:
屬性名稱 |
類型 |
描述 |
BaseFileName |
String |
包含擴充的檔案名稱 |
BreadcrumbFolderName |
String |
檔案夾名稱(介面顯示) |
BreadcrumbDocName |
String |
文檔名稱(介面顯示) |
OwnerId |
String |
唯一標識檔案所有者 |
Size |
Long |
檔案大小 |
SHA256 |
String |
SHA-2 256位散列編碼值 |
Version |
String |
版本編號 |
SupportsUpdate |
Bool |
是否支援PUT檔案 |
UserCanWrite |
Bool |
是否有許可權修改 |
SupportsLocks |
Bool |
是否支援Lock和Unlock |
CloseButtonClosesWindow |
Bool |
是否顯示關閉按鈕 |
更多屬性參考API https://wopirest.readthedocs.io/en/latest/files/CheckFileInfo.html |
屬性名稱必須與API中一致才能被OWA伺服器識別。
using System;using System.Runtime.Serialization;namespace WebApplication.Models.FileInfoModels{ [DataContract(Name = "OwaFileInfo")] public class OwaFileInfo { public OwaFileInfo() { this.SupportsUpdate = false; this.UserCanWrite = false; this.SupportsLocks = false; } [DataMember(Name = "BaseFileName")] public string BaseFileName { get; set; } [DataMember(Name = "OwnerId")] public string OwnerId { get; set; } [DataMember(Name = "Size")] public long Size { get; set; } [DataMember(Name = "SHA256")] public string SHA256 { get; set; } [DataMember(Name = "Version")] public string Version { get; set; } [DataMember(Name = "SupportsUpdate")] public bool SupportsUpdate { get; set; } [DataMember(Name = "UserCanWrite")] public bool UserCanWrite { get; set; } [DataMember(Name = "SupportsLocks")] public bool SupportsLocks { get; set; } [DataMember(Name = "BreadcrumbDocName")] public string BreadcrumbDocName { get; set; } [DataMember(Name = "CloseButtonClosesWindow")] public bool CloseButtonClosesWindow { get; set; } [DataMember(Name = "BreadcrumbFolderName")] public string BreadcrumbFolderName { get; set; } }}OwaFileInfoCheckFileInfo服務
介面要求實現CheckFileInfo服務,作用是OWA伺服器需要擷取檔案的詳細資料和操作許可權(如:是否可編輯),以確保檔案的真實有效。這些資訊已經在上面的類中進行了定義。
Method:GET
URI:HTTP://server/<...>/wopi*/files/<id>
Request Headers:
X-WOPI-SessionContext 上下文session參數值
該介面需要返回的json格式如:
{"BaseFileName":"test.docx","OwnerId":"admin","Size":798,"SHA256":"wlbRK+XNdLtHNaOcXnejbIVzHPHAZzI+1MhKNHUCVlw=","Version":"2016-03-17T02:27:33","SupportsUpdate":true,"UserCanWrite":true,"SupportsLocks":true,"WebEditingDisabled":false}
說明:
0.返回的屬性名稱區分大小寫,很多外掛程式預設將頭字母轉換成小寫,最終導致對接失敗。
1.所有的OWA用戶端介面URl必須以/wopi開頭
如:
http://localhost:5000/api/wopi/files/test.docx
http://localhost:5000/api/wopi_test/files/test.docx
2. SHA256的計算
a.擷取該檔案的檔案流
b SHA256計算檔案流Hash值
c.將Hash值轉換為Base64String
string sha256 = "";using (FileStream stream = File.OpenRead(fileName))using (var sha = SHA256.Create()){ byte[] checksum = sha.ComputeHash(stream); sha256 = Convert.ToBase64String(checksum);}
3.驗證action返回的結果,訪問http://localhost:5000/api/wopi/files/test.docx,返回結果如
GetFile服務
當office web apps檢驗完檔案資訊後就可以擷取檔案了
Method:GET
URI: HTTP://server/<...>/wopi*/files/<id>/contents?access_token=<token>
Request Headers:
X-WOPI-MaxExpectedSize
Response Headers:
X-WOPI-ItemVersion 檔案版本號碼類似於CheckFileInfo裡的Version
返迴文件的二進位流
[Route("files/{name}/contents")][HttpGetAttribute]public FileStreamResult Get(string name, string access_token){ var file = "Files/" + name; var stream = new FileStream(file, FileMode.Open, FileAccess.Read); return new FileStreamResult(stream, "application/octet-stream");}PutFile服務
Method:POST
URI: HTTP://server/<...>/wopi*/files/<id>/contents?access_token=<token>
Request Headers:
X-WOPI-Override 固定值 PUT,必須
X-WOPI-Lock 鎖定請求的字串標識(具體參考LOCK請求)
Response Headers:
X-WOPI-Lock鎖定請求的字串標識
X-WOPI-LockFailureReason 鎖定失敗原因
X-WOPI-ItemVersion 版本號碼
請求內容為檔案的二進位格式
[Route("files/{name}/contents")][HttpPostAttribute]public async void Post(string name, string access_token){ using (FileStream fs = System.IO.File.Create("Files/" + name)) { await Request.Body.CopyToAsync(fs); }}
至此基本的Office線上預覽功能就基本完成了
後續工作:
1.規範請求和返回的頭資訊,完善功能實現線上編輯
2.引入設定檔和緩衝配置資料
3.添加預覽和編輯連結產生controller
4.完善access_token驗證邏輯
.Net Core整合Office Web Apps(一)