aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Capabilities/Handlers/GetTextureHandler.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Capabilities/Handlers/GetTextureHandler.cs')
-rw-r--r--OpenSim/Capabilities/Handlers/GetTextureHandler.cs356
1 files changed, 356 insertions, 0 deletions
diff --git a/OpenSim/Capabilities/Handlers/GetTextureHandler.cs b/OpenSim/Capabilities/Handlers/GetTextureHandler.cs
new file mode 100644
index 0000000..00186ee
--- /dev/null
+++ b/OpenSim/Capabilities/Handlers/GetTextureHandler.cs
@@ -0,0 +1,356 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using System.Collections;
30using System.Collections.Specialized;
31using System.Drawing;
32using System.Drawing.Imaging;
33using System.Reflection;
34using System.IO;
35using System.Web;
36using log4net;
37using Nini.Config;
38using OpenMetaverse;
39using OpenMetaverse.StructuredData;
40using OpenMetaverse.Imaging;
41using OpenSim.Framework;
42using OpenSim.Framework.Servers;
43using OpenSim.Framework.Servers.HttpServer;
44using OpenSim.Region.Framework.Interfaces;
45using OpenSim.Services.Interfaces;
46using Caps = OpenSim.Framework.Capabilities.Caps;
47
48namespace OpenSim.Capabilities.Handlers
49{
50
51 public class GetTextureHandler : BaseStreamHandler
52 {
53 private static readonly ILog m_log =
54 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
55 private IAssetService m_assetService;
56
57 public const string DefaultFormat = "x-j2c";
58
59 // TODO: Change this to a config option
60 const string REDIRECT_URL = null;
61
62 public GetTextureHandler(string path, IAssetService assService) :
63 base("GET", path)
64 {
65 m_assetService = assService;
66 }
67
68 public override byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
69 {
70 //m_log.DebugFormat("[GETTEXTURE]: called in {0}", m_scene.RegionInfo.RegionName);
71
72 // Try to parse the texture ID from the request URL
73 NameValueCollection query = HttpUtility.ParseQueryString(httpRequest.Url.Query);
74 string textureStr = query.GetOne("texture_id");
75 string format = query.GetOne("format");
76
77 if (m_assetService == null)
78 {
79 m_log.Error("[GETTEXTURE]: Cannot fetch texture " + textureStr + " without an asset service");
80 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
81 return null;
82 }
83
84 UUID textureID;
85 if (!String.IsNullOrEmpty(textureStr) && UUID.TryParse(textureStr, out textureID))
86 {
87 string[] formats;
88 if (format != null && format != string.Empty)
89 {
90 formats = new string[1] { format.ToLower() };
91 }
92 else
93 {
94 formats = WebUtil.GetPreferredImageTypes(httpRequest.Headers.Get("Accept"));
95 if (formats.Length == 0)
96 formats = new string[1] { DefaultFormat }; // default
97
98 }
99 // OK, we have an array with preferred formats, possibly with only one entry
100
101 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
102 foreach (string f in formats)
103 {
104 if (FetchTexture(httpRequest, httpResponse, textureID, f))
105 break;
106 }
107
108 }
109 else
110 {
111 m_log.Warn("[GETTEXTURE]: Failed to parse a texture_id from GetTexture request: " + httpRequest.Url);
112 }
113
114 httpResponse.Send();
115 return null;
116 }
117
118 /// <summary>
119 ///
120 /// </summary>
121 /// <param name="httpRequest"></param>
122 /// <param name="httpResponse"></param>
123 /// <param name="textureID"></param>
124 /// <param name="format"></param>
125 /// <returns>False for "caller try another codec"; true otherwise</returns>
126 private bool FetchTexture(OSHttpRequest httpRequest, OSHttpResponse httpResponse, UUID textureID, string format)
127 {
128// m_log.DebugFormat("[GETTEXTURE]: {0} with requested format {1}", textureID, format);
129 AssetBase texture;
130
131 string fullID = textureID.ToString();
132 if (format != DefaultFormat)
133 fullID = fullID + "-" + format;
134
135 if (!String.IsNullOrEmpty(REDIRECT_URL))
136 {
137 // Only try to fetch locally cached textures. Misses are redirected
138 texture = m_assetService.GetCached(fullID);
139
140 if (texture != null)
141 {
142 if (texture.Type != (sbyte)AssetType.Texture)
143 {
144 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
145 return true;
146 }
147 WriteTextureData(httpRequest, httpResponse, texture, format);
148 }
149 else
150 {
151 string textureUrl = REDIRECT_URL + textureID.ToString();
152 m_log.Debug("[GETTEXTURE]: Redirecting texture request to " + textureUrl);
153 httpResponse.RedirectLocation = textureUrl;
154 return true;
155 }
156 }
157 else // no redirect
158 {
159 // try the cache
160 texture = m_assetService.GetCached(fullID);
161
162 if (texture == null)
163 {
164 //m_log.DebugFormat("[GETTEXTURE]: texture was not in the cache");
165
166 // Fetch locally or remotely. Misses return a 404
167 texture = m_assetService.Get(textureID.ToString());
168
169 if (texture != null)
170 {
171 if (texture.Type != (sbyte)AssetType.Texture)
172 {
173 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
174 return true;
175 }
176 if (format == DefaultFormat)
177 {
178 WriteTextureData(httpRequest, httpResponse, texture, format);
179 return true;
180 }
181 else
182 {
183 AssetBase newTexture = new AssetBase(texture.ID + "-" + format, texture.Name, (sbyte)AssetType.Texture, texture.Metadata.CreatorID);
184 newTexture.Data = ConvertTextureData(texture, format);
185 if (newTexture.Data.Length == 0)
186 return false; // !!! Caller try another codec, please!
187
188 newTexture.Flags = AssetFlags.Collectable;
189 newTexture.Temporary = true;
190 m_assetService.Store(newTexture);
191 WriteTextureData(httpRequest, httpResponse, newTexture, format);
192 return true;
193 }
194 }
195 }
196 else // it was on the cache
197 {
198 //m_log.DebugFormat("[GETTEXTURE]: texture was in the cache");
199 WriteTextureData(httpRequest, httpResponse, texture, format);
200 return true;
201 }
202 }
203
204 // not found
205// m_log.Warn("[GETTEXTURE]: Texture " + textureID + " not found");
206 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
207 return true;
208 }
209
210 private void WriteTextureData(OSHttpRequest request, OSHttpResponse response, AssetBase texture, string format)
211 {
212 string range = request.Headers.GetOne("Range");
213 //m_log.DebugFormat("[GETTEXTURE]: Range {0}", range);
214 if (!String.IsNullOrEmpty(range)) // JP2's only
215 {
216 // Range request
217 int start, end;
218 if (TryParseRange(range, out start, out end))
219 {
220 // Before clamping start make sure we can satisfy it in order to avoid
221 // sending back the last byte instead of an error status
222 if (start >= texture.Data.Length)
223 {
224 response.StatusCode = (int)System.Net.HttpStatusCode.RequestedRangeNotSatisfiable;
225 return;
226 }
227
228 end = Utils.Clamp(end, 0, texture.Data.Length - 1);
229 start = Utils.Clamp(start, 0, end);
230 int len = end - start + 1;
231
232 //m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID);
233
234 if (len < texture.Data.Length)
235 response.StatusCode = (int)System.Net.HttpStatusCode.PartialContent;
236
237 response.ContentLength = len;
238 response.ContentType = texture.Metadata.ContentType;
239 response.AddHeader("Content-Range", String.Format("bytes {0}-{1}/{2}", start, end, texture.Data.Length));
240
241 response.Body.Write(texture.Data, start, len);
242 }
243 else
244 {
245 m_log.Warn("[GETTEXTURE]: Malformed Range header: " + range);
246 response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
247 }
248 }
249 else // JP2's or other formats
250 {
251 // Full content request
252 response.StatusCode = (int)System.Net.HttpStatusCode.OK;
253 response.ContentLength = texture.Data.Length;
254 if (format == DefaultFormat)
255 response.ContentType = texture.Metadata.ContentType;
256 else
257 response.ContentType = "image/" + format;
258 response.Body.Write(texture.Data, 0, texture.Data.Length);
259 }
260 }
261
262 private bool TryParseRange(string header, out int start, out int end)
263 {
264 if (header.StartsWith("bytes="))
265 {
266 string[] rangeValues = header.Substring(6).Split('-');
267 if (rangeValues.Length == 2)
268 {
269 if (Int32.TryParse(rangeValues[0], out start) && Int32.TryParse(rangeValues[1], out end))
270 return true;
271 }
272 }
273
274 start = end = 0;
275 return false;
276 }
277
278
279 private byte[] ConvertTextureData(AssetBase texture, string format)
280 {
281 m_log.DebugFormat("[GETTEXTURE]: Converting texture {0} to {1}", texture.ID, format);
282 byte[] data = new byte[0];
283
284 MemoryStream imgstream = new MemoryStream();
285 Bitmap mTexture = new Bitmap(1, 1);
286 ManagedImage managedImage;
287 Image image = (Image)mTexture;
288
289 try
290 {
291 // Taking our jpeg2000 data, decoding it, then saving it to a byte array with regular data
292
293 imgstream = new MemoryStream();
294
295 // Decode image to System.Drawing.Image
296 if (OpenJPEG.DecodeToImage(texture.Data, out managedImage, out image))
297 {
298 // Save to bitmap
299 mTexture = new Bitmap(image);
300
301 EncoderParameters myEncoderParameters = new EncoderParameters();
302 myEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 95L);
303
304 // Save bitmap to stream
305 ImageCodecInfo codec = GetEncoderInfo("image/" + format);
306 if (codec != null)
307 {
308 mTexture.Save(imgstream, codec, myEncoderParameters);
309 // Write the stream to a byte array for output
310 data = imgstream.ToArray();
311 }
312 else
313 m_log.WarnFormat("[GETTEXTURE]: No such codec {0}", format);
314
315 }
316 }
317 catch (Exception e)
318 {
319 m_log.WarnFormat("[GETTEXTURE]: Unable to convert texture {0} to {1}: {2}", texture.ID, format, e.Message);
320 }
321 finally
322 {
323 // Reclaim memory, these are unmanaged resources
324 // If we encountered an exception, one or more of these will be null
325 if (mTexture != null)
326 mTexture.Dispose();
327
328 if (image != null)
329 image.Dispose();
330
331 if (imgstream != null)
332 {
333 imgstream.Close();
334 imgstream.Dispose();
335 }
336 }
337
338 return data;
339 }
340
341 // From msdn
342 private static ImageCodecInfo GetEncoderInfo(String mimeType)
343 {
344 ImageCodecInfo[] encoders;
345 encoders = ImageCodecInfo.GetImageEncoders();
346 for (int j = 0; j < encoders.Length; ++j)
347 {
348 if (encoders[j].MimeType == mimeType)
349 return encoders[j];
350 }
351 return null;
352 }
353
354
355 }
356}