📜 ⬆️ ⬇️

How to make online viewing of documents

First work day. The first task in Redmine. The first specification is in doc format. On the new working machine. It was possible to start reading the specification in about 3 hours. While MS Office was downloaded and installed. Recalling this case, I was sure that in our task management system it is necessary to make online viewing of documents. Here are just ideas for the implementation of a reasonable time and labor costs were not. We recently found a way - Microsoft Office Web Apps.

This article will discuss how to add online viewing of documents in any product.

General view


Office Web Apps Server (hereinafter referred to as OWA) is a web version of the popular office suite of applications that includes lightweight browser applications Word, Excel, PowerPoint and OneNote.

Available for free download and use in document viewing mode.
')
You can watch OWA in action on OneDrive . Several documents in read mode:


OWA is placed on a separate physical server and is only responsible for displaying documents in the browser. All the logic of organizing file storage falls on your application. The interaction between OWA and the repository takes place through the WOPI API, which will have to be implemented.
image


It is installed using the wizard and the next button.
After successful installation at the address HTTPS: // OWA-server / hosting / discovery there will be an xml-description of supported applications, actions and access url-templates:

<?xml version="1.0" encoding="utf-8"?> <wopi-discovery> <net-zone name="external-http"> <app name="Excel" favIconUrl="https://OWA-server/x/_layouts/images/FavIcon_Excel.ico" checkLicense="true"> ... </app> <app name="OneNote" favIconUrl="https://OWA-server/o/resources/1033/FavIcon_OneNote.ico" checkLicense="true"> ... </app> <app name="PowerPoint" favIconUrl="https://OWA-server/p/resources/1033/FavIcon_Ppt.ico" checkLicense="true"> ... </app> <app name="Word" favIconUrl="https://OWA-server/wv/resources/1033/FavIcon_Word.ico" checkLicense="true"> ... <action name="view" ext="docx" default="true" urlsrc="https://OWA-server/wv/wordviewerframe.aspx?<ui=UI_LLCC&><rs=DC_LLCC&><showpagestats=PERFSTATS&>" /> ... <action name="view" ext="odt" default="true" urlsrc="https://OWA-server/wv/wordviewerframe.aspx?<ui=UI_LLCC&><rs=DC_LLCC&><showpagestats=PERFSTATS&>" /> ... <action name="edit" ext="docx" requires="locks,cobalt,update" urlsrc="https://OWA-server/we/wordeditorframe.aspx?<ui=UI_LLCC&><rs=DC_LLCC&><showpagestats=PERFSTATS&>" /> ... <action name="editnew" ext="docx" requires="locks,cobalt,update" urlsrc="https://OWA-server/we/wordeditorframe.aspx?new=1&<ui=UI_LLCC&><rs=DC_LLCC&><showpagestats=PERFSTATS&>" /> ... <action name="imagepreview" ext="doc" urlsrc="https://OWA-server/wv/WordPreviewHandler.ashx?<ui=UI_LLCC&><rs=DC_LLCC&><showpagestats=PERFSTATS&>" /> <action name="interactivepreview" ext="doc" urlsrc="https://OWA-server/wv/wordviewerframe.aspx?embed=1&<ui=UI_LLCC&><rs=DC_LLCC&><showpagestats=PERFSTATS&>" /> ... <action name="mobileView" ext="doc" urlsrc="https://OWA-server/wv/wordviewerframe.aspx?<ui=UI_LLCC&><rs=DC_LLCC&><showpagestats=PERFSTATS&>" /> ... <action name="embedview" ext="doc" urlsrc="https://OWA-server/wv/wordviewerframe.aspx?embed=1&<ui=UI_LLCC&><rs=DC_LLCC&><showpagestats=PERFSTATS&>" /> ... </app> <app name="WordPdf" favIconUrl="https://OWA-server/wv/resources/1033/FavIcon_Word.ico" checkLicense="true"> ... </app> </net-zone> <proof-key oldvalue="" value="..." /> </wopi-discovery> 

There are many hidden behind dots. But for the remaining actions, it is clear that both documents in Microsoft Office format and Open Office are supported. In addition to viewing (view), you can open documents for editing (edit). There is a mobile version (mobileView). OWA can generate a preview of the first page of a document as an image (imagepreview) and a preview of all pages of a document (interactivepreview).

Suppose we want to display in the Word browser a document with the extension docx. Find the url template:
HTTPS: //OWA-server/wv/wordviewerframe.aspx? <Ui = UI_LLCC &> <rs = DC_LLCC &> <showpagestats = PERFSTATS &>
The optional parameters are enclosed in angle brackets (<>):


To this url, we need to add the WOPISrc parameter, through which OWA will request a document from our repository, and the access_token parameter, which will be sent along with the file request for authorization.

The result will be a URL like HTTPS: //OWA-server/wv/wordviewerframe.aspx? WOPISrc = https% 3A% 2F% 2Fmy-wopi-host% 2Fwopi% 2Ffiles% 2FDocument.docx & access_token = 0bf6fe96-3510-4105-a-ct-ac4ct4ct4ct4ct4fc. (*)

WOPI


WOPI (Web Application Open Platform Interface) is a RESTful API that defines a set of operations for accessing and modifying files in our file storage. Works through HTTP / HTTPS.

I prepared an example of the simplest file repository with the minimum required for viewing documents from WOPI operations. Project on ASP.Net MVC 5. But nothing prevents to use any other framework and programming language.

Open the URL (*) we received a little earlier in the browser.

First, OWA will call the CheckFileInfo method available at http: // server / <...> / wopi * / files / <id>? Access_token = <token>.
Method implementation:
 [Route("files/{fileName}")] [HttpGet] public FileInfoDto CheckFileInfo(string fileName, [FromUri(Name = "access_token")] Guid tokenId) { var fullFileName = GetFullPath(fileName); Validation(tokenId, fullFileName); return new FileInfoDto { BaseFileName = fileName, OwnerId = "admin", ReadOnly = true, SHA256 = GetChecksum(fullFileName), Size = new FileInfo(fullFileName).Length, Version = 1 }; } private static string GetChecksum(string filePath) { using (var stream = new BufferedStream(File.OpenRead(filePath), 1200000)) { var checksum = SHA256.Create().ComputeHash(stream); return Convert.ToBase64String(checksum); } } 


Only required parameters are returned here. This is file information. A number of optional parameters describe, for example, whether it is possible to edit a file and how, information about the author, support for working with folders, information about user rights, whether it is necessary to protect the file from being copied, and so on. All configs are described here .

If the method has successfully completed and returned the correct data, OWA will request the file itself at the address HTTP: // server / <...> / wopi * / files / <id> / contents? Access_token = <token>.
Give the file:
 [Route("wopi/files/{fileName}/contents")] [HttpGet] public HttpResponseMessage GetFile(string fileName, [FromUri(Name = "access_token")] Guid tokenId) { var fullFileName = GetFullPath(fileName); Validation(tokenId, fullFileName); var stream = new FileStream(fullFileName, FileMode.Open); var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(stream) }; result.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(fileName)); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName }; return result; } 


Actually everything, we wrote the necessary functionality to display the files!

Iron


The most narrow and costly place in this whole idea.

Minimum system requirements:


It is claimed that the 8th nuclear server with 8 GB of RAM can handle 10,000 users. A 16 and nuclear with 16 GB of RAM - 20 000 users.

Supported operating systems:


It is placed on a separate physical or virtual server. However, other server applications (for example, MS SQL Server) cannot be installed on the same server.

The server must be part of the domain , otherwise OWA just will not install, I tried. Also, OWA will not work if you install it on a domain controller.

Is it possible to edit documents in the browser?


OWA supports editing, but you can only view documents for free. Editing requires a license .

By default, the edit mode is turned off. But you can turn it on at any time. Editing PowerPoint and Excel documents works through WOPI. But for Word you have to implement FSSHTTP .

Source: https://habr.com/ru/post/223179/


All Articles