// Token: 0x06000D05 RID: 3333 RVA: 0x00036658 File Offset: 0x00034858
 private void ReadAttachmentIntoStreamAndValidateHash(IStreamAttachment attachment, Stream outputStream, Action abortFileOperation)
 {
     using (IOCostStream iocostStream = new IOCostStream(outputStream))
     {
         using (new FileSystemPerformanceTracker("MailboxDownload", iocostStream, this.logger))
         {
             using (HashAlgorithm hashAlgorithm = new SHA1CryptoServiceProvider())
             {
                 using (new StorePerformanceTracker("MailboxDownload", this.logger))
                 {
                     CopyStreamResult arg;
                     using (IOCostStream iocostStream2 = new IOCostStream(attachment.GetContentStream()))
                     {
                         using (new FileSystemPerformanceTracker("MailboxDownload", iocostStream2, this.logger))
                         {
                             arg = MailboxFileStore.streamCopier.CopyStream(iocostStream2, iocostStream, hashAlgorithm, abortFileOperation);
                         }
                     }
                     this.tracer.TraceDebug <CopyStreamResult>((long)this.GetHashCode(), "Downloaded file attachment from mailbox. {0}", arg);
                     byte[] array = attachment[AttachmentSchema.AttachHash] as byte[];
                     this.tracer.TraceDebug <ArrayTracer <byte> >((long)this.GetHashCode(), "Expected hash in attachment downloaded from mailbox is: {0}.", new ArrayTracer <byte>(array));
                     if (!ArrayComparer <byte> .Comparer.Equals(hashAlgorithm.Hash, array))
                     {
                         throw new InvalidDataException(string.Format("Hash of data content doesn't match hash of original uploaded file. Expected: {0}, actual: {1}.", BitConverter.ToString(array), BitConverter.ToString(hashAlgorithm.Hash)));
                     }
                 }
             }
         }
     }
 }
        // Token: 0x06000D03 RID: 3331 RVA: 0x0003642C File Offset: 0x0003462C
        private void UploadSingleFile(string fileName, IAttachmentCollection attachmentCollection, Action abortFileOperation)
        {
            abortFileOperation();
            string fileName2 = Path.GetFileName(fileName);

            using (IOCostStream iocostStream = new IOCostStream(new FileStream(fileName, FileMode.Open)))
            {
                using (new FileSystemPerformanceTracker("MailboxUpload", iocostStream, this.logger))
                {
                    using (HashAlgorithm hashAlgorithm = new SHA1CryptoServiceProvider())
                    {
                        using (IStreamAttachment streamAttachment = (IStreamAttachment)attachmentCollection.CreateIAttachment(AttachmentType.Stream))
                        {
                            using (new StorePerformanceTracker("MailboxUpload", this.logger))
                            {
                                CopyStreamResult arg;
                                using (IOCostStream iocostStream2 = new IOCostStream(streamAttachment.GetContentStream()))
                                {
                                    arg = MailboxFileStore.streamCopier.CopyStream(iocostStream, iocostStream2, hashAlgorithm, abortFileOperation);
                                    iocostStream2.Flush();
                                }
                                streamAttachment[AttachmentSchema.DisplayName]        = fileName2;
                                streamAttachment[AttachmentSchema.AttachLongFileName] = fileName2;
                                streamAttachment[AttachmentSchema.AttachHash]         = hashAlgorithm.Hash;
                                streamAttachment.Save();
                                this.tracer.TraceDebug <string, CopyStreamResult>((long)this.GetHashCode(), "Uploaded file '{0}' to mailbox. {1}", fileName, arg);
                            }
                        }
                    }
                }
            }
        }
 internal static void CreateAttachment(IItem parentItem, Attachment16Data attachmentData)
 {
     AirSyncDiagnostics.TraceDebug <byte>(ExTraceGlobals.RequestsTracer, null, "CreateAttachment with AttachMethod:{0}", attachmentData.Method);
     if (attachmentData.Method == 1)
     {
         if (attachmentData.Content == null)
         {
             throw new ConversionException(string.Format(" Attachment content can not be null.", new object[0]));
         }
         IStreamAttachment streamAttachment = parentItem.IAttachmentCollection.CreateIAttachment(AttachmentType.Stream) as IStreamAttachment;
         AttachmentHelper.CopyCommonAttachmentProperties(streamAttachment, attachmentData);
         using (Stream contentStream = streamAttachment.GetContentStream())
         {
             contentStream.Write(attachmentData.Content, 0, attachmentData.Content.Length);
         }
         streamAttachment.Save();
     }
     else
     {
         if (attachmentData.Method != 5)
         {
             throw new ConversionException(string.Format("UnSupported Value '{0}' for Attachment Method. Only 1 & 5 is supported AttachemntType", attachmentData.Method));
         }
         ItemAttachment itemAttachment = parentItem.IAttachmentCollection.CreateIAttachment(AttachmentType.EmbeddedMessage) as ItemAttachment;
         AttachmentHelper.CopyCommonAttachmentProperties(itemAttachment, attachmentData);
         using (Stream stream = new MemoryStream(attachmentData.Content))
         {
             stream.Seek(0L, SeekOrigin.Begin);
             InboundConversionOptions inboundConversionOptions = AirSyncUtility.GetInboundConversionOptions();
             inboundConversionOptions.ClearCategories = false;
             try
             {
                 using (Item item = itemAttachment.GetItem())
                 {
                     ItemConversion.ConvertAnyMimeToItem(item, stream, inboundConversionOptions);
                     item.Save(SaveMode.NoConflictResolution);
                 }
             }
             catch (ExchangeDataException innerException)
             {
                 throw new AirSyncPermanentException(HttpStatusCode.BadRequest, StatusCode.InvalidMIME, innerException, false);
             }
             catch (ConversionFailedException innerException2)
             {
                 throw new AirSyncPermanentException(HttpStatusCode.BadRequest, StatusCode.InvalidMIME, innerException2, false);
             }
         }
         itemAttachment.Save();
     }
     AirSyncDiagnostics.TraceDebug <int>(ExTraceGlobals.RequestsTracer, null, "AttachmentHelper:CreateAttachments:: AttachmentCreated successful. AttachmentCount:{0}", parentItem.IAttachmentCollection.Count);
 }
            // Token: 0x06001AFF RID: 6911 RVA: 0x0006673C File Offset: 0x0006493C
            private void UploadAttachment(IAttachment attachment, IItem draftItem, out IAttachment newAttachment, out AttachmentId newAttachmentId)
            {
                IStreamAttachment streamAttachment = attachment as IStreamAttachment;

                if (streamAttachment == null)
                {
                    throw new InvalidOperationException("UploadAttachment requires a stream attachment, but was given a " + attachment.GetType().Name);
                }
                UploadItemAsyncResult uploadItemAsyncResult;

                using (Stream contentStream = streamAttachment.GetContentStream())
                {
                    byte[] array = new byte[contentStream.Length];
                    while (contentStream.Position != contentStream.Length)
                    {
                        int count = (int)Math.Min(4000L, contentStream.Length - contentStream.Position);
                        contentStream.Read(array, (int)contentStream.Position, count);
                    }
                    CancellationToken cancellationToken = default(CancellationToken);
                    uploadItemAsyncResult = this.provider.UploadItemSync(array, attachment.FileName, cancellationToken);
                }
                if (uploadItemAsyncResult.ResultCode != AttachmentResultCode.Success)
                {
                    throw new GetWacAttachmentInfo.AttachmentUploadException(uploadItemAsyncResult.ResultCode.ToString());
                }
                UserContext userContext = UserContextManager.GetUserContext(CallContext.Current.HttpContext, CallContext.Current.EffectiveCaller, true);

                this.ResultAttachmentWebServiceUrl = uploadItemAsyncResult.Item.ProviderEndpointUrl;
                this.ResultAttachmentProviderType  = uploadItemAsyncResult.Item.ProviderType.ToString();
                this.ResultAttachmentContentUrl    = this.provider.GetItemAbsoulteUrl(userContext, uploadItemAsyncResult.Item.Location, uploadItemAsyncResult.Item.ProviderEndpointUrl, null, null);
                this.ResultAttachmentExtension     = attachment.FileExtension;
                IReferenceAttachment referenceAttachment = (IReferenceAttachment)this.factory.CreateAttachment(draftItem, AttachmentType.Reference);

                newAttachment   = referenceAttachment;
                newAttachmentId = newAttachment.Id;
                referenceAttachment.AttachLongPathName  = this.ResultAttachmentContentUrl;
                referenceAttachment.ProviderEndpointUrl = this.ResultAttachmentWebServiceUrl;
                referenceAttachment.ProviderType        = this.ResultAttachmentProviderType;
                referenceAttachment.FileName            = attachment.FileName;
                newAttachment.IsInline = false;
                newAttachment.Save();
                draftItem.Save(SaveMode.NoConflictResolutionForceSave);
            }