Skip to content

Commit

Permalink
Merge pull request #346 from ONLYOFFICE/feature/referenceData
Browse files Browse the repository at this point in the history
Feature/reference data
  • Loading branch information
LinneyS authored Mar 2, 2023
2 parents 251f48e + 4d4951f commit 9c88f86
Show file tree
Hide file tree
Showing 26 changed files with 732 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Change Log

- referenceData
- anonymous can't protect file
- separate setting for checking the token in requests
- php: linter refactoring
Expand Down
11 changes: 11 additions & 0 deletions web/documentserver-example/csharp-mvc/Models/FileModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ public string GetDocConfig(HttpRequest request, UrlHelper url)
{ "favorite", favorite}
}
},
{
"referenceData", new Dictionary<string, string>()
{
{ "fileKey", !user.id.Equals("uid-0") ?
jss.Serialize(new Dictionary<string, object>{
{"fileName", FileName},
{"userAddress", HttpUtility.UrlEncode(DocManagerHelper.CurUserHostAddress(HttpContext.Current.Request.UserHostAddress))}
}) : null },
{"instanceId", DocManagerHelper.GetServerUrl(false) }
}
},
{
// the permission for the document to be edited and downloaded or not
"permissions", new Dictionary<string, object>
Expand Down
13 changes: 13 additions & 0 deletions web/documentserver-example/csharp-mvc/Views/Home/Editor.aspx
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@
}
};
var onRequestReferenceData = function (event) { // user refresh external data source
event.data.directUrl = !!config.document.directUrl;
let xhr = new XMLHttpRequest();
xhr.open("POST", "webeditor.ashx?type=reference");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(event.data));
xhr.onload = function () {
console.log(xhr.responseText);
docEditor.setReferenceData(JSON.parse(xhr.responseText));
}
};
config = <%= Model.GetDocConfig(Request, Url) %>;
config.width = "100%";
Expand Down Expand Up @@ -239,6 +251,7 @@
};
// prevent file renaming for anonymous users
config.events['onRequestRename'] = onRequestRename;
config.events['onRequestReferenceData'] = onRequestReferenceData;
}
if (config.editorConfig.createUrl) {
Expand Down
94 changes: 94 additions & 0 deletions web/documentserver-example/csharp-mvc/WebEditor.ashx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public void ProcessRequest(HttpContext context)
case "rename":
Rename(context);
break;
case "reference":
Reference(context);
break;
}
}

Expand Down Expand Up @@ -591,5 +594,96 @@ private static void Rename(HttpContext context)
TrackManager.commandRequest("meta", docKey, meta);
context.Response.Write("{ \"result\": \"OK\"}");
}

private static void Reference(HttpContext context)
{
string fileData;
try
{
using (var receiveStream = context.Request.InputStream)
using (var readStream = new StreamReader(receiveStream))
{
fileData = readStream.ReadToEnd();
if (string.IsNullOrEmpty(fileData)) return;
}
}
catch (Exception e)
{
throw new HttpException((int)HttpStatusCode.BadRequest, e.Message);
}

var jss = new JavaScriptSerializer();
var body = jss.Deserialize<Dictionary<string, object>>(fileData);
Dictionary<string, object> referenceData = null;
var fileName = "";
var userAddress = "";

if (body.ContainsKey("referenceData"))
{
referenceData = jss.Deserialize<Dictionary<string, object>>(jss.Serialize(body["referenceData"]));
var instanceId = (string)referenceData["instanceId"];
var fileKey = (string)referenceData["fileKey"];
if (instanceId == DocManagerHelper.GetServerUrl(false))
{
var fileKeyObj = jss.Deserialize<Dictionary<string, object>>(fileKey);
userAddress = (string)fileKeyObj["userAddress"];
if (userAddress == HttpUtility.UrlEncode(DocManagerHelper.CurUserHostAddress(HttpContext.Current.Request.UserHostAddress)))
{
fileName = (string)fileKeyObj["fileName"];
}
}
}

if (fileName == "")
{
try
{
var path = (string)body["path"];
path = Path.GetFileName(path);
if (File.Exists(DocManagerHelper.StoragePath(path, null)))
{
fileName = path;
}
}
catch
{
context.Response.Write("{ \"error\": \"Path not found!\"}");
return;
}
}

if (fileName == "")
{
context.Response.Write("{ \"error\": \"File not found!\"}");
return;
}

var data = new Dictionary<string, object>() {
{ "fileType", (Path.GetExtension(fileName) ?? "").ToLower() },
{ "url", DocManagerHelper.GetDownloadUrl(fileName)},
{ "directUrl", DocManagerHelper.GetDownloadUrl(fileName) },
{ "referenceData", new Dictionary<string, string>()
{
{ "fileKey", jss.Serialize(new Dictionary<string, object>{
{"fileName", fileName},
{"userAddress", HttpUtility.UrlEncode(DocManagerHelper.CurUserHostAddress(HttpContext.Current.Request.UserHostAddress))}
})
},
{"instanceId", DocManagerHelper.GetServerUrl(false) }
}
},
{ "path", fileName }
};

if (JwtManager.Enabled)
{
var token = JwtManager.Encode(data);
data.Add("token", token);
}

context.Response.Write(jss.Serialize(data));
}

}

}
14 changes: 14 additions & 0 deletions web/documentserver-example/csharp/DocEditor.aspx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@
}
};
var onRequestReferenceData = function (event) { // user refresh external data source
event.data.directUrl = !!config.document.directUrl;
let xhr = new XMLHttpRequest();
xhr.open("POST", "webeditor.ashx?type=reference");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(event.data));
xhr.onload = function () {
console.log(xhr.responseText);
docEditor.setReferenceData(JSON.parse(xhr.responseText));
}
};
config = <%= DocConfig %>;
config.width = "100%";
Expand Down Expand Up @@ -242,6 +255,7 @@
};
// prevent file renaming for anonymous users
config.events['onRequestRename'] = onRequestRename;
config.events['onRequestReferenceData'] = onRequestReferenceData;
}
if (config.editorConfig.createUrl) {
Expand Down
11 changes: 11 additions & 0 deletions web/documentserver-example/csharp/DocEditor.aspx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ protected void Page_Load(object sender, EventArgs e)
{ "favorite", favorite }
}
},
{
"referenceData", new Dictionary<string, string>()
{
{ "fileKey", !user.id.Equals("uid-0") ?
jss.Serialize(new Dictionary<string, object>{
{"fileName", FileName},
{"userAddress", HttpUtility.UrlEncode(_Default.CurUserHostAddress(HttpContext.Current.Request.UserHostAddress))}
}) : null },
{"instanceId", _Default.GetServerUrl(false) }
}
},
{
// the permission for the document to be edited and downloaded or not
"permissions", new Dictionary<string, object>
Expand Down
93 changes: 93 additions & 0 deletions web/documentserver-example/csharp/WebEditor.ashx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Web.Configuration;
using System.Linq;
using System.Net;
using System.Net.Sockets;

namespace OnlineEditorsExample
{
Expand Down Expand Up @@ -71,6 +72,9 @@ public void ProcessRequest(HttpContext context)
case "rename":
Rename(context);
break;
case "reference":
Reference(context);
break;
}
}

Expand Down Expand Up @@ -409,5 +413,94 @@ private static void Rename(HttpContext context)
TrackManager.commandRequest("meta", docKey, meta);
context.Response.Write("{ \"result\": \"OK\"}");
}

private static void Reference(HttpContext context)
{
string fileData;
try
{
using (var receiveStream = context.Request.InputStream)
using (var readStream = new StreamReader(receiveStream))
{
fileData = readStream.ReadToEnd();
if (string.IsNullOrEmpty(fileData)) return;
}
}
catch (Exception e)
{
throw new HttpException((int)HttpStatusCode.BadRequest, e.Message);
}

var jss = new JavaScriptSerializer();
var body = jss.Deserialize<Dictionary<string, object>>(fileData);
Dictionary<string, object> referenceData = null;
var fileName = "";
var userAddress = "";

if (body.ContainsKey("referenceData"))
{
referenceData = jss.Deserialize<Dictionary<string, object>>(jss.Serialize(body["referenceData"]));
var instanceId = (string)referenceData["instanceId"];
var fileKey = (string)referenceData["fileKey"];
if (instanceId == _Default.GetServerUrl(false))
{
var fileKeyObj = jss.Deserialize<Dictionary<string, object>>(fileKey);
userAddress = (string)fileKeyObj["userAddress"];
if (userAddress == HttpUtility.UrlEncode(_Default.CurUserHostAddress(HttpContext.Current.Request.UserHostAddress)))
{
fileName = (string)fileKeyObj["fileName"];
}
}
}

if (fileName == "")
{
try
{
var path = (string)body["path"];
path = Path.GetFileName(path);
if (File.Exists(_Default.StoragePath(path, null)))
{
fileName = path;
}
}
catch
{
context.Response.Write("{ \"error\": \"Path not found!\"}");
return;
}
}

if (fileName == "")
{
context.Response.Write("{ \"error\": \"File not found!\"}");
return;
}

var data = new Dictionary<string, object>() {
{ "fileType", (Path.GetExtension(fileName) ?? "").ToLower() },
{ "url", DocEditor.getDownloadUrl(fileName)},
{ "directUrl", DocEditor.getDownloadUrl(fileName) },
{ "referenceData", new Dictionary<string, string>()
{
{ "fileKey", jss.Serialize(new Dictionary<string, object>{
{"fileName", fileName},
{"userAddress", HttpUtility.UrlEncode(_Default.CurUserHostAddress(HttpContext.Current.Request.UserHostAddress))}
})
},
{"instanceId", _Default.GetServerUrl(false) }
}
},
{ "path", fileName }
};

if (JwtManager.Enabled)
{
var token = JwtManager.Encode(data);
data.Add("token", token);
}

context.Response.Write(jss.Serialize(data));
}
}
}
Loading

0 comments on commit 9c88f86

Please sign in to comment.