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); }
private static string GetAttachmentByUrlCompName(MailboxSession mailboxSession, string attachmentId, Stream outStream, int offset, int count, Unlimited <ByteQuantifiedSize> maxAttachmentSize, out int total) { attachmentId = "/" + attachmentId; int andCheckLastSlashLocation = AttachmentHelper.GetAndCheckLastSlashLocation(attachmentId, attachmentId); string text = attachmentId.Substring(0, andCheckLastSlashLocation); string text2 = attachmentId.Substring(andCheckLastSlashLocation + 1); andCheckLastSlashLocation = AttachmentHelper.GetAndCheckLastSlashLocation(text, attachmentId); string propertyValue = text.Substring(0, andCheckLastSlashLocation); StoreId storeId = null; QueryFilter seekFilter = new ComparisonFilter(ComparisonOperator.Equal, FolderSchema.UrlName, propertyValue); using (Folder folder = Folder.Bind(mailboxSession, mailboxSession.GetDefaultFolderId(DefaultFolderType.Root))) { using (QueryResult queryResult = folder.FolderQuery(FolderQueryFlags.DeepTraversal, AirSyncUtility.XsoFilters.GetHierarchyFilter, null, AttachmentHelper.folderPropertyIds)) { if (queryResult.SeekToCondition(SeekReference.OriginBeginning, seekFilter)) { object[][] rows = queryResult.GetRows(1); storeId = (rows[0][0] as StoreId); } } } if (storeId == null) { throw new ObjectNotFoundException(ServerStrings.MapiCannotOpenAttachmentId(attachmentId)); } AirSyncDiagnostics.TraceDebug <StoreId>(ExTraceGlobals.RequestsTracer, null, "Getting attachment with parentStoreId {0}.", storeId); StoreId storeId2 = null; seekFilter = new ComparisonFilter(ComparisonOperator.Equal, FolderSchema.UrlName, text); using (Folder folder2 = Folder.Bind(mailboxSession, storeId)) { using (QueryResult queryResult2 = folder2.ItemQuery(ItemQueryType.None, null, null, AttachmentHelper.itemPropertyIds)) { if (queryResult2.SeekToCondition(SeekReference.OriginBeginning, seekFilter)) { object[][] rows2 = queryResult2.GetRows(1); storeId2 = (rows2[0][0] as StoreId); } } } if (storeId2 == null) { throw new ObjectNotFoundException(ServerStrings.MapiCannotOpenAttachmentId(attachmentId)); } AirSyncDiagnostics.TraceDebug <StoreId>(ExTraceGlobals.RequestsTracer, null, "Getting attachment with itemStoreId {0}.", storeId2); PropertyTagPropertyDefinition propertyTagPropertyDefinition = PropertyTagPropertyDefinition.CreateCustom("UrlCompName", 284360734U); string attachmentItself; using (Item item = Item.Bind(mailboxSession, storeId2)) { AttachmentCollection attachmentCollection = item.AttachmentCollection; attachmentCollection.Load(new PropertyDefinition[] { propertyTagPropertyDefinition }); AttachmentId attachmentId2 = null; foreach (AttachmentHandle handle in attachmentCollection) { using (Attachment attachment = attachmentCollection.Open(handle)) { if (text2.Equals((string)attachment[propertyTagPropertyDefinition], StringComparison.Ordinal)) { attachmentId2 = attachment.Id; break; } } } if (attachmentId2 == null) { foreach (AttachmentHandle handle2 in attachmentCollection) { using (Attachment attachment2 = attachmentCollection.Open(handle2)) { string str = (string)attachment2[propertyTagPropertyDefinition]; if (text2.EndsWith("_" + str, StringComparison.Ordinal)) { attachmentId2 = attachment2.Id; break; } } } } if (attachmentId2 == null) { throw new ObjectNotFoundException(ServerStrings.MapiCannotOpenAttachmentId(attachmentId)); } AirSyncDiagnostics.TraceDebug <AttachmentId>(ExTraceGlobals.RequestsTracer, null, "Getting attachment with attachment ID {0}.", attachmentId2); using (Attachment attachment3 = attachmentCollection.Open(attachmentId2)) { if (!maxAttachmentSize.IsUnlimited && attachment3.Size > (long)maxAttachmentSize.Value.ToBytes()) { throw new DataTooLargeException(StatusCode.AttachmentIsTooLarge); } attachmentItself = AttachmentHelper.GetAttachmentItself(attachment3, outStream, offset, count, out total); } } return(attachmentItself); }