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;
}
}