Ejemplo n.º 1
0
        public StreamAttachment ConvertToImageAttachment(CoreAttachmentCollection collection, ImageFormat format)
        {
            base.CheckDisposed("ConvertToImageAttachment");
            Util.ThrowOnNullArgument(collection, "collection");
            EnumValidator.ThrowIfInvalid <ImageFormat>(format);
            StreamAttachment result;

            using (DisposeGuard disposeGuard = default(DisposeGuard))
            {
                CoreAttachment coreAttachment = collection.InternalCreateCopy(new AttachmentType?(AttachmentType.Stream), base.CoreAttachment);
                disposeGuard.Add <CoreAttachment>(coreAttachment);
                StreamAttachment streamAttachment = (StreamAttachment)AttachmentCollection.CreateTypedAttachment(coreAttachment, new AttachmentType?(AttachmentType.Stream));
                disposeGuard.Add <StreamAttachment>(streamAttachment);
                string text = streamAttachment.FileName;
                if (string.IsNullOrEmpty(text))
                {
                    text = Attachment.GenerateFilename();
                }
                string str = null;
                switch (format)
                {
                case ImageFormat.Jpeg:
                    str = ".jpg";
                    break;

                case ImageFormat.Png:
                    str = ".png";
                    break;
                }
                streamAttachment.FileName    = text + str;
                streamAttachment.ContentType = "image/jpeg";
                streamAttachment.IsInline    = true;
                using (Stream contentStream = streamAttachment.GetContentStream(PropertyOpenMode.Create))
                {
                    if (!this.TryConvertToImage(contentStream, ImageFormat.Jpeg))
                    {
                        ConvertUtils.SaveDefaultImage(contentStream);
                    }
                }
                disposeGuard.Success();
                result = streamAttachment;
            }
            return(result);
        }
Ejemplo n.º 2
0
        internal static void CoreObjectUpdateStreamAttachmentName(CoreAttachment coreAttachment)
        {
            ICorePropertyBag propertyBag = coreAttachment.PropertyBag;
            string           text        = (propertyBag.TryGetProperty(InternalSchema.AttachLongFileName) as string) ?? (propertyBag.TryGetProperty(InternalSchema.AttachFileName) as string);
            string           text2       = propertyBag.TryGetProperty(InternalSchema.DisplayName) as string;
            string           text3       = propertyBag.TryGetProperty(InternalSchema.AttachExtension) as string;

            if (!string.IsNullOrEmpty(text))
            {
                text = Attachment.TrimFilename(text);
            }
            if (!string.IsNullOrEmpty(text2))
            {
                text2 = Attachment.TrimFilename(text2);
            }
            if (!string.IsNullOrEmpty(text3))
            {
                text3 = '.' + Attachment.TrimFilename(text3);
            }
            string text4 = null;
            string text5 = null;
            string text6 = null;
            string text7 = null;

            Attachment.TryFindFileExtension(text, out text5, out text4);
            Attachment.TryFindFileExtension(text2, out text7, out text6);
            if (!string.IsNullOrEmpty(text5))
            {
                text3 = text5;
            }
            if (!string.IsNullOrEmpty(text7) && string.Compare(text3, text7, StringComparison.OrdinalIgnoreCase) != 0)
            {
                text6 += text7;
            }
            if (string.IsNullOrEmpty(text4))
            {
                text4 = Attachment.GenerateFilename();
            }
            if (EmailMessageHelpers.IsGeneratedFileName(text4) && string.IsNullOrEmpty(text3))
            {
                string text8 = propertyBag.TryGetProperty(InternalSchema.AttachMimeTag) as string;
                if (!string.IsNullOrEmpty(text8))
                {
                    text3 = StreamAttachmentBase.GetExtensionByContentType(text8);
                }
            }
            if (string.IsNullOrEmpty(text6))
            {
                text6 = text4;
            }
            if (!string.IsNullOrEmpty(text3))
            {
                text  = text4 + text3;
                text2 = text6 + text3;
            }
            else
            {
                text  = text4;
                text2 = text6;
                propertyBag.Delete(InternalSchema.AttachExtension);
            }
            propertyBag[InternalSchema.AttachLongFileName] = text;
            propertyBag[InternalSchema.DisplayName]        = text2;
            propertyBag[InternalSchema.AttachExtension]    = (text3 ?? string.Empty);
            bool      flag          = false;
            ICoreItem containerItem = coreAttachment.ParentCollection.ContainerItem;

            if (containerItem != null)
            {
                containerItem.PropertyBag.Load(new PropertyDefinition[]
                {
                    InternalSchema.IsAssociated
                });
                flag = containerItem.PropertyBag.GetValueOrDefault <bool>(InternalSchema.IsAssociated, false);
            }
            if (!flag)
            {
                propertyBag[InternalSchema.AttachFileName] = Attachment.Make8x3FileName(text, coreAttachment != null && coreAttachment.Session != null && coreAttachment.Session.IsMoveUser);
            }
        }