Ejemplo n.º 1
0
        private void UpdateHash(HashAlgorithm hash, long length, UpdateChecksum update)
        {
            HashAlgorithmReuse reuse = hash as HashAlgorithmReuse;

            if (reuse != null && update != null)
            {
                using (HashAlgorithm hashReuse = (HashAlgorithm)reuse.Clone()) {
                    hashReuse.TransformFinalBlock(new byte[0], 0, 0);
                    update(hashReuse.Hash, length);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///  Uploads the file.
        ///  Resumes an upload if the given localFileStream.Position is larger than zero.
        /// </summary>
        /// <returns>
        ///  The new CMIS document.
        /// </returns>
        /// <param name='remoteDocument'>
        ///  Remote document where the local content should be uploaded to.
        /// </param>
        /// <param name='localFileStream'>
        ///  Local file stream.
        /// </param>
        /// <param name='transmission'>
        ///  Transmission status where the uploader should report its uploading status.
        /// </param>
        /// <param name='hashAlg'>
        ///  Hash alg which should be used to calculate a checksum over the uploaded content.
        /// </param>
        /// <param name='overwrite'>
        ///  If true, the local content will overwrite the existing content.
        /// </param>
        /// <param name="update">Is called on every new chunk, if not <c>null</c>.</param>
        /// <exception cref="CmisSync.Lib.Tasks.UploadFailedException">
        /// Contains the last successful remote document state. This is needed for continue a failed upload.
        /// </exception>
        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);
        }