public override void OnProcessRequest(HttpContext context)
        {
            try
            {
                var request = new ChunkedRequestHelper(context.Request);

                if (!TryAuthorize(request))
                {
                    WriteError(context, "Can't authorize given initiate session request or session with specified upload id already expired");
                    return;
                }

                if (CoreContext.TenantManager.GetCurrentTenant().Status != TenantStatus.Active)
                {
                    WriteError(context, "Can't perform upload for deleted or transfering portals");
                    return;
                }

                switch (request.Type)
                {
                case ChunkedRequestType.Abort:
                    FileUploader.AbortUpload(request.UploadId);
                    WriteSuccess(context, null);
                    return;

                case ChunkedRequestType.Initiate:
                    var createdSession = FileUploader.InitiateUpload(request.FolderId, request.FileId, request.FileName, request.FileSize, request.Encrypted);
                    WriteSuccess(context, ToResponseObject(createdSession, true));
                    return;

                case ChunkedRequestType.Upload:
                    var resumedSession = FileUploader.UploadChunk(request.UploadId, request.ChunkStream, request.ChunkSize);

                    if (resumedSession.BytesUploaded == resumedSession.BytesTotal)
                    {
                        WriteSuccess(context, ToResponseObject(resumedSession.File), (int)HttpStatusCode.Created);
                        FilesMessageService.Send(resumedSession.File, context.Request, MessageAction.FileUploaded, resumedSession.File.Title);
                    }
                    else
                    {
                        WriteSuccess(context, ToResponseObject(resumedSession));
                    }
                    return;

                default:
                    WriteError(context, "Unknown request type.");
                    return;
                }
            }
            catch (FileNotFoundException error)
            {
                Global.Logger.Error(error);
                WriteError(context, FilesCommonResource.ErrorMassage_FileNotFound);
            }
            catch (Exception error)
            {
                Global.Logger.Error(error);
                WriteError(context, error.Message);
            }
        }
        public object CreateUploadSession(T folderId, string fileName, long fileSize, string relativePath, bool encrypted)
        {
            var file = FileUploader.VerifyChunkedUpload(folderId, fileName, fileSize, FilesSettingsHelper.UpdateIfExist, relativePath);

            if (FilesLinkUtility.IsLocalFileUploader)
            {
                var session = FileUploader.InitiateUpload(file.FolderID.ToString(), (file.ID ?? default).ToString(), file.Title, file.ContentLength, encrypted);

                var response = ChunkedUploadSessionHelper.ToResponseObject(session, true);
                return(new
                {
                    success = true,
                    data = response
                });
            }

            var createSessionUrl = FilesLinkUtility.GetInitiateUploadSessionUrl(TenantManager.GetCurrentTenant().TenantId, file.FolderID, file.ID, file.Title, file.ContentLength, encrypted, SecurityContext);
            var request          = (HttpWebRequest)WebRequest.Create(createSessionUrl);

            request.Method        = "POST";
            request.ContentLength = 0;

            // hack for uploader.onlyoffice.com in api requests
            var rewriterHeader = ApiContext.HttpContext.Request.Headers[HttpRequestExtensions.UrlRewriterHeader];

            if (!string.IsNullOrEmpty(rewriterHeader))
            {
                request.Headers[HttpRequestExtensions.UrlRewriterHeader] = rewriterHeader;
            }

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

            using (var response = request.GetResponse())
                using (var responseStream = response.GetResponseStream())
                {
                    return(JObject.Parse(new StreamReader(responseStream).ReadToEnd())); //result is json string
                }
        }
        public async Task Invoke <T>(HttpContext context)
        {
            try
            {
                if (context.Request.Method == "OPTIONS")
                {
                    context.Response.StatusCode = 200;
                    return;
                }

                var request = new ChunkedRequestHelper <T>(context.Request);

                if (!TryAuthorize(request))
                {
                    await WriteError(context, "Can't authorize given initiate session request or session with specified upload id already expired");

                    return;
                }

                if (TenantManager.GetCurrentTenant().Status != TenantStatus.Active)
                {
                    await WriteError(context, "Can't perform upload for deleted or transfering portals");

                    return;
                }

                switch (request.Type(InstanceCrypto))
                {
                case ChunkedRequestType.Abort:
                    FileUploader.AbortUpload <T>(request.UploadId);
                    await WriteSuccess(context, null);

                    return;

                case ChunkedRequestType.Initiate:
                    var createdSession = FileUploader.InitiateUpload(request.FolderId, request.FileId, request.FileName, request.FileSize, request.Encrypted);
                    await WriteSuccess(context, ChunkedUploadSessionHelper.ToResponseObject(createdSession, true));

                    return;

                case ChunkedRequestType.Upload:
                    var resumedSession = FileUploader.UploadChunk <T>(request.UploadId, request.ChunkStream, request.ChunkSize);

                    if (resumedSession.BytesUploaded == resumedSession.BytesTotal)
                    {
                        await WriteSuccess(context, ToResponseObject(resumedSession.File), (int)HttpStatusCode.Created);

                        FilesMessageService.Send(resumedSession.File, MessageAction.FileUploaded, resumedSession.File.Title);
                    }
                    else
                    {
                        await WriteSuccess(context, ChunkedUploadSessionHelper.ToResponseObject(resumedSession));
                    }
                    return;

                default:
                    await WriteError(context, "Unknown request type.");

                    return;
                }
            }
            catch (FileNotFoundException error)
            {
                Logger.Error(error);
                await WriteError(context, FilesCommonResource.ErrorMassage_FileNotFound);
            }
            catch (Exception error)
            {
                Logger.Error(error);
                await WriteError(context, error.Message);
            }
        }