Beispiel #1
0
        public static Stream ConvertItemToMime(StoreObject xsoItem)
        {
            Item item = xsoItem as Item;

            if (item == null)
            {
                throw new UnexpectedTypeException("Item", xsoItem);
            }
            OutboundConversionOptions outboundConversionOptions = AirSyncUtility.GetOutboundConversionOptions();
            PolicyData policyData = ADNotificationManager.GetPolicyData(Command.CurrentCommand.User);

            if (policyData != null && !policyData.AttachmentsEnabled)
            {
                outboundConversionOptions.FilterAttachmentHandler = ((Item item1, Attachment attachment) => false);
            }
            item.Load(StoreObjectSchema.ContentConversionProperties);
            AirSyncStream airSyncStream = new AirSyncStream();

            try
            {
                if (BodyConversionUtilities.IsMessageRestrictedAndDecoded(item))
                {
                    ItemConversion.ConvertItemToMime(((RightsManagedMessageItem)item).DecodedItem, airSyncStream, outboundConversionOptions);
                }
                else
                {
                    ItemConversion.ConvertItemToMime(item, airSyncStream, outboundConversionOptions);
                }
            }
            catch (ConversionFailedException innerException)
            {
                throw new ConversionException(string.Format(CultureInfo.InvariantCulture, "MIME conversion failed for Item {0}", new object[]
                {
                    item
                }), innerException);
            }
            return(airSyncStream);
        }
        internal static string GetAttachment(MailboxSession mailboxSession, StoreObjectId itemId, string attachmentId, Stream outStream, int offset, int count, Unlimited <ByteQuantifiedSize> maxAttachmentSize, bool rightsManagementSupport, out int total)
        {
            string result;

            using (Item item = Item.Bind(mailboxSession, itemId))
            {
                if ("DRMLicense".Equals(attachmentId, StringComparison.OrdinalIgnoreCase))
                {
                    total  = AttachmentHelper.WriteDrmLicenseToStream(item, outStream, offset, count, maxAttachmentSize);
                    result = "application/x-microsoft-rpmsg-message-license";
                }
                else
                {
                    if (rightsManagementSupport)
                    {
                        Command.CurrentCommand.DecodeIrmMessage(item, true);
                    }
                    Attachment attachment = null;
                    try
                    {
                        AttachmentCollection attachmentCollection;
                        if (BodyConversionUtilities.IsMessageRestrictedAndDecoded(item))
                        {
                            attachmentCollection = ((RightsManagedMessageItem)item).ProtectedAttachmentCollection;
                        }
                        else
                        {
                            attachmentCollection = item.AttachmentCollection;
                        }
                        if (attachmentId.Length > 8)
                        {
                            AttachmentId attachmentId2 = EntitySyncItemId.GetAttachmentId(attachmentId);
                            if (attachmentId2 != null)
                            {
                                attachment = attachmentCollection.Open(attachmentId2);
                            }
                        }
                        if (attachment == null)
                        {
                            int num;
                            if (!int.TryParse(attachmentId, NumberStyles.None, CultureInfo.InvariantCulture, out num) || num < 0)
                            {
                                throw new FormatException("Invalid Attachment Index format: " + attachmentId.ToString(CultureInfo.InvariantCulture));
                            }
                            IList <AttachmentHandle> handles = attachmentCollection.GetHandles();
                            if (num < handles.Count)
                            {
                                attachment = attachmentCollection.Open(handles[num]);
                            }
                        }
                        if (attachment == null)
                        {
                            throw new ObjectNotFoundException(ServerStrings.MapiCannotOpenAttachmentId(attachmentId));
                        }
                        if (BodyConversionUtilities.IsMessageRestrictedAndDecoded(item) && AttachmentHelper.IsProtectedVoiceAttachment(attachment.FileName))
                        {
                            RightsManagedMessageItem rightsManagedMessageItem = item as RightsManagedMessageItem;
                            rightsManagedMessageItem.UnprotectAttachment(attachment.Id);
                            AttachmentId id = attachment.Id;
                            attachment.Dispose();
                            attachment = rightsManagedMessageItem.ProtectedAttachmentCollection.Open(id);
                        }
                        if (!maxAttachmentSize.IsUnlimited && attachment.Size > (long)maxAttachmentSize.Value.ToBytes())
                        {
                            throw new DataTooLargeException(StatusCode.AttachmentIsTooLarge);
                        }
                        result = AttachmentHelper.GetAttachmentItself(attachment, outStream, offset, count, out total);
                    }
                    finally
                    {
                        if (attachment != null)
                        {
                            attachment.Dispose();
                        }
                        if (rightsManagementSupport)
                        {
                            Command.CurrentCommand.SaveLicense(item);
                        }
                    }
                }
            }
            return(result);
        }