CmisSync.Lib.FileTransmission.SimpleFileUploader.UploadFile C# (CSharp) Метод

UploadFile() публичный Метод

Uploads the localFileStream to remoteDocument.
If upload fails
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
Результат IDocument
        public virtual IDocument UploadFile(
            IDocument remoteDocument,
            Stream localFileStream,
            Transmission transmission,
            HashAlgorithm hashAlg,
            bool overwrite = true,
            UpdateChecksum update = null)
        {
            if (remoteDocument == null) {
                throw new ArgumentException("remoteDocument can not be null");
            }

            if (localFileStream == null) {
                throw new ArgumentException("localFileStream can not be null");
            }

            if (transmission == null) {
                throw new ArgumentException("status can not be null");
            }

            if (hashAlg == null) {
                throw new ArgumentException("hashAlg can not be null");
            }

            using(NonClosingHashStream hashstream = new NonClosingHashStream(localFileStream, hashAlg, CryptoStreamMode.Read))
            using(var transmissionStream = transmission.CreateStream(hashstream))
            {
                ContentStream contentStream = new ContentStream();
                contentStream.FileName = remoteDocument.Name;
                contentStream.MimeType = Cmis.MimeType.GetMIMEType(contentStream.FileName);
                contentStream.Stream = transmissionStream;
                try {
                    remoteDocument.SetContentStream(contentStream, overwrite, true);
                } catch(Exception e) {
                    throw new UploadFailedException(e, remoteDocument);
                }
            }

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

Usage Example

Пример #1
0
        public void UploadWhileAnotherProcessIsWritingToFile() {
            var fileName = "slowFile.txt";
            var chunkSize = 1024;
            var chunks = 100;
            byte[] chunk = new byte[chunkSize];
            var finalLength = chunks * chunkSize;
            var file = new FileInfo(Path.Combine(this.LocalTestDir.FullName, fileName));
            var mockedDocument = new Mock<IDocument>();
            var transmissionStatus = new Transmission(TransmissionType.UPLOAD_NEW_FILE, fileName);
            mockedDocument.Setup(doc => doc.Name).Returns(fileName);
            using (var remoteStream = new MemoryStream()) {
                mockedDocument.Setup(doc => doc.SetContentStream(It.IsAny<IContentStream>(), It.Is<bool>(b => b == true), It.Is<bool>(b => b == true)))
                    .Callback<IContentStream, bool, bool>((s, b, r) => s.Stream.CopyTo(remoteStream))
                        .Returns(new Mock<IObjectId>().Object);
                using (var fileStream = file.Open(FileMode.CreateNew, FileAccess.Write, FileShare.Read)) {
                    using (var task = Task.Factory.StartNew(() => {
                        var newFileHandle = new FileInfo(file.FullName);
                        using (var hashAlg = new SHA1Managed())
                        using (var readingFileStream = newFileHandle.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        using (var uploader = new SimpleFileUploader()) {
                            uploader.UploadFile(mockedDocument.Object, readingFileStream, transmissionStatus, hashAlg);
                        }

                        Assert.That(remoteStream.Length, Is.EqualTo(finalLength));
                    })) {
                        for (int i = 0; i < chunks; i++) {
                            Thread.Sleep(10);
                            fileStream.Write(chunk, 0, chunkSize);
                        }

                        task.Wait();
                    }
                }
            }
        }
All Usage Examples Of CmisSync.Lib.FileTransmission.SimpleFileUploader::UploadFile