From 1070cffcf905f77d694e140d0c9e978f5d0052c0 Mon Sep 17 00:00:00 2001 From: Diva Canto Date: Wed, 8 Dec 2010 18:53:15 -0800 Subject: Added ability for GetTexture to serve multiple formats. The format may come as an extra query parameter in the URL format= (this was tested and working) or it may come in the Accept header (code added, but not tested). The result of the conversion is placed in the asset cache, under the name -. --- OpenSim/Framework/WebUtil.cs | 81 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) (limited to 'OpenSim/Framework') diff --git a/OpenSim/Framework/WebUtil.cs b/OpenSim/Framework/WebUtil.cs index d16f9bf..1c856af 100644 --- a/OpenSim/Framework/WebUtil.cs +++ b/OpenSim/Framework/WebUtil.cs @@ -26,6 +26,7 @@ */ using System; +using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; @@ -363,5 +364,85 @@ namespace OpenSim.Framework } #endregion Stream + + public class QBasedComparer : IComparer + { + public int Compare(Object x, Object y) + { + float qx = GetQ(x); + float qy = GetQ(y); + if (qx < qy) + return -1; + if (qx == qy) + return 0; + return 1; + } + + private float GetQ(Object o) + { + // Example: image/png;q=0.9 + + if (o is String) + { + string mime = (string)o; + string[] parts = mime.Split(new char[] { ';' }); + if (parts.Length > 1) + { + string[] kvp = parts[1].Split(new char[] { '=' }); + if (kvp.Length == 2 && kvp[0] == "q") + { + float qvalue = 1F; + float.TryParse(kvp[1], out qvalue); + return qvalue; + } + } + } + + return 1F; + } + } + + /// + /// Takes the value of an Accept header and returns the preferred types + /// ordered by q value (if it exists). + /// Example input: image/jpg;q=0.7, image/png;q=0.8, image/jp2 + /// Exmaple output: ["jp2", "png", "jpg"] + /// NOTE: This doesn't handle the semantics of *'s... + /// + /// + /// + public static string[] GetPreferredImageTypes(string accept) + { + + if (accept == null || accept == string.Empty) + return new string[0]; + + string[] types = accept.Split(new char[] { ',' }); + if (types.Length > 0) + { + List list = new List(types); + list.RemoveAll(delegate(string s) { return !s.ToLower().StartsWith("image"); }); + ArrayList tlist = new ArrayList(list); + tlist.Sort(new QBasedComparer()); + + string[] result = new string[tlist.Count]; + for (int i = 0; i < tlist.Count; i++) + { + string mime = (string)tlist[i]; + string[] parts = mime.Split(new char[] { ';' }); + string[] pair = parts[0].Split(new char[] { '/' }); + if (pair.Length == 2) + result[i] = pair[1].ToLower(); + else // oops, we don't know what this is... + result[i] = pair[0]; + } + + return result; + } + + return new string[0]; + } + + } } -- cgit v1.1