public static string DoConvert(HttpContext context)
        {
            _fileName = context.Request["filename"];

            var extension         = (Path.GetExtension(_fileName) ?? "").Trim('.');
            var internalExtension = FileType.GetInternalExtension(_fileName).Trim('.');

            if (ConvertExts.Contains("." + extension) &&
                !string.IsNullOrEmpty(internalExtension))
            {
                var key = ServiceConverter.GenerateRevisionId(FileUri(_fileName));

                string newFileUri;
                var    result = ServiceConverter.GetConvertedUri(FileUri(_fileName), extension, internalExtension, key, true, out newFileUri);
                if (result != 100)
                {
                    return("{ \"step\" : \"" + result + "\", \"filename\" : \"" + _fileName + "\"}");
                }

                var fileName = GetCorrectName(Path.GetFileNameWithoutExtension(_fileName) + "." + internalExtension);

                var req = (HttpWebRequest)WebRequest.Create(newFileUri);

                // hack. http://ubuntuforums.org/showthread.php?t=1841740
                if (IsMono)
                {
                    ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true;
                }

                using (var stream = req.GetResponse().GetResponseStream())
                {
                    if (stream == null)
                    {
                        throw new Exception("Stream is null");
                    }
                    const int bufferSize = 4096;

                    using (var fs = File.Open(StoragePath(fileName, null), FileMode.Create))
                    {
                        var buffer = new byte[bufferSize];
                        int readed;
                        while ((readed = stream.Read(buffer, 0, bufferSize)) != 0)
                        {
                            fs.Write(buffer, 0, readed);
                        }
                    }
                }

                File.Delete(StoragePath(_fileName, null));
                _fileName = fileName;
            }

            return("{ \"filename\" : \"" + _fileName + "\"}");
        }
        // converting a file
        public static string DoConvert(HttpContext context)
        {
            string fileData;

            try
            {
                using (var receiveStream = context.Request.InputStream)
                    using (var readStream = new StreamReader(receiveStream))
                    {
                        fileData = readStream.ReadToEnd();
                        if (string.IsNullOrEmpty(fileData))
                        {
                            context.Response.Write("{\"error\":1,\"message\":\"Request stream is empty\"}");
                        }
                    }
            }
            catch (Exception e)
            {
                throw new HttpException((int)HttpStatusCode.BadRequest, e.Message);
            }

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

            _fileName = Path.GetFileName(body["filename"].ToString());
            var filePass = body["filePass"] != null ? body["filePass"].ToString() : null;
            var lang     = context.Request.Cookies.GetOrDefault("ulang", null);

            var extension         = (Path.GetExtension(_fileName).ToLower() ?? "").Trim('.');
            var internalExtension = FileType.GetInternalExtension(_fileName).Trim('.');

            // check if the file with such an extension can be converted
            if (ConvertExts.Contains("." + extension) &&
                !string.IsNullOrEmpty(internalExtension))
            {
                // generate document key
                var key = ServiceConverter.GenerateRevisionId(FileUri(_fileName, true));

                var fileUrl = new UriBuilder(_Default.GetServerUrl(true));
                fileUrl.Path = HttpRuntime.AppDomainAppVirtualPath
                               + (HttpRuntime.AppDomainAppVirtualPath.EndsWith("/") ? "" : "/")
                               + "webeditor.ashx";
                fileUrl.Query = "type=download&fileName=" + HttpUtility.UrlEncode(_fileName)
                                + "&userAddress=" + HttpUtility.UrlEncode(HttpContext.Current.Request.UserHostAddress);

                // get the url to the converted file
                string newFileUri;
                var    result = ServiceConverter.GetConvertedUri(fileUrl.ToString(), extension, internalExtension, key, true, out newFileUri, filePass, lang);
                if (result != 100)
                {
                    return("{ \"step\" : \"" + result + "\", \"filename\" : \"" + _fileName + "\"}");
                }

                // get a file name of an internal file extension with an index if the file with such a name already exists
                var fileName = GetCorrectName(Path.GetFileNameWithoutExtension(_fileName) + "." + internalExtension);

                var req = (HttpWebRequest)WebRequest.Create(newFileUri);

                VerifySSL();

                using (var stream = req.GetResponse().GetResponseStream())  // get response stream of the converting file
                {
                    if (stream == null)
                    {
                        throw new Exception("Stream is null");
                    }
                    const int bufferSize = 4096;

                    using (var fs = File.Open(StoragePath(fileName, null), FileMode.Create))
                    {
                        var buffer = new byte[bufferSize];
                        int readed;
                        while ((readed = stream.Read(buffer, 0, bufferSize)) != 0)
                        {
                            fs.Write(buffer, 0, readed);  // write bytes to the output stream
                        }
                    }
                }

                // remove the original file and its history if it exists
                var storagePath = StoragePath(_fileName, null);
                var histDir     = HistoryDir(storagePath);
                File.Delete(storagePath);
                if (Directory.Exists(histDir))
                {
                    Directory.Delete(histDir, true);
                }

                // create meta information about the converted file with user id and name specified
                _fileName = fileName;
                var id   = context.Request.Cookies.GetOrDefault("uid", null);
                var user = Users.getUser(id);  // get the user
                DocEditor.CreateMeta(_fileName, user.id, user.name, null);
            }

            return("{ \"filename\" : \"" + _fileName + "\"}");
        }
        public static string DoConvert(HttpContext context)
        {
            _fileName = context.Request["filename"];

            var extension         = (Path.GetExtension(_fileName) ?? "").Trim('.');
            var internalExtension = FileType.GetInternalExtension(_fileName).Trim('.');

            if (ConvertExts.Contains("." + extension) &&
                !string.IsNullOrEmpty(internalExtension))
            {
                var key = ServiceConverter.GenerateRevisionId(FileUri(_fileName));

                string newFileUri;
                var    result = ServiceConverter.GetConvertedUri(FileUri(_fileName), extension, internalExtension, key, true, out newFileUri);
                if (result != 100)
                {
                    return("{ \"step\" : \"" + result + "\", \"filename\" : \"" + _fileName + "\"}");
                }

                var fileName = GetCorrectName(Path.GetFileNameWithoutExtension(_fileName) + "." + internalExtension);

                var req = (HttpWebRequest)WebRequest.Create(newFileUri);

                // hack. http://ubuntuforums.org/showthread.php?t=1841740
                if (IsMono)
                {
                    ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true;
                }

                using (var stream = req.GetResponse().GetResponseStream())
                {
                    if (stream == null)
                    {
                        throw new Exception("Stream is null");
                    }
                    const int bufferSize = 4096;

                    using (var fs = File.Open(StoragePath(fileName, null), FileMode.Create))
                    {
                        var buffer = new byte[bufferSize];
                        int readed;
                        while ((readed = stream.Read(buffer, 0, bufferSize)) != 0)
                        {
                            fs.Write(buffer, 0, readed);
                        }
                    }
                }

                var storagePath = StoragePath(_fileName, null);
                var histDir     = HistoryDir(storagePath);
                File.Delete(storagePath);
                if (Directory.Exists(histDir))
                {
                    Directory.Delete(histDir, true);
                }

                _fileName = fileName;
                histDir   = HistoryDir(StoragePath(_fileName, null));
                Directory.CreateDirectory(histDir);
                File.WriteAllText(Path.Combine(histDir, "createdInfo.json"), new JavaScriptSerializer().Serialize(new Dictionary <string, object> {
                    { "created", DateTime.Now.ToString() },
                    { "id", context.Request.Cookies.GetOrDefault("uid", "uid-1") },
                    { "name", context.Request.Cookies.GetOrDefault("uname", "John Smith") }
                }));
            }

            return("{ \"filename\" : \"" + _fileName + "\"}");
        }