OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
UploadBakedTextureHandler.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.Specialized;
32 using System.Drawing;
33 using System.Drawing.Imaging;
34 using System.Reflection;
35 using System.IO;
36 using System.Web;
37 using log4net;
38 using Nini.Config;
39 using OpenMetaverse;
40 using OpenMetaverse.StructuredData;
41 using OpenMetaverse.Imaging;
42 using OpenSim.Framework;
43 using OpenSim.Framework.Capabilities;
44 using OpenSim.Framework.Servers;
45 using OpenSim.Framework.Servers.HttpServer;
46 using OpenSim.Region.Framework.Interfaces;
47 using OpenSim.Services.Interfaces;
49 
50 namespace OpenSim.Capabilities.Handlers
51 {
53  {
54 
55  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
56 
57  private Caps m_HostCapsObj;
58  private IAssetService m_assetService;
59  private bool m_persistBakedTextures;
60 
61  public UploadBakedTextureHandler(Caps caps, IAssetService assetService, bool persistBakedTextures)
62  {
63  m_HostCapsObj = caps;
64  m_assetService = assetService;
65  m_persistBakedTextures = persistBakedTextures;
66  }
67 
77  public string UploadBakedTexture(
78  string request, string path, string param, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
79  {
80  try
81  {
82  string capsBase = "/CAPS/" + m_HostCapsObj.CapsObjectPath;
83  string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000");
84 
85  BakedTextureUploader uploader =
86  new BakedTextureUploader(capsBase + uploaderPath, m_HostCapsObj.HttpListener, m_HostCapsObj.AgentID);
87  uploader.OnUpLoad += BakedTextureUploaded;
88 
89  m_HostCapsObj.HttpListener.AddStreamHandler(
91  "POST", capsBase + uploaderPath, uploader.uploaderCaps, "UploadBakedTexture", null));
92 
93  string protocol = "http://";
94 
95  if (m_HostCapsObj.SSLCaps)
96  protocol = "https://";
97 
98  string uploaderURL = protocol + m_HostCapsObj.HostName + ":" +
99  m_HostCapsObj.Port.ToString() + capsBase + uploaderPath;
100 
101  LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
102  uploadResponse.uploader = uploaderURL;
103  uploadResponse.state = "upload";
104 
105  return LLSDHelpers.SerialiseLLSDReply(uploadResponse);
106  }
107  catch (Exception e)
108  {
109  m_log.ErrorFormat("[UPLOAD BAKED TEXTURE HANDLER]: {0}{1}", e.Message, e.StackTrace);
110  }
111 
112  return null;
113  }
114 
120  private void BakedTextureUploaded(UUID assetID, byte[] data)
121  {
122  m_log.DebugFormat("[UPLOAD BAKED TEXTURE HANDLER]: Received baked texture {0}", assetID.ToString());
123 
124  AssetBase asset;
125  asset = new AssetBase(assetID, "Baked Texture", (sbyte)AssetType.Texture, m_HostCapsObj.AgentID.ToString());
126  asset.Data = data;
127  asset.Temporary = true;
128  asset.Local = !m_persistBakedTextures; // Local assets aren't persisted, non-local are
129  m_assetService.Store(asset);
130 
131  }
132  }
133 
135  {
136 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
137 
138  public event Action<UUID, byte[]> OnUpLoad;
139 
140  private string uploaderPath = String.Empty;
141  private UUID newAssetID;
142  private IHttpServer httpListener;
143  private UUID AgentId = UUID.Zero;
144 
145  public BakedTextureUploader(string path, IHttpServer httpServer, UUID uUID)
146  {
147  newAssetID = UUID.Random();
148  uploaderPath = path;
149  httpListener = httpServer;
150  AgentId = uUID;
151  // m_log.InfoFormat("[CAPS] baked texture upload starting for {0}",newAssetID);
152  }
153 
154 
155 
163  public string uploaderCaps(byte[] data, string path, string param)
164  {
165  Action<UUID, byte[]> handlerUpLoad = OnUpLoad;
166 
167  // Don't do this asynchronously, otherwise it's possible for the client to send set appearance information
168  // on another thread which might send out avatar updates before the asset has been put into the asset
169  // service.
170  if (handlerUpLoad != null)
171  handlerUpLoad(newAssetID, data);
172 
173  string res = String.Empty;
174  LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete();
175  uploadComplete.new_asset = newAssetID.ToString();
176  uploadComplete.new_inventory_item = UUID.Zero;
177  uploadComplete.state = "complete";
178 
179  res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
180 
181  httpListener.RemoveStreamHandler("POST", uploaderPath);
182 
183 // m_log.DebugFormat("[BAKED TEXTURE UPLOADER]: baked texture upload completed for {0}", newAssetID);
184 
185  return res;
186  }
187  }
188 }
string uploaderCaps(byte[] data, string path, string param)
Handle raw uploaded baked texture data.
BakedTextureUploader(string path, IHttpServer httpServer, UUID uUID)
string UploadBakedTexture(string request, string path, string param, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
Handle a request from the client for a Uri to upload a baked texture.
Interface to OpenSimulator's built in HTTP server. Use this to register handlers (http, llsd, xmlrpc, etc.) for given URLs.
Definition: IHttpServer.cs:36
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
OpenSim.Framework.Capabilities.Caps Caps
UploadBakedTextureHandler(Caps caps, IAssetService assetService, bool persistBakedTextures)