public T Fetch <T>(string assemblyWithResource) where T : class
 {
     foreach (var contentInstance in _contentInstancesCache)
     {
         if (contentInstance.GetType() == typeof(T))
         {
             return((T)contentInstance);
         }
     }
     using (var dataStream = _dataStreamHelper.GetResourceStream <T>(".json", assemblyWithResource))
     {
         _contentInstancesCache.Add(_jsonDeserializer.Deserialize <T>(dataStream));
         return((T)_contentInstancesCache.Single(i => typeof(T) == i.GetType()));
     }
 }
Exemple #2
0
        private static Stream GetFileStream(IResource resource, ref IResource source)
        {
            #region Preconditions
            if (resource == null)
            {
                throw new ArgumentNullException("resource", "FileTypes -- Input resource is null or empty.");
            }
            #endregion Preconditions

            source = GetSource(resource);
            if (source != null)
            {
                IStreamProvider provider = Core.PluginLoader.GetStreamProvider(source.Type);
                if (provider != null)
                {
                    try
                    {
                        Stream resourceStream = provider.GetResourceStream(resource);
                        if (!resourceStream.CanRead)
                        {
                            throw new Exception("Resource stream returned for resource of type " +
                                                resource.Type + ", source resource type " + source.Type + " cannot be read");
                        }
                        return(resourceStream);
                    }
                    catch {}
                }
            }
            return(null);
        }
 // Deserialise the JSON into our models by using our EmbeddedStreamProvider service to read the JSON contents
 public T Fetch()
 {
     using (var dataStream = _dataStreamHelper.GetResourceStream <T>("json"))
         using (var readDataStream = new StreamReader(dataStream))
         {
             return(_jsonDeserializer.Deserialize <T>(readDataStream.ReadToEnd()));
         }
 }
Exemple #4
0
        private static string AttachmentPicture(IResource res, IResource attach)
        {
            bool   isConveted = false;
            int    picWidth, picHeight;
            string cachedFileName = Path.Combine(Path.GetTempPath(), attach.GetStringProp(Core.Props.Name));

            //  Browser completely dislikes ".ico" files (I can only agree with it...) -
            //  convert them to ".png" files for uniformity.
            //  TODO: correct the icons background corresponding to html div's background.

            string ext = Path.GetExtension(cachedFileName);

            if (ext.Equals(".ico", StringComparison.OrdinalIgnoreCase))
            {
                int index = cachedFileName.LastIndexOf(".ico", StringComparison.OrdinalIgnoreCase);
                cachedFileName = cachedFileName.Substring(0, index) + ".png";
                isConveted     = true;
            }

            //  Check whether the attachment has been already downloaded once, and
            //  if so do not request the attachment stream from the Outlook (since
            //  this is rather costly).
            if (File.Exists(cachedFileName) &&
                attach.HasProp(PROP.AttachmentPicWidth) && attach.HasProp(PROP.AttachmentPicHeight))
            {
                picWidth  = attach.GetIntProp(PROP.AttachmentPicWidth);
                picHeight = attach.GetIntProp(PROP.AttachmentPicHeight);
            }
            else
            {
                IStreamProvider provider     = Core.PluginLoader.GetStreamProvider(res.Type);
                Stream          attachStream = provider.GetResourceStream(attach);
                Image           img          = Image.FromStream(attachStream);

                if (isConveted)
                {
                    img.Save(cachedFileName, ImageFormat.Png);
                }
                else
                {
                    img.Save(cachedFileName);
                }

                picWidth  = img.Width;
                picHeight = img.Height;
                new ResourceProxy(attach).SetPropAsync(PROP.AttachmentPicWidth, picWidth);
                new ResourceProxy(attach).SetPropAsync(PROP.AttachmentPicHeight, picHeight);
            }

            StringBuilder sb = StringBuilderPool.Alloc();

            if (picWidth <= 70 && picHeight <= 70)
            {
                sb.Append("\t\t<div class=\"picture ico\"><p><img src=\"").Append(cachedFileName).Append("\" /></p></div>");
            }
            else
            {
                int    maxSide     = Math.Max(picWidth, picHeight);
                double ratio       = maxSide / 70.0;
                int    thumbWidth  = (int)(picWidth / ratio),
                       thumbHeight = (int)(picHeight / ratio);
                sb.Append("\t\t<div class=\"picture\">\n\t\t\t<img src=\"").Append(cachedFileName).Append("\"");
                sb.Append(" width=\"").Append(thumbWidth).Append("\" height=\"").Append(thumbHeight).Append("\" ").
                Append(" onclick=\"resize(this.id, ").Append(thumbWidth).Append(", ").Append(thumbHeight).Append(", ").
                Append(picWidth).Append(", ").Append(picHeight).Append(")\" id=\"").Append(attach.GetStringProp(Core.Props.Name)).Append("\"");
                sb.Append(" /><p class=\"fullSize\">.</p>\n");
            }
            return(sb.ToString());
        }