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);
            }
        }
Exemplo n.º 2
0
        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);
            }
        }