Example #1
0
 public void Can_get_MimeType_file_extension()
 {
     Assert.That(MimeTypes.GetExtension(MimeTypes.Html), Is.EqualTo(".html"));
     Assert.That(MimeTypes.GetExtension(MimeTypes.HtmlUtf8), Is.EqualTo(".html"));
     Assert.That(MimeTypes.GetExtension(MimeTypes.ImagePng), Is.EqualTo(".png"));
     Assert.That(MimeTypes.GetExtension(MimeTypes.ImageSvg), Is.EqualTo(".svg"));
 }
        public override void Clear(params string[] cacheKeys)
        {
            var ext = MimeTypes.GetExtension(MimeTypes.Json);
            var cacheKeyWithExts = cacheKeys.ToList().ConvertAll(x => x + ext);

            this.CacheClient.RemoveAll(cacheKeyWithExts);
        }
        public string Resolve <T>(string cacheKey, TimeSpan?expiresIn, Func <T> createCacheFn) where T : class
        {
            cacheKey = cacheKey + MimeTypes.GetExtension(MimeTypes.Json);

            var result = this.CacheClient.Get <string>(cacheKey);

            if (result != null)
            {
                return(result);
            }

            var cacheValue = createCacheFn();

            var cacheValueText = JsonDataContractSerializer.Instance.Parse(cacheValue);

            if (expiresIn.HasValue)
            {
                this.CacheClient.Set(cacheKey, cacheValueText, expiresIn.Value);
            }
            else
            {
                this.CacheClient.Set(cacheKey, cacheValueText);
            }

            return(cacheValueText);
        }
        /// <summary>Resolves.</summary>
        ///
        /// <typeparam name="T">Generic type parameter.</typeparam>
        /// <param name="compressionType">Type of the compression.</param>
        /// <param name="cacheKey">       The cache key.</param>
        /// <param name="createCacheFn">  The create cache function.</param>
        ///
        /// <returns>An object.</returns>
        public object Resolve <T>(string compressionType, string cacheKey, Func <T> createCacheFn)
            where T : class
        {
            var contentTypeCacheKey = cacheKey + MimeTypes.GetExtension(this.ContentType);

            var acceptsCompress = !string.IsNullOrEmpty(compressionType);
            var cacheKeyZip     = acceptsCompress
                ? contentTypeCacheKey + CompressionTypes.GetExtension(compressionType)
                : null;

            if (acceptsCompress)
            {
                var fromCache = GetCompressedBytesFromCache(compressionType, cacheKeyZip);
                if (fromCache != null)
                {
                    return(fromCache);
                }

                var fromFile = GetCompressedBytesFromFile(compressionType, cacheKeyZip);
                if (fromFile != null)
                {
                    return(fromFile);
                }
            }
            else
            {
                var result = this.CacheClient.Get <string>(cacheKey);
                if (result != null)
                {
                    return(result);
                }

                var fromFile = GetFromFile(contentTypeCacheKey);
                if (fromFile != null)
                {
                    return(fromFile);
                }
            }

            var cacheValueString = this.cacheManager.ResolveText(cacheKey, createCacheFn);

            WriteToFile(contentTypeCacheKey, Encoding.UTF8.GetBytes(cacheValueString));

            if (acceptsCompress)
            {
                var cacheValueZip = cacheValueString.Compress(compressionType);

                this.CacheClient.Set(cacheKeyZip, cacheValueZip);
                WriteToFile(cacheKeyZip, cacheValueZip);

                return(new CompressedResult(cacheValueZip, compressionType, this.ContentType));
            }

            return(cacheValueString);
        }
Example #5
0
    public override StaticContent?Get(string path)
    {
        var file = VirtualFiles.GetFile(DirPath.CombineWith(path));

        if (file == null)
        {
            return(null);
        }

        return(new StaticContent(file.GetBytesContentsAsMemory(), MimeTypes.GetExtension(file.Extension)));
    }
Example #6
0
        public string ObterTipoArquivo()
        {
            ////data:image/jpeg;base64,
            var posicaoDoisPontos   = this.FormatoBase64.IndexOf(":", System.StringComparison.Ordinal) + 1;
            var posicaoPontoVirgula = this.FormatoBase64.IndexOf(";", System.StringComparison.Ordinal);

            var mime = this.FormatoBase64.Substring(
                posicaoDoisPontos,
                posicaoPontoVirgula - posicaoDoisPontos);

            return(MimeTypes.GetExtension(mime));
        }
        public CacheKeyTuple GetCacheKeys <T>(string id)
        {
            var cacheKey = IdUtils.CreateCacheKeyPath <T>(id);

            var contentType     = cacheKey + MimeTypes.GetExtension(MimeTypes.Xml);
            var compressionType = contentType + CompressionTypes.GetExtension(CompressionTypes.Deflate);

            return(new CacheKeyTuple {
                CacheKey = cacheKey,
                ContentTypeCacheKey = contentType,
                CompressionTypeCacheKey = compressionType,
            });
        }
        /// <summary>Clears this object to its blank/initial state.</summary>
        ///
        /// <param name="cacheKeys">A variable-length parameters list containing cache keys.</param>
        public void Clear(params string[] cacheKeys)
        {
            this.cacheManager.Clear(cacheKeys);

            var contentTypeCacheKeys = cacheKeys.ToList().ConvertAll(x => x + MimeTypes.GetExtension(this.ContentType));

            foreach (var cacheKey in contentTypeCacheKeys)
            {
                var filePath        = Path.Combine(this.baseCachePath, cacheKey);
                var gzipFilePath    = Path.Combine(this.baseCachePath, cacheKey + CompressionTypes.GetExtension(CompressionTypes.GZip));
                var deflateFilePath = Path.Combine(this.baseCachePath, cacheKey + CompressionTypes.GetExtension(CompressionTypes.Deflate));

                DeleteFiles(filePath, gzipFilePath, deflateFilePath);
            }
        }
Example #9
0
        private IDictionary <string, byte[]> GenerateFile(ReportNode report, string name, string format)
        {
            var source = new GroupMessage(null,
                                          null,
                                          PX.Common.Mail.MailSender.MessageAddressee.Empty,
                                          PX.Common.Mail.MailSender.MessageContent.Empty,
                                          null,
                                          format,
                                          MessageRelationship.Empty, ReportAttachment.Empty);
            var message     = new PX.Reports.Mail.Message(source, report, null);
            var attachments = message.Attachments;

            if (attachments == null)
            {
                return(null);
            }

            var result = new Dictionary <string, byte[]>(attachments.Count);
            var index  = 0;

            foreach (var item in attachments)
            {
                var filename = item.Name;
                if (!string.IsNullOrEmpty(name))
                {
                    filename = name;
                    if (index++ > 0)
                    {
                        filename += " (" + index.ToString() + ")";
                    }
                }
                if (!System.IO.Path.HasExtension(item.Name))
                {
                    var ext = MimeTypes.GetExtension(item.MimeType);
                    if (ext != null)
                    {
                        filename = System.IO.Path.GetFileNameWithoutExtension(filename) + ext;
                    }
                }
                result.Add(filename, item.ToArray());
            }
            return(result);
        }
Example #10
0
 public static string GetCacheKeyForSerialized(string cacheKey, string mimeType, string modifiers)
 {
     return(cacheKey + MimeTypes.GetExtension(mimeType) + modifiers);
 }
Example #11
0
 public static string GetSerializedCacheKey(string cacheKey, string mimeType)
 {
     return(cacheKey + MimeTypes.GetExtension(mimeType));
 }