Beispiel #1
0
        internal override async Task Invoke()
        {
            var metadata = Request.GetHeader(HeaderConstants.UploadMetadata);

            var onBeforeCreateResult = await EventHelper.Validate <BeforeCreateContext>(Context, ctx =>
            {
                ctx.Metadata     = _metadataFromRequirement;
                ctx.UploadLength = Request.UploadLength;
            });

            if (onBeforeCreateResult == ResultType.StopExecution)
            {
                return;
            }

            var fileId = await _creationStore.CreateFileAsync(Request.UploadLength, metadata, CancellationToken);

            await EventHelper.Notify <CreateCompleteContext>(Context, ctx =>
            {
                ctx.FileId            = fileId;
                ctx.FileConcatenation = null;
                ctx.Metadata          = _metadataFromRequirement;
                ctx.UploadLength      = Request.UploadLength;
            });

            var isEmptyFile = Request.UploadLength == 0;

            DateTimeOffset?expires      = null;
            long?          uploadOffset = null;

            // If the file is empty there is no need to save any data.
            if (isEmptyFile)
            {
                // Normally we would call NotifyFileComplete from WriteFileHandler but since we never use
                // WriteFileContextForCreationWithUpload if the file is empty, nor allow PATCH requests for the file, we need to trigger the event here.
                await EventHelper.NotifyFileComplete(Context, ctx => ctx.FileId = fileId);
            }
            else
            {
                // Expiration is only used when patching files so if the file is not empty and we did not have any data in the current request body,
                // we need to update the header here to be able to keep track of expiration for this file.
                expires = await _expirationHelper.SetExpirationIfSupported(fileId, CancellationToken);

                var writeFileContext = await WriteFileContextForCreationWithUpload.FromCreationContext(Context, fileId);

                if (writeFileContext.FileContentIsAvailable)
                {
                    uploadOffset = await writeFileContext.SaveFileContent();

                    expires = writeFileContext.UploadExpires;
                }
            }

            SetReponseHeaders(fileId, expires, uploadOffset);

            Response.SetStatus(HttpStatusCode.Created);
        }
Beispiel #2
0
        internal override async Task Invoke()
        {
            var metadata = Request.GetHeader(HeaderConstants.UploadMetadata);

            var onBeforeCreateResult = await EventHelper.Validate <BeforeCreateContext>(Context, ctx =>
            {
                ctx.Metadata     = _metadataFromRequirement;
                ctx.UploadLength = Request.UploadLength;
            });

            if (onBeforeCreateResult == ResultType.StopExecution)
            {
                return;
            }

            var fileId = await _creationStore.CreateFileAsync(Request.UploadLength, metadata, CancellationToken);

            await EventHelper.Notify <CreateCompleteContext>(Context, ctx =>
            {
                ctx.FileId            = fileId;
                ctx.FileConcatenation = null;
                ctx.Metadata          = _metadataFromRequirement;
                ctx.UploadLength      = Request.UploadLength;
            });

            var expires = await _expirationHelper.SetExpirationIfSupported(fileId, CancellationToken);

            long?uploadOffset     = null;
            var  writeFileContext = await WriteFileContextForCreationWithUpload.FromCreationContext(Context, fileId);

            if (writeFileContext.FileContentIsAvailable)
            {
                uploadOffset = await writeFileContext.SaveFileContent();

                expires = writeFileContext.UploadExpires;
            }

            SetReponseHeaders(fileId, expires, uploadOffset);

            Response.SetStatus(HttpStatusCode.Created);
        }
Beispiel #3
0
        internal override async Task Invoke()
        {
            var metadataString = Request.GetHeader(HeaderConstants.UploadMetadata);

            string         fileId;
            DateTimeOffset?expires      = null;
            long?          uploadOffset = null;

            var onBeforeCreateResult = await EventHelper.Validate <BeforeCreateContext>(Context, ctx =>
            {
                ctx.Metadata          = _metadataFromRequirement;
                ctx.UploadLength      = Request.UploadLength;
                ctx.FileConcatenation = UploadConcat.Type;
            });

            if (onBeforeCreateResult == ResultType.StopExecution)
            {
                return;
            }

            fileId = await HandleCreationOfConcatFiles(Request.UploadLength, metadataString, _metadataFromRequirement);

            if (_isPartialFile)
            {
                expires = await _expirationHelper.SetExpirationIfSupported(fileId, CancellationToken);

                var writeFileContext = await WriteFileContextForCreationWithUpload.FromCreationContext(Context, fileId);

                if (writeFileContext.FileContentIsAvailable)
                {
                    uploadOffset = await writeFileContext.SaveFileContent(UploadConcat.Type);

                    expires = writeFileContext.UploadExpires;
                }
            }

            SetResponseHeaders(fileId, expires, uploadOffset);

            Response.SetStatus(HttpStatusCode.Created);
        }