aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Capabilities/Handlers/UploadBakedTexture
diff options
context:
space:
mode:
authorJustin Clark-Casey (justincc)2011-12-01 23:24:15 +0000
committerJustin Clark-Casey (justincc)2011-12-01 23:24:15 +0000
commit5460f2e035f50aade96b3daa0cb284bcb6faeea7 (patch)
tree623a22611e75c74f82fcb77664d971f463bcffb7 /OpenSim/Capabilities/Handlers/UploadBakedTexture
parentProvide more user feedback when "debug http" is set (diff)
downloadopensim-SC-5460f2e035f50aade96b3daa0cb284bcb6faeea7.zip
opensim-SC-5460f2e035f50aade96b3daa0cb284bcb6faeea7.tar.gz
opensim-SC-5460f2e035f50aade96b3daa0cb284bcb6faeea7.tar.bz2
opensim-SC-5460f2e035f50aade96b3daa0cb284bcb6faeea7.tar.xz
refactor: Separate the upload baked texture handler out from BunchOfCaps
Diffstat (limited to '')
-rw-r--r--OpenSim/Capabilities/Handlers/UploadBakedTexture/UploadBakedTextureHandler.cs179
1 files changed, 179 insertions, 0 deletions
diff --git a/OpenSim/Capabilities/Handlers/UploadBakedTexture/UploadBakedTextureHandler.cs b/OpenSim/Capabilities/Handlers/UploadBakedTexture/UploadBakedTextureHandler.cs
new file mode 100644
index 0000000..97b558c
--- /dev/null
+++ b/OpenSim/Capabilities/Handlers/UploadBakedTexture/UploadBakedTextureHandler.cs
@@ -0,0 +1,179 @@
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.Capabilities;
43using OpenSim.Framework.Servers;
44using OpenSim.Framework.Servers.HttpServer;
45using OpenSim.Region.Framework.Interfaces;
46using OpenSim.Services.Interfaces;
47using Caps = OpenSim.Framework.Capabilities.Caps;
48
49namespace OpenSim.Capabilities.Handlers
50{
51 public class UploadBakedTextureHandler
52 {
53 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
54
55 private Caps m_HostCapsObj;
56 private IAssetService m_assetService;
57 private bool m_persistBakedTextures;
58
59 public UploadBakedTextureHandler(Caps caps, IAssetService assetService, bool persistBakedTextures)
60 {
61 m_HostCapsObj = caps;
62 m_assetService = assetService;
63 m_persistBakedTextures = persistBakedTextures;
64 }
65
66 /// <summary>
67 /// Handle a request from the client for a Uri to upload a baked texture.
68 /// </summary>
69 /// <param name="request"></param>
70 /// <param name="path"></param>
71 /// <param name="param"></param>
72 /// <param name="httpRequest"></param>
73 /// <param name="httpResponse"></param>
74 /// <returns>The upload response if the request is successful, null otherwise.</returns>
75 public string UploadBakedTexture(
76 string request, string path, string param, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
77 {
78 try
79 {
80// m_log.Debug("[CAPS]: UploadBakedTexture Request in region: " + m_regionName);
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);
87 uploader.OnUpLoad += BakedTextureUploaded;
88
89 m_HostCapsObj.HttpListener.AddStreamHandler(
90 new BinaryStreamHandler("POST", capsBase + uploaderPath,
91 uploader.uploaderCaps));
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.Error("[CAPS]: " + e.ToString());
110 }
111
112 return null;
113 }
114
115 /// <summary>
116 /// Called when a baked texture has been successfully uploaded by a client.
117 /// </summary>
118 /// <param name="assetID"></param>
119 /// <param name="data"></param>
120 private void BakedTextureUploaded(UUID assetID, byte[] data)
121 {
122 // m_log.WarnFormat("[CAPS]: 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 class BakedTextureUploader
134 {
135 public event Action<UUID, byte[]> OnUpLoad;
136
137 private string uploaderPath = String.Empty;
138 private UUID newAssetID;
139 private IHttpServer httpListener;
140
141 public BakedTextureUploader(string path, IHttpServer httpServer)
142 {
143 newAssetID = UUID.Random();
144 uploaderPath = path;
145 httpListener = httpServer;
146 // m_log.InfoFormat("[CAPS] baked texture upload starting for {0}",newAssetID);
147 }
148
149 /// <summary>
150 /// Handle raw uploaded baked texture data.
151 /// </summary>
152 /// <param name="data"></param>
153 /// <param name="path"></param>
154 /// <param name="param"></param>
155 /// <returns></returns>
156 public string uploaderCaps(byte[] data, string path, string param)
157 {
158 Action<UUID, byte[]> handlerUpLoad = OnUpLoad;
159 if (handlerUpLoad != null)
160 {
161 Util.FireAndForget(delegate(object o) { handlerUpLoad(newAssetID, data); });
162 }
163
164 string res = String.Empty;
165 LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete();
166 uploadComplete.new_asset = newAssetID.ToString();
167 uploadComplete.new_inventory_item = UUID.Zero;
168 uploadComplete.state = "complete";
169
170 res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
171
172 httpListener.RemoveStreamHandler("POST", uploaderPath);
173
174 // m_log.InfoFormat("[CAPS] baked texture upload completed for {0}",newAssetID);
175
176 return res;
177 }
178 }
179} \ No newline at end of file