Exemplo n.º 1
0
        /// <summary>
        ///     The method is to convert the file to the required format
        /// </summary>
        /// <param name="documentUri">Uri for the document to convert</param>
        /// <param name="fromExtension">Document extension</param>
        /// <param name="toExtension">Extension to which to convert</param>
        /// <param name="documentRevisionId">Key for caching on service</param>
        /// <param name="isAsync">Perform conversions asynchronously</param>
        /// <param name="convertedDocumentUri">Uri to the converted document</param>
        /// <returns>The percentage of conversion completion</returns>
        /// <example>
        /// string convertedDocumentUri;
        /// GetConvertedUri("http://helpcenter.onlyoffice.com/content/GettingStarted.pdf", ".pdf", ".docx", "http://helpcenter.onlyoffice.com/content/GettingStarted.pdf", false, out convertedDocumentUri);
        /// </example>
        /// <exception>
        /// </exception>
        public static int GetConvertedUri(string documentUri,
                                          string fromExtension,
                                          string toExtension,
                                          string documentRevisionId,
                                          bool isAsync,
                                          out string convertedDocumentUri,
                                          string filePass = null,
                                          string lang     = null)
        {
            convertedDocumentUri = string.Empty;

            // check if the fromExtension parameter is defined; if not, get it from the document url
            fromExtension = string.IsNullOrEmpty(fromExtension) ? Path.GetExtension(documentUri).ToLower() : fromExtension;

            // check if the file name parameter is defined; if not, get random uuid for this file
            var title = Path.GetFileName(documentUri);

            title = string.IsNullOrEmpty(title) ? Guid.NewGuid().ToString() : title;

            // get document key
            documentRevisionId = string.IsNullOrEmpty(documentRevisionId)
                                     ? documentUri
                                     : documentRevisionId;
            documentRevisionId = GenerateRevisionId(documentRevisionId);

            // specify request parameters
            var request = (HttpWebRequest)WebRequest.Create(DocumentConverterUrl);

            request.Method      = "POST";
            request.ContentType = "application/json";
            request.Accept      = "application/json";
            request.Timeout     = ConvertTimeout;

            // write all the necessary parameters to the body object
            var body = new Dictionary <string, object>()
            {
                { "async", isAsync },
                { "filetype", fromExtension.Trim('.') },
                { "key", documentRevisionId },
                { "outputtype", toExtension.Trim('.') },
                { "title", title },
                { "url", documentUri },
                { "password", filePass },
                { "region", lang }
            };

            if (JwtManager.Enabled)
            {
                // create payload object
                var payload = new Dictionary <string, object>
                {
                    { "payload", body }
                };

                var payloadToken = JwtManager.Encode(payload); // encode the payload object to the payload token
                var bodyToken    = JwtManager.Encode(body);    // encode the body object to the body token
                // create header token
                string JWTheader = WebConfigurationManager.AppSettings["files.docservice.header"].Equals("") ? "Authorization" : WebConfigurationManager.AppSettings["files.docservice.header"];
                request.Headers.Add(JWTheader, "Bearer " + payloadToken);

                body.Add("token", bodyToken);
            }

            var bytes = Encoding.UTF8.GetBytes(new JavaScriptSerializer().Serialize(body));

            request.ContentLength = bytes.Length;
            using (var requestStream = request.GetRequestStream()) // get the request stream
            {
                requestStream.Write(bytes, 0, bytes.Length);       // and write the serialized body object to it
            }

            DocManagerHelper.VerifySSL();

            string dataResponse;

            using (var response = request.GetResponse())
                using (var stream = response.GetResponseStream()) // get the response stream
                {
                    if (stream == null)
                    {
                        throw new Exception("Response is null");
                    }

                    using (var reader = new StreamReader(stream))
                    {
                        dataResponse = reader.ReadToEnd(); // and read it
                    }
                }

            return(GetResponseUri(dataResponse, out convertedDocumentUri));
        }
Exemplo n.º 2
0
        // create a command request
        public static void commandRequest(string method, string key, object meta = null)
        {
            DocManagerHelper.VerifySSL();

            string documentCommandUrl = WebConfigurationManager.AppSettings["files.docservice.url.site"] + WebConfigurationManager.AppSettings["files.docservice.url.command"];

            var request = (HttpWebRequest)WebRequest.Create(documentCommandUrl);

            request.Method      = "POST";
            request.ContentType = "application/json";

            var body = new Dictionary <string, object>()
            {
                { "c", method },
                { "key", key }
            };

            if (meta != null)
            {
                body.Add("meta", meta);
            }

            // check if a secret key to generate token exists or not
            if (JwtManager.Enabled)
            {
                var payload = new Dictionary <string, object>
                {
                    { "payload", body }
                };

                var    payloadToken = JwtManager.Encode(payload);         // encode a payload object into a header token
                var    bodyToken    = JwtManager.Encode(body);            // encode body into a body token
                string JWTheader    = WebConfigurationManager.AppSettings["files.docservice.header"].Equals("") ? "Authorization" : WebConfigurationManager.AppSettings["files.docservice.header"];
                request.Headers.Add(JWTheader, "Bearer " + payloadToken); // add a header Authorization with a header token and Authorization prefix in it

                body.Add("token", bodyToken);
            }

            var bytes = Encoding.UTF8.GetBytes(new JavaScriptSerializer().Serialize(body));

            request.ContentLength = bytes.Length;
            using (var requestStream = request.GetRequestStream())
            {
                // write bytes to the output stream
                requestStream.Write(bytes, 0, bytes.Length);
            }

            string dataResponse;

            using (var response = request.GetResponse())  // get the response
                using (var stream = response.GetResponseStream())
                {
                    if (stream == null)
                    {
                        throw new Exception("Response is null");
                    }

                    using (var reader = new StreamReader(stream))
                    {
                        dataResponse = reader.ReadToEnd(); // and read it
                    }
                }

            // convert stream to json string
            var jss         = new JavaScriptSerializer();
            var responseObj = jss.Deserialize <Dictionary <string, object> >(dataResponse);

            if (!responseObj["error"].ToString().Equals("0"))
            {
                throw new Exception(dataResponse);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///     The method is to convert the file to the required format
        /// </summary>
        /// <param name="documentUri">Uri for the document to convert</param>
        /// <param name="fromExtension">Document extension</param>
        /// <param name="toExtension">Extension to which to convert</param>
        /// <param name="documentRevisionId">Key for caching on service</param>
        /// <param name="isAsync">Perform conversions asynchronously</param>
        /// <param name="convertedDocumentUri">Uri to the converted document</param>
        /// <returns>The percentage of completion of conversion</returns>
        /// <example>
        /// string convertedDocumentUri;
        /// GetConvertedUri("http://helpcenter.onlyoffice.com/content/GettingStarted.pdf", ".pdf", ".docx", "http://helpcenter.onlyoffice.com/content/GettingStarted.pdf", false, out convertedDocumentUri);
        /// </example>
        /// <exception>
        /// </exception>
        public static int GetConvertedUri(string documentUri,
                                          string fromExtension,
                                          string toExtension,
                                          string documentRevisionId,
                                          bool isAsync,
                                          out string convertedDocumentUri)
        {
            convertedDocumentUri = string.Empty;

            fromExtension = string.IsNullOrEmpty(fromExtension) ? Path.GetExtension(documentUri) : fromExtension;

            var title = Path.GetFileName(documentUri);

            title = string.IsNullOrEmpty(title) ? Guid.NewGuid().ToString() : title;

            documentRevisionId = string.IsNullOrEmpty(documentRevisionId)
                                     ? documentUri
                                     : documentRevisionId;
            documentRevisionId = GenerateRevisionId(documentRevisionId);

            var request = (HttpWebRequest)WebRequest.Create(DocumentConverterUrl);

            request.Method      = "POST";
            request.ContentType = "application/json";
            request.Accept      = "application/json";
            request.Timeout     = ConvertTimeout;

            var body = new Dictionary <string, object>()
            {
                { "async", isAsync },
                { "filetype", fromExtension.Trim('.') },
                { "key", documentRevisionId },
                { "outputtype", toExtension.Trim('.') },
                { "title", title },
                { "url", documentUri }
            };

            if (JwtManager.Enabled)
            {
                var payload = new Dictionary <string, object>
                {
                    { "payload", body }
                };

                var    payloadToken = JwtManager.Encode(payload);
                var    bodyToken    = JwtManager.Encode(body);
                string JWTheader    = WebConfigurationManager.AppSettings["files.docservice.header"].Equals("") ? "Authorization" : WebConfigurationManager.AppSettings["files.docservice.header"];
                request.Headers.Add(JWTheader, "Bearer " + payloadToken);

                body.Add("token", bodyToken);
            }

            var bytes = Encoding.UTF8.GetBytes(new JavaScriptSerializer().Serialize(body));

            request.ContentLength = bytes.Length;
            using (var requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
            }

            string dataResponse;

            using (var response = request.GetResponse())
                using (var stream = response.GetResponseStream())
                {
                    if (stream == null)
                    {
                        throw new Exception("Response is null");
                    }

                    using (var reader = new StreamReader(stream))
                    {
                        dataResponse = reader.ReadToEnd();
                    }
                }

            return(GetResponseUri(dataResponse, out convertedDocumentUri));
        }
        public static void commandRequest(string method, string key)
        {
            string documentCommandUrl = WebConfigurationManager.AppSettings["files.docservice.url.site"] + WebConfigurationManager.AppSettings["files.docservice.url.command"];

            var request = (HttpWebRequest)WebRequest.Create(documentCommandUrl);

            request.Method      = "POST";
            request.ContentType = "application/json";

            var body = new Dictionary <string, object>()
            {
                { "c", method },
                { "key", key }
            };

            if (JwtManager.Enabled)
            {
                var payload = new Dictionary <string, object>
                {
                    { "payload", body }
                };

                var    payloadToken = JwtManager.Encode(payload);
                var    bodyToken    = JwtManager.Encode(body);
                string JWTheader    = WebConfigurationManager.AppSettings["files.docservice.header"].Equals("") ? "Authorization" : WebConfigurationManager.AppSettings["files.docservice.header"];
                request.Headers.Add(JWTheader, "Bearer " + payloadToken);

                body.Add("token", bodyToken);
            }

            var bytes = Encoding.UTF8.GetBytes(new JavaScriptSerializer().Serialize(body));

            request.ContentLength = bytes.Length;
            using (var requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);
            }

            string dataResponse;

            using (var response = request.GetResponse())
                using (var stream = response.GetResponseStream())
                {
                    if (stream == null)
                    {
                        throw new Exception("Response is null");
                    }

                    using (var reader = new StreamReader(stream))
                    {
                        dataResponse = reader.ReadToEnd();
                    }
                }

            var jss         = new JavaScriptSerializer();
            var responseObj = jss.Deserialize <Dictionary <string, object> >(dataResponse);

            if (!responseObj["error"].ToString().Equals("0"))
            {
                throw new Exception(dataResponse);
            }
        }