예제 #1
0
        public async Task <UploadContextModel> UploadChunkAsync(
            UploadContextModel uploadContext,
            long from,
            long to,
            Stream inputStream,
            int chunkIndex,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            var uploadStateToken = UploadTokenModel.Parse(uploadContext.UploadToken);

            var fileLocator = await FileStore.FileLocatorGetAsync(uploadStateToken.Identifier);

            var uploadChunk = new UploadChunkModel
            {
                Identifier   = new UploadChunkIdentifier(uploadStateToken.Identifier, fileLocator + '.' + chunkIndex.ToString()),
                ChunkIndex   = chunkIndex,
                PositionFrom = from,
                PositionTo   = to
            };

            uploadContext.SequentialState = await BackendClient.UploadChunkAsync(
                await LoadConfigurationAsync(uploadStateToken.Identifier as OrganizationIdentifier),
                uploadStateToken.Identifier.UploadKey,
                fileLocator,
                uploadChunk.Identifier.UploadChunkKey,
                chunkIndex,
                uploadContext.TotalChunks,
                uploadContext.SequentialState,
                uploadChunk.PositionFrom,
                uploadChunk.PositionTo,
                uploadContext.FileLength,
                inputStream,
                cancellationToken
                );

            uploadChunk.State   = uploadContext.SequentialState;
            uploadChunk.Success = true;

            await UploadChunkStore.InsertAsync(uploadChunk);

            return(uploadContext);
        }
예제 #2
0
        public async Task <FileModel> UploadCompleteAsync(
            UploadContextModel uploadContext,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            var uploadStateToken = UploadTokenModel.Parse(uploadContext.UploadToken);

            var upload = await UploadStore.GetOneAsync(uploadStateToken.Identifier, new[]
            {
                new PopulationDirective
                {
                    Name = nameof(UploadModel.Chunks)
                }
            });

            var chunkStates = upload.Chunks?.Rows
                              .OrderBy(c => c.ChunkIndex)
                              .Select(c => new ChunkedStatusModel
            {
                ChunkIndex     = c.ChunkIndex,
                UploadChunkKey = c.Identifier.UploadChunkKey,
                State          = c.State,
                Success        = true
            })
                              .ToArray();

            var fileLocator = await FileStore.FileLocatorGetAsync(uploadStateToken.Identifier);

            var returnData = await BackendClient.CompleteChunkedUploadAsync(
                await LoadConfigurationAsync(uploadStateToken.Identifier as OrganizationIdentifier),
                uploadStateToken.Identifier.UploadKey,
                fileLocator,
                chunkStates
                );

            var fileModel = await FileStore.GetOneAsync(uploadStateToken.Identifier);

            await FileStore.UpdateAsync(fileModel);

            if (returnData != null)
            {
                await FileStore.HashSetAsync(
                    fileModel.Identifier,
                    GetHash(returnData, "md5"),
                    GetHash(returnData, "sha1"),
                    GetHash(returnData, "sha256")
                    );
            }

            await FileStore.UploadingStatusSetAsync(fileModel.Identifier, false);

            await UploadStore.Cleanup(upload.Identifier);

            var evt = new FileContentsUploadCompleteEvent
            {
                FileIdentifier = fileModel.Identifier
            };

            evt.Populate(fileModel);

            await EventSender.SendAsync(evt);

            return(fileModel);
        }