CmisSync.Lib.FileTransmission.ChunkedUploader.UploadFile C# (CSharp) Méthode

UploadFile() public méthode

Uploads the file. Resumes an upload if the given localFileStream.Position is larger than zero.
/// Contains the last successful remote document state. This is needed for continue a failed upload. ///
public UploadFile ( IDocument remoteDocument, Stream localFileStream, Transmission transmission, HashAlgorithm hashAlg, bool overwrite = true, UpdateChecksum update = null ) : IDocument
remoteDocument IDocument /// Remote document where the local content should be uploaded to. ///
localFileStream Stream /// Local file stream. ///
transmission Transmission /// Transmission status where the uploader should report its uploading status. ///
hashAlg System.Security.Cryptography.HashAlgorithm /// Hash alg which should be used to calculate a checksum over the uploaded content. ///
overwrite bool /// If true, the local content will overwrite the existing content. ///
update UpdateChecksum Is called on every new chunk, if not null.
Résultat IDocument
        public override IDocument UploadFile(
            IDocument remoteDocument,
            Stream localFileStream,
            Transmission transmission,
            HashAlgorithm hashAlg,
            bool overwrite = true,
            UpdateChecksum update = null)
        {
            IDocument result = remoteDocument;
            for (long offset = localFileStream.Position; offset < localFileStream.Length; offset += this.ChunkSize) {
                bool isFirstChunk = offset == 0;
                bool isLastChunk = (offset + this.ChunkSize) >= localFileStream.Length;
                using (var hashstream = new NonClosingHashStream(localFileStream, hashAlg, CryptoStreamMode.Read))
                using (var chunkstream = new ChunkedStream(hashstream, this.ChunkSize))
                using (var offsetstream = new OffsetStream(chunkstream, offset))
                using (var transmissionStream = transmission.CreateStream(offsetstream)) {
                    transmission.Length = localFileStream.Length;
                    transmission.Position = offset;
                    chunkstream.ChunkPosition = offset;

                    ContentStream contentStream = new ContentStream();
                    contentStream.FileName = remoteDocument.Name;
                    contentStream.MimeType = Cmis.MimeType.GetMIMEType(remoteDocument.Name);
                    if (isLastChunk) {
                        contentStream.Length = localFileStream.Length - offset;
                    } else {
                        contentStream.Length = this.ChunkSize;
                    }

                    contentStream.Stream = transmissionStream;
                    try {
                        if (isFirstChunk && result.ContentStreamId != null && overwrite) {
                            result.DeleteContentStream(true);
                        }

                        result.AppendContentStream(contentStream, isLastChunk, true);
                        HashAlgorithmReuse reuse = hashAlg as HashAlgorithmReuse;
                        if (reuse != null && update != null) {
                            using (HashAlgorithm hash = (HashAlgorithm)reuse.Clone()) {
                                hash.TransformFinalBlock(new byte[0], 0, 0);
                                update(hash.Hash, result.ContentStreamLength.GetValueOrDefault());
                            }
                        }
                    } catch (Exception e) {
                        if (e is FileTransmission.AbortException) {
                            throw;
                        }

                        if (e.InnerException is FileTransmission.AbortException) {
                            throw e.InnerException;
                        }

                        throw new UploadFailedException(e, result);
                    }
                }
            }

            hashAlg.TransformFinalBlock(new byte[0], 0, 0);
            return result;
        }
    }

Usage Example

        public void NormalUploadReplacesRemoteStreamIfRemoteStreamExists() {
            this.mockedDocument.Setup(doc => doc.ContentStreamId).Returns("StreamId");
            this.mockedDocument.Setup(doc => doc.DeleteContentStream(It.IsAny<bool>())).Callback(() => {
                if (this.remoteStream != null) {
                    this.remoteStream.Dispose();
                }

                this.remoteStream = new MemoryStream();
            }).Returns(this.mockedDocument.Object);

            this.remoteStream.WriteByte(1);

            using (IFileUploader uploader = new ChunkedUploader(this.chunkSize)) {
                uploader.UploadFile(this.mockedDocument.Object, this.localFileStream, this.transmission, this.hashAlg);
            }

            this.mockedDocument.Verify(doc => doc.DeleteContentStream(It.IsAny<bool>()), Times.Once());
            this.AssertThatLocalAndRemoteContentAreEqualToHash();
            Assert.AreEqual(1, this.lastChunk);
        }
All Usage Examples Of CmisSync.Lib.FileTransmission.ChunkedUploader::UploadFile