public async Task <byte[]> DownloadAttachment(IAttachment attachment)
        {
            if (attachment.GetType() != typeof(SFAttachment))
            {
                return(null);
            }
            var sfattachment = (SFAttachment)attachment;
            var client       = await GetForceClient();

            string fileName = sfattachment.Name;
            string bodyUrl  = client.InstanceUrl + sfattachment.Body;

            HttpWebRequest aRequest = (HttpWebRequest)WebRequest.Create(bodyUrl);

            aRequest.Headers.Add("Authorization: Bearer " + client.AccessToken);
            aRequest.Method = "GET";

            HttpWebResponse aResponse = (HttpWebResponse)aRequest.GetResponse();

            byte[] doc = null;
            using (MemoryStream ms = new MemoryStream())
            {
                aResponse.GetResponseStream().CopyTo(ms);
                doc = ms.ToArray();
            }
            return(doc);
        }
        /// <summary>
        /// Gets the type name of an attachment minus "Attachment".
        /// </summary>
        /// <param name="attachment">An attachment to get the type name for.</param>
        /// <returns>A type name.</returns>
        public static string GetTypeName(this IAttachment attachment)
        {
            if (attachment == null)
            {
                throw new ArgumentNullException(nameof(attachment));
            }

            return(attachment.GetType().Name.Replace(_typeNameSuffix, string.Empty));
        }
        public async void DownloadAndOpenAttachment(IAttachment attachment)
        {
            if (attachment.GetType() != typeof(SFAttachment))
            {
                return;
            }
            var sfattachment = (SFAttachment)attachment;
            var client       = await GetForceClient();

            string fileName = sfattachment.Name;
            string bodyUrl  = client.InstanceUrl + sfattachment.Body;

            HttpWebRequest aRequest = (HttpWebRequest)WebRequest.Create(bodyUrl);

            aRequest.Headers.Add("Authorization: Bearer " + client.AccessToken);
            aRequest.Method = "GET";

            HttpWebResponse aResponse = (HttpWebResponse)aRequest.GetResponse();

            byte[] doc = null;
            using (MemoryStream ms = new MemoryStream())
            {
                aResponse.GetResponseStream().CopyTo(ms);
                doc = ms.ToArray();
            }

            var path = AppDomain.CurrentDomain.BaseDirectory;

            if (!Directory.Exists(path + "/temp"))
            {
                Directory.CreateDirectory(path + "/temp");
            }
            File.WriteAllBytes($"{path}/temp/{fileName}", doc);

            System.Diagnostics.Process.Start($"{path}/temp/{fileName}");
        }
            // 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);
            }
Exemple #5
0
        // Token: 0x06000767 RID: 1895 RVA: 0x00029340 File Offset: 0x00027540
        protected override int InternalExecute(int count)
        {
            string[] array = HttpUtility.UrlDecode(base.FileReference).Split(new char[]
            {
                ':'
            });
            if (array.Length != 2)
            {
                throw new AirSyncPermanentException(StatusCode.Sync_TooManyFolders, false)
                      {
                          ErrorStringForProtocolLogger = "InvalidEntityAttachemtnId"
                      };
            }
            StoreObjectId itemId = StoreId.EwsIdToStoreObjectId(array[0]);
            IEvents       events = EntitySyncItem.GetEvents(this.CalendaringContainer, base.Session, itemId);

            if (events == null)
            {
                throw new AirSyncPermanentException(StatusCode.Sync_ClientServerConversion, false)
                      {
                          ErrorStringForProtocolLogger = "EventsNotFound"
                      };
            }
            IAttachments attachments = events[array[0]].Attachments;

            if (attachments == null)
            {
                throw new AirSyncPermanentException(StatusCode.Sync_ClientServerConversion, false)
                      {
                          ErrorStringForProtocolLogger = "EntityNotFound"
                      };
            }
            IAttachment attachment = attachments.Read(array[1], null);

            if (attachment == null)
            {
                throw new AirSyncPermanentException(StatusCode.Sync_ClientServerConversion, false)
                      {
                          ErrorStringForProtocolLogger = "EntityAttachementNotFound"
                      };
            }
            base.ContentType = attachment.ContentType;
            FileAttachment fileAttachment = attachment as FileAttachment;
            ItemAttachment itemAttachment = attachment as ItemAttachment;

            if (fileAttachment != null)
            {
                if (!base.MaxAttachmentSize.IsUnlimited && fileAttachment.Content.Length > (int)base.MaxAttachmentSize.Value.ToBytes())
                {
                    throw new DataTooLargeException(StatusCode.AttachmentIsTooLarge);
                }
                count = ((count == -1) ? fileAttachment.Content.Length : Math.Min(count, fileAttachment.Content.Length - base.MinRange));
                base.OutStream.Write(fileAttachment.Content, base.MinRange, count);
                return(count);
            }
            else
            {
                if (itemAttachment != null)
                {
                    int result;
                    AttachmentHelper.GetAttachment(base.Session, itemId, attachment.Id, base.OutStream, base.MinRange, count, base.MaxAttachmentSize, base.RightsManagementSupport, out result);
                    return(result);
                }
                throw new AirSyncPermanentException(StatusCode.Sync_InvalidWaitTime, new LocalizedString(string.Format("Attachment type \"{0}\" is not supported.", attachment.GetType().FullName)), false)
                      {
                          ErrorStringForProtocolLogger = "UnsupportedEntityAttachementType"
                      };
            }
        }
Exemple #6
0
        private Attachment14Data CreateAttachment14Data(IAttachment attachment)
        {
            Attachment14Data attachment14Data = new Attachment14Data();

            attachment14Data.DisplayName       = attachment.Name;
            attachment14Data.EstimatedDataSize = attachment.Size;
            attachment14Data.IsInline          = attachment.IsInline;
            attachment14Data.FileReference     = HttpUtility.UrlEncode(base.Item.Id + ":" + attachment.Id);
            attachment14Data.Id = EntitySyncItemId.GetAttachmentId(attachment.Id);
            FileAttachment fileAttachment = attachment as FileAttachment;
            ItemAttachment itemAttachment = attachment as ItemAttachment;

            if (fileAttachment != null)
            {
                attachment14Data.Method          = 1;
                attachment14Data.ContentId       = fileAttachment.ContentId;
                attachment14Data.ContentLocation = fileAttachment.ContentLocation;
                if (fileAttachment.Content != null)
                {
                    attachment14Data.EstimatedDataSize = (long)fileAttachment.Content.Length;
                }
            }
            else
            {
                if (itemAttachment == null)
                {
                    throw new ConversionException(string.Format("Attachment type \"{0}\" is not supported.", attachment.GetType().FullName));
                }
                attachment14Data.Method = 5;
            }
            return(attachment14Data);
        }