コード例 #1
0
 internal static ITusStore WithExistingFile(this ITusStore store, string fileId, Func <CallInfo, long?> uploadLength, Func <CallInfo, long> uploadOffset)
 {
     store.FileExistAsync(fileId, Arg.Any <CancellationToken>()).Returns(true);
     store.GetUploadLengthAsync(fileId, Arg.Any <CancellationToken>()).Returns(uploadLength);
     store.GetUploadOffsetAsync(fileId, Arg.Any <CancellationToken>()).Returns(uploadOffset);
     return(store);
 }
コード例 #2
0
        private DefaultTusConfiguration GetConfigForEmptyStoreWithOnAuthorize(bool storeSupportsTermination)
        {
            ITusStore store = null;

            if (storeSupportsTermination)
            {
                store = Substitute.For <ITusStore, ITusTerminationStore>();
            }
            else
            {
                store = Substitute.For <ITusStore>();
            }

            return(new DefaultTusConfiguration
            {
                Store = store,
                UrlPath = "/files",
                Events = new Events
                {
                    OnAuthorizeAsync = ctx =>
                    {
                        _onAuthorizeWasCalled = true;
                        _onAuthorizeWasCalledWithIntent = ctx.Intent;
                        return Task.FromResult(0);
                    }
                }
            });
        }
コード例 #3
0
        internal static ITusStore WithExistingPartialFile(this ITusStore store, string fileId, long?uploadLength = 1, long uploadOffset = 0)
        {
            store.WithExistingFile(fileId, uploadLength, uploadOffset);

            ((ITusConcatenationStore)store).GetUploadConcatAsync(fileId, Arg.Any <CancellationToken>()).Returns(new Models.Concatenation.FileConcatPartial());

            return(store);
        }
コード例 #4
0
        /// <summary>
        /// Check if the <see cref="ITusFile"/> has been completely uploaded.
        /// </summary>
        /// <param name="file">The file to check</param>
        /// <param name="store">The store responsible for handling the file</param>
        /// <param name="cancellationToken">The cancellation token to use when cancelling</param>
        /// <returns>True if the file is completed, otherwise false</returns>
        public static async Task <bool> IsCompleteAsync(this ITusFile file, ITusStore store, CancellationToken cancellationToken)
        {
            var length = store.GetUploadLengthAsync(file.Id, cancellationToken);
            var offset = store.GetUploadOffsetAsync(file.Id, cancellationToken);

            await Task.WhenAll(length, offset);

            return(length.Result == offset.Result);
        }
コード例 #5
0
        protected IntentHandler(ContextAdapter context, IntentType intent, LockType requiresLock)
        {
            Context           = context;
            Request           = context.Request;
            Response          = context.Response;
            CancellationToken = context.CancellationToken;
            Store             = context.Configuration.Store;

            Intent   = intent;
            LockType = requiresLock;
        }
コード例 #6
0
 public static TestServer Create(ITusStore store)
 {
     return(Create(app =>
     {
         app.UseTus(context => new DefaultTusConfiguration
         {
             UrlPath = "/files",
             Store = store
         });
     }));
 }
コード例 #7
0
        public async Task Runs_OnFileCompleteAsync_When_A_Final_File_Is_Created(string methodToUse)
        {
            var store = CreateStoreForFinalFileConcatenation();

            string    oldCallbackFileId = null;
            ITusStore oldCallbackStore  = null;

            string    callbackFileId = null;
            ITusStore callbackStore  = null;

            using (var server = TestServerFactory.Create(app =>
            {
                app.UseTus(request => new DefaultTusConfiguration
                {
                    Store = store,
                    UrlPath = "/files",
#pragma warning disable CS0618 // Type or member is obsolete
                    OnUploadCompleteAsync = (fileId, tusStore, ct) =>
#pragma warning restore CS0618 // Type or member is obsolete
                    {
                        oldCallbackFileId = fileId;
                        oldCallbackStore = tusStore;
                        return(Task.FromResult(0));
                    },
                    Events = new Events
                    {
                        OnFileCompleteAsync = ctx =>
                        {
                            callbackFileId = ctx.FileId;
                            callbackStore = ctx.Store;
                            return(Task.FromResult(0));
                        }
                    }
                });
            }))
            {
                var response = await server
                               .CreateRequest("/files")
                               .AddTusResumableHeader()
                               .AddHeader("Upload-Concat", "final;/files/partial1 /files/partial2")
                               .OverrideHttpMethodIfNeeded("POST", methodToUse)
                               .SendAsync(methodToUse);

                response.StatusCode.ShouldBe(HttpStatusCode.Created);

                oldCallbackFileId.ShouldBe("finalId");
                oldCallbackStore.ShouldBe(store);

                callbackFileId.ShouldBe("finalId");
                callbackStore.ShouldBe(store);
            }
        }
コード例 #8
0
 public static TestServer Create(ITusStore store, Events events = null, MetadataParsingStrategy metadataParsingStrategy = MetadataParsingStrategy.AllowEmptyValues)
 {
     return(Create(app =>
     {
         app.UseTus(_ => new DefaultTusConfiguration
         {
             UrlPath = "/files",
             Store = store,
             Events = events,
             MetadataParsingStrategy = metadataParsingStrategy
         });
     }));
 }
コード例 #9
0
ファイル: DeleteTests.cs プロジェクト: hannuniemela/tusdotnet
        public async Task Runs_OnDeleteCompleteAsync_After_Deleting_The_File(string methodToUse)
        {
            var fileId = Guid.NewGuid().ToString();
            var store  = Substitute.For <ITusStore, ITusTerminationStore>();

            store.FileExistAsync(fileId, Arg.Any <CancellationToken>()).Returns(true);

            var       onDeleteCompleteAsyncCalled = false;
            var       deleteFileAsyncCalled       = false;
            string    callbackFileId = null;
            ITusStore callbackStore  = null;

            var terminationStore = (ITusTerminationStore)store;

            terminationStore.DeleteFileAsync(null, CancellationToken.None)
            .ReturnsForAnyArgs(Task.FromResult(0))
            .AndDoes(ci =>
            {
                onDeleteCompleteAsyncCalled.ShouldBeFalse();
                deleteFileAsyncCalled = true;
            });

            var events = new Events
            {
                OnDeleteCompleteAsync = ctx =>
                {
                    deleteFileAsyncCalled.ShouldBe(true);
                    onDeleteCompleteAsyncCalled = true;
                    callbackFileId = ctx.FileId;
                    callbackStore  = ctx.Store;
                    return(Task.FromResult(0));
                }
            };

            using (var server = TestServerFactory.Create(store, events))
            {
                var response = await server
                               .CreateRequest($"/files/{fileId}")
                               .AddTusResumableHeader()
                               .OverrideHttpMethodIfNeeded("DELETE", methodToUse)
                               .SendAsync(methodToUse);

                response.StatusCode.ShouldBe(HttpStatusCode.NoContent);
                deleteFileAsyncCalled.ShouldBeTrue();
                callbackFileId.ShouldBe(fileId);
                callbackStore.ShouldBe(store);
            }
        }
コード例 #10
0
        public async Task OnUploadCompleteAsync_Is_Called_When_A_Final_File_Is_Created(string methodToUse)
        {
            var store       = Substitute.For <ITusStore, ITusCreationStore, ITusConcatenationStore>();
            var concatStore = (ITusConcatenationStore)store;

            store.FileExistAsync("partial1", Arg.Any <CancellationToken>()).Returns(true);
            store.FileExistAsync("partial2", Arg.Any <CancellationToken>()).Returns(true);
            store.GetUploadLengthAsync("partial1", Arg.Any <CancellationToken>()).Returns(10);
            store.GetUploadLengthAsync("partial2", Arg.Any <CancellationToken>()).Returns(20);
            store.GetUploadOffsetAsync("partial1", Arg.Any <CancellationToken>()).Returns(10);
            store.GetUploadOffsetAsync("partial2", Arg.Any <CancellationToken>()).Returns(20);
            concatStore.GetUploadConcatAsync("partial1", Arg.Any <CancellationToken>()).Returns(new FileConcatPartial());
            concatStore.GetUploadConcatAsync("partial2", Arg.Any <CancellationToken>()).Returns(new FileConcatPartial());
            concatStore.CreateFinalFileAsync(null, null, Arg.Any <CancellationToken>())
            .ReturnsForAnyArgs("finalId");

            string    callbackFileId = null;
            ITusStore callbackStore  = null;

            using (var server = TestServerFactory.Create(app =>
            {
                app.UseTus(request => new DefaultTusConfiguration
                {
                    Store = store,
                    UrlPath = "/files",
                    OnUploadCompleteAsync = (fileId, tusStore, ct) =>
                    {
                        callbackFileId = fileId;
                        callbackStore = tusStore;
                        return(Task.FromResult(0));
                    }
                });
            }))
            {
                var response = await server
                               .CreateRequest("/files")
                               .AddTusResumableHeader()
                               .AddHeader("Upload-Concat", "final;/files/partial1 /files/partial2")
                               .OverrideHttpMethodIfNeeded("POST", methodToUse)
                               .SendAsync(methodToUse);

                response.StatusCode.ShouldBe(HttpStatusCode.Created);

                callbackFileId.ShouldBe("finalId");
                callbackStore.ShouldBe(store);
            }
        }
コード例 #11
0
 internal static ITusStore WithExistingFile(this ITusStore store, string fileId, long?uploadLength = 1, long uploadOffset = 0)
 {
     return(store.WithExistingFile(fileId, _ => uploadLength, _ => uploadOffset));
 }