123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
-
- using System;
- using System.Collections;
- using System.Collections.Specialized;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Reflection;
- using System.IO;
- using System.Web;
- using log4net;
- using Nini.Config;
- using OpenMetaverse;
- using OpenMetaverse.StructuredData;
- using OpenMetaverse.Imaging;
- using OpenSim.Framework;
- using OpenSim.Framework.Servers;
- using OpenSim.Framework.Servers.HttpServer;
- using OpenSim.Region.Framework.Interfaces;
- using OpenSim.Services.Interfaces;
- using Caps = OpenSim.Framework.Capabilities.Caps;
- namespace OpenSim.Capabilities.Handlers
- {
- public class GetTextureRobustHandler : BaseStreamHandler
- {
- private static readonly ILog m_log =
- LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- private IAssetService m_assetService;
- public const string DefaultFormat = "x-j2c";
-
- private string m_RedirectURL = null;
- public GetTextureRobustHandler(string path, IAssetService assService, string name, string description, string redirectURL)
- : base("GET", path, name, description)
- {
- m_assetService = assService;
- m_RedirectURL = redirectURL;
- if (m_RedirectURL != null && !m_RedirectURL.EndsWith("/"))
- m_RedirectURL += "/";
- }
- protected override byte[] ProcessRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
- {
-
- NameValueCollection query = HttpUtility.ParseQueryString(httpRequest.Url.Query);
- string textureStr = query.GetOne("texture_id");
- string format = query.GetOne("format");
-
- if (m_assetService == null)
- {
- m_log.Error("[GETTEXTURE]: Cannot fetch texture " + textureStr + " without an asset service");
- httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
- return null;
- }
- UUID textureID;
- if (!String.IsNullOrEmpty(textureStr) && UUID.TryParse(textureStr, out textureID))
- {
- string[] formats;
- if (!string.IsNullOrEmpty(format))
- {
- formats = new string[1] { format.ToLower() };
- }
- else
- {
- formats = WebUtil.GetPreferredImageTypes(httpRequest.Headers.Get("Accept"));
- if (formats.Length == 0)
- formats = new string[1] { DefaultFormat };
- }
-
- httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
- foreach (string f in formats)
- {
- if (FetchTexture(httpRequest, httpResponse, textureID, f))
- break;
- }
- }
- else
- {
- m_log.Warn("[GETTEXTURE]: Failed to parse a texture_id from GetTexture request: " + httpRequest.Url);
- }
- return null;
- }
-
-
-
-
-
-
-
-
- private bool FetchTexture(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, UUID textureID, string format)
- {
- AssetBase texture;
- string fullID = textureID.ToString();
- if (format != DefaultFormat)
- fullID = fullID + "-" + format;
- if (!String.IsNullOrEmpty(m_RedirectURL))
- {
-
- texture = m_assetService.GetCached(fullID);
- if (texture != null)
- {
- if (texture.Type != (sbyte)AssetType.Texture)
- {
- httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
- return true;
- }
- WriteTextureData(httpRequest, httpResponse, texture, format);
- return true;
- }
- else
- {
- string textureUrl = m_RedirectURL + "?texture_id="+ textureID.ToString();
- m_log.Debug("[GETTEXTURE]: Redirecting texture request to " + textureUrl);
- httpResponse.StatusCode = (int)OSHttpStatusCode.RedirectMovedPermanently;
- httpResponse.RedirectLocation = textureUrl;
- return true;
- }
- }
- else
- {
-
- texture = m_assetService.GetCached(fullID);
- if (texture == null)
- {
-
- texture = m_assetService.Get(textureID.ToString());
- if (texture != null)
- {
- if (texture.Type != (sbyte)AssetType.Texture)
- {
- httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
- return true;
- }
- if (format == DefaultFormat)
- {
- WriteTextureData(httpRequest, httpResponse, texture, format);
- return true;
- }
- else
- {
- AssetBase newTexture = new AssetBase(texture.ID + "-" + format, texture.Name, (sbyte)AssetType.Texture, texture.Metadata.CreatorID);
- newTexture.Data = ConvertTextureData(texture, format);
- if (newTexture.Data.Length == 0)
- return false;
- newTexture.Flags = AssetFlags.Collectable;
- newTexture.Temporary = true;
- newTexture.Local = true;
- m_assetService.Store(newTexture);
- WriteTextureData(httpRequest, httpResponse, newTexture, format);
- return true;
- }
- }
- }
- else
- {
- WriteTextureData(httpRequest, httpResponse, texture, format);
- return true;
- }
- }
-
- httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
- return true;
- }
- private void WriteTextureData(IOSHttpRequest request, IOSHttpResponse response, AssetBase texture, string format)
- {
- string range = request.Headers.GetOne("Range");
- if (!String.IsNullOrEmpty(range))
- {
-
- int start, end;
- if (TryParseRange(range, out start, out end))
- {
-
-
- if (start >= texture.Data.Length)
- {
-
-
-
-
-
-
-
-
-
-
- response.StatusCode = (int)System.Net.HttpStatusCode.PartialContent;
- response.ContentType = texture.Metadata.ContentType;
- }
- else
- {
-
-
- if (end == -1)
- end = int.MaxValue;
- end = Utils.Clamp(end, 0, texture.Data.Length - 1);
- start = Utils.Clamp(start, 0, end);
- int len = end - start + 1;
-
-
-
-
-
-
-
- response.StatusCode = (int)System.Net.HttpStatusCode.PartialContent;
- response.ContentLength = len;
- response.ContentType = texture.Metadata.ContentType;
- response.AddHeader("Content-Range", String.Format("bytes {0}-{1}/{2}", start, end, texture.Data.Length));
- response.Body.Write(texture.Data, start, len);
- }
- }
- else
- {
- m_log.Warn("[GETTEXTURE]: Malformed Range header: " + range);
- response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
- }
- }
- else
- {
-
- response.StatusCode = (int)System.Net.HttpStatusCode.OK;
- response.ContentLength = texture.Data.Length;
- if (format == DefaultFormat)
- response.ContentType = texture.Metadata.ContentType;
- else
- response.ContentType = "image/" + format;
- response.Body.Write(texture.Data, 0, texture.Data.Length);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- private bool TryParseRange(string header, out int start, out int end)
- {
- start = end = 0;
- if (header.StartsWith("bytes="))
- {
- string[] rangeValues = header.Substring(6).Split('-');
- if (rangeValues.Length == 2)
- {
- if (!Int32.TryParse(rangeValues[0], out start))
- return false;
- string rawEnd = rangeValues[1];
- if (rawEnd == "")
- {
- end = -1;
- return true;
- }
- else if (Int32.TryParse(rawEnd, out end))
- {
- return true;
- }
- }
- }
- start = end = 0;
- return false;
- }
- private byte[] ConvertTextureData(AssetBase texture, string format)
- {
- m_log.DebugFormat("[GETTEXTURE]: Converting texture {0} to {1}", texture.ID, format);
- byte[] data = new byte[0];
- MemoryStream imgstream = new MemoryStream();
- Bitmap mTexture = null;
- ManagedImage managedImage = null;
- Image image = null;
- try
- {
-
-
- if (OpenJPEG.DecodeToImage(texture.Data, out managedImage, out image) && image != null)
- {
-
- mTexture = new Bitmap(image);
- using(EncoderParameters myEncoderParameters = new EncoderParameters())
- {
- myEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,95L);
-
- ImageCodecInfo codec = GetEncoderInfo("image/" + format);
- if (codec != null)
- {
- mTexture.Save(imgstream, codec, myEncoderParameters);
-
- data = imgstream.ToArray();
- }
- else
- m_log.WarnFormat("[GETTEXTURE]: No such codec {0}", format);
- }
- }
- }
- catch (Exception e)
- {
- m_log.WarnFormat("[GETTEXTURE]: Unable to convert texture {0} to {1}: {2}", texture.ID, format, e.Message);
- }
- finally
- {
-
-
- if (mTexture != null)
- mTexture.Dispose();
- if (image != null)
- image.Dispose();
- if(managedImage != null)
- managedImage.Clear();
- if (imgstream != null)
- imgstream.Dispose();
- }
- return data;
- }
-
- private static ImageCodecInfo GetEncoderInfo(String mimeType)
- {
- ImageCodecInfo[] encoders;
- encoders = ImageCodecInfo.GetImageEncoders();
- for (int j = 0; j < encoders.Length; ++j)
- {
- if (encoders[j].MimeType == mimeType)
- return encoders[j];
- }
- return null;
- }
- }
- }
|