Ejemplo n.º 1
0
        private async Task ValidateForPost(ContextAdapter context)
        {
            ITusConcatenationStore concatenationStore = context.Configuration.Store as ITusConcatenationStore;

            if (concatenationStore != null &&
                context.Request.Headers.ContainsKey(HeaderConstants.UploadConcat))
            {
                var uploadConcat = new Models.Concatenation.UploadConcat(
                    context.Request.GetHeader(HeaderConstants.UploadConcat),
                    context.Configuration.UrlPath);

                if (!uploadConcat.IsValid)
                {
                    await BadRequest(uploadConcat.ErrorMessage);

                    return;
                }

                if (uploadConcat.Type is FileConcatFinal finalConcat)
                {
                    await ValidateFinalFileCreation(finalConcat, context, concatenationStore);
                }
            }
            else
            {
                await Done;
            }
        }
Ejemplo n.º 2
0
 public ConcatenateFilesHandler(ContextAdapter context, ITusConcatenationStore concatenationStore)
     : base(context, IntentType.ConcatenateFiles, LockType.NoLock)
 {
     UploadConcat        = ParseUploadConcatHeader();
     _concatenationStore = concatenationStore;
     _expirationHelper   = new ExpirationHelper(context.Configuration);
 }
Ejemplo n.º 3
0
        private static async Task <string> HandleCreationOfConcatFiles(ContextAdapter context, Models.Concatenation.UploadConcat uploadConcat,
                                                                       ITusConcatenationStore tusConcatenationStore, long uploadLength, string metadata,
                                                                       CancellationToken cancellationToken)
        {
            string fileId;

            if (uploadConcat.Type is FileConcatPartial)
            {
                fileId = await tusConcatenationStore
                         .CreatePartialFileAsync(uploadLength, metadata, cancellationToken);
            }
            else
            {
                var finalConcat = (FileConcatFinal)uploadConcat.Type;
                fileId = await tusConcatenationStore.CreateFinalFileAsync(finalConcat.Files, metadata, cancellationToken);

                // Run callback that the final file is completed.
                if (context.Configuration.OnUploadCompleteAsync != null)
                {
                    await context.Configuration.OnUploadCompleteAsync(fileId, context.Configuration.Store, cancellationToken);
                }
            }

            return(fileId);
        }
Ejemplo n.º 4
0
        private static async Task <string> HandleCreationOfConcatFiles(
            ContextAdapter context,
            UploadConcat uploadConcat,
            ITusConcatenationStore tusConcatenationStore,
            long uploadLength,
            string metadata,
            CancellationToken cancellationToken)
        {
            string fileId;

            if (uploadConcat.Type is FileConcatPartial)
            {
                fileId = await tusConcatenationStore
                         .CreatePartialFileAsync(uploadLength, metadata, cancellationToken);

                await HandleOnCreateComplete(context, fileId, true, uploadConcat, metadata, uploadLength);
            }
            else
            {
                var finalConcat = (FileConcatFinal)uploadConcat.Type;
                fileId = await tusConcatenationStore.CreateFinalFileAsync(finalConcat.Files, metadata,
                                                                          cancellationToken);

                await HandleOnCreateComplete(context, fileId, true, uploadConcat, metadata, uploadLength);

                // Run callback that the final file is completed.
                await HandleOnFileComplete(context, cancellationToken, fileId);
            }

            return(fileId);
        }
        private async Task ValidateForPatch(ContextAdapter context, ITusConcatenationStore concatStore)
        {
            var uploadConcat = await concatStore.GetUploadConcatAsync(context.Request.FileId, context.CancellationToken);

            if (uploadConcat is FileConcatFinal)
            {
                await Forbidden("File with \"Upload-Concat: final\" cannot be patched");
            }
        }
Ejemplo n.º 6
0
        private async Task ValidateFinalFileCreation(FileConcatFinal finalConcat, ContextAdapter context, ITusConcatenationStore store)
        {
            var filesExist = await Task.WhenAll(finalConcat.Files.Select(f =>
                                                                         context.Configuration.Store.FileExistAsync(f, context.CancellationToken)));

            if (filesExist.Any(f => !f))
            {
                await BadRequest(
                    $"Could not find some of the files supplied for concatenation: {string.Join(", ", filesExist.Zip(finalConcat.Files, (b, s) => new {exist = b, name = s}).Where(f => !f.exist).Select(f => f.name))}");

                return;
            }

            var filesArePartial = await Task.WhenAll(
                finalConcat.Files.Select(f => store.GetUploadConcatAsync(f, context.CancellationToken)));

            if (filesArePartial.Any(f => !(f is FileConcatPartial)))
            {
                await BadRequest($"Some of the files supplied for concatenation are not marked as partial and can not be concatenated: {string.Join(", ", filesArePartial.Zip(finalConcat.Files, (s, s1) => new { partial = s is FileConcatPartial, name = s1 }).Where(f => !f.partial).Select(f => f.name))}");

                return;
            }

            var incompleteFiles = new List <string>(finalConcat.Files.Length);
            var totalSize       = 0L;

            foreach (var file in finalConcat.Files)
            {
                var length = context.Configuration.Store.GetUploadLengthAsync(file, context.CancellationToken);
                var offset = context.Configuration.Store.GetUploadOffsetAsync(file, context.CancellationToken);
                await Task.WhenAll(length, offset);

                if (length.Result != null)
                {
                    totalSize += length.Result.Value;
                }

                if (length.Result != offset.Result)
                {
                    incompleteFiles.Add(file);
                }
            }

            if (incompleteFiles.Count > 0)
            {
                await BadRequest(
                    $"Some of the files supplied for concatenation are not finished and can not be concatenated: {string.Join(", ", incompleteFiles)}");

                return;
            }

            if (totalSize > context.Configuration.MaxAllowedUploadSizeInBytes)
            {
                await RequestEntityTooLarge("The concatenated file exceeds the server's max file size.");
            }
        }
Ejemplo n.º 7
0
 public UploadConcatForConcatenateFiles(Models.Concatenation.UploadConcat uploadConcat, ITusConcatenationStore concatenationStore)
 {
     _uploadConcat       = uploadConcat;
     _concatenationStore = concatenationStore;
 }