Skip to content
Snippets Groups Projects
wopiapi-iframe.js 2.23 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexey Lunin's avatar
    Alexey Lunin committed
    const axios = require("axios");
    
    function WopiAPI() {}
    
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    WopiAPI.prototype.getPassportsNewProtocol = function(resourceID, contentType) {
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      const {
        publicKey,
        uuid,
        token,
        deviceHash
      } = window.viamApi.getConfig().headers;
    
      const requestConfig = {
    
    Gospodin Bodurov's avatar
    Gospodin Bodurov committed
        url: `${window.WOPI_URL}getPassportsNewProtocol`,
    
    Alexey Lunin's avatar
    Alexey Lunin committed
        method: "POST",
    
        headers: {
    
          publicKey,
          uuid,
          token,
          deviceHash,
    
          resourceID: encodeURI(resourceID),
          contentType: encodeURI(contentType)
    
    Markin Igor's avatar
    Markin Igor committed
      return axios(requestConfig);
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    WopiAPI.prototype.getPassports = function(fileID) {
      const {
        publicKey,
        uuid,
        token,
        deviceHash
      } = window.viamApi.getConfig().headers;
    
      const requestConfig = {
        url: `${window.WOPI_URL}getPassports`,
    
    Alexey Lunin's avatar
    Alexey Lunin committed
        method: "POST",
    
        headers: {
          publicKey,
          uuid,
          token,
          deviceHash,
          fileID: encodeURI(fileID)
        }
      };
    
      return axios(requestConfig);
    };
    
    
    // contentType is optional.
    // When not specified, function returns token, which can write multiple files and only read the last one.
    // When specified, function returns read-write token for the specified contentType.
    WopiAPI.prototype.getAccessToken = function(passportUUID, resourceID, contentType = "") {
      const {
        publicKey,
        uuid,
        token,
        deviceHash
      } = window.viamApi.getConfig().headers;
    
      if (!contentType) {
        contentType = "";
      }
    
      const requestConfig = {
        url: `${window.WOPI_URL}getAccessToken`,
        method: "POST",
        headers: {
          publicKey,
          uuid,
          token,
          deviceHash,
          passportUUID: encodeURI(passportUUID),
          resourceID: encodeURI(resourceID),
          contentType: encodeURI(contentType)
        }
      };
    
      return axios(requestConfig);
    }
    
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    WopiAPI.prototype.putDocument = function(resourceID, accessToken, file) {
      const {
        publicKey,
        uuid,
        token,
        deviceHash
      } = window.viamApi.getConfig().headers;
    
    
      resourceID = encodeURI(resourceID);
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      const requestConfig = {
    
        url: `${window.WOPI_URL}files/${resourceID}/contents`,
    
    Alexey Lunin's avatar
    Alexey Lunin committed
        method: "POST",
    
    Alexey Lunin's avatar
    Alexey Lunin committed
        headers: {
          publicKey,
          uuid,
          token,
          deviceHash
        },
        params: {
          access_token: accessToken
        },
        data: file
      };
    
      return axios(requestConfig);
    };
    
    
    module.exports = WopiAPI;